Tuesday, 9 July 2013

An Even Better IR Decoder Library

I improved my old IR decoder library by adding some new features, without making it any more complicated. What's nice it that this is all done in the interrupt routine, so the library is still non-blocking. Here are the new features:

  • It no longer records invalid IR codes, and automatically resets when an invalid code is received (who cares if the code is invalid anyway?).
  • It removes the start bits, and the toggle bit from the code, to make it easier to determine what button was pressed on the remote.
  • It can determine whether a code is a repeat, or a new button press by the user.

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

As before, the ir_begin() function starts the decoder, but ir_data() is a little different:
  • If there is no valid code present, it will return a 0. 
  • If there is a new code, it will return the code. 
  • Finally, if it is receiving a string of codes with each one being a repeat of the previous one, it will return -1 until the string is broken.

Whats nice about the repeat detection, is that you don't have to worry about stuff happening more than once when you hold down a button on the remote. And if you do want something to happen for as long as the button is held down, like a volume control for example, you just need to look for the -1s.

Here is the example code:

#include "irDecoderV2.h"

const int IR_pin = 10;

int code = 0;

void setup()
  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
  else if(code == -1)  // If its a repeat
    Serial.println("Repeat");  // Print it
  delay(250);  // Prevent clogging up serial monitor

As before, 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