Saturday 29 June 2013

IR Decoder Library for Arduino



In a previous post we looked at a method (or algorithm) for decoding the Philips RC-5 protocol with the Arduino. The code was very simple, but not very efficient as it was 'blocking code'.
As a follow up to this, I have written a simple library for the Arduino which can decode RC-5 codes in the background, whilst your main sketch runs un-encumbered in the foreground.

The library can be downloaded here: https://github.com/frodofski/RC5_Decoder

Here is an example of using the library:

#include "irDecoder.h"

const int IR_pin = 10;

int code = 0;

void setup()
{
  Serial.begin(9600);  
  ir_begin(IR_pin);      // Start the decoder
}

void loop()
{
  code = ir_data();      // Check for IR code
  
  if(code != 0)          // If a code is available...
  {
    Serial.print("Code: ");  // Print it
    Serial.println(code);
  }
}




The code is very efficient and only uses a small percentage of the Arduino's computing power. This leaves plenty of computing power left for the rest of your sketch to run. One thing to note, is that the library configures timer2 in a way which might conflict with the Tone library, and also may cause problems when trying to use pwm on digital pins 9 and 10.

No comments:

Post a Comment