-
Notifications
You must be signed in to change notification settings - Fork 0
Receiving with the IRremote library
This page gives some simple examples of how to receive IR codes with the IRremote Arduino library.
The following sketch will receive codes and print them to the serial port. This sketch is very useful for testing IR receiving, and for determining what code values to use in your code. A slightly more complex version is in the examples directory as IRrecvDump.
This sketch also illustrates how to perform an action while a button is pressed. In this example, the action is writing to the serial port.
#include <IRremote.h>
IRrecv irrecv(11); // Receive on pin 11
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}
void loop() {
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
irrecv.resume(); // Continue receiving
}
}This sketch illustrates how to perform an action when a button is pressed, and another action when a button is released. Note that an IR remote repeatedly sends a code while a button is pressed, so the code uses a timer to determine if the button has been released (i.e. no code has been received for a little while).
In this example, the LED is turned on when the button is pressed, and off when the button is released. The variable state keeps track of the current state – on or off. If a code is received, state is set to HI. If nothing is received for 150ms, state is set to LOW.
#include <IRremote.h>
#define MAX_TIME 150 // max ms between codes
#define LED_PIN 3
long lastPressTime = 0;
int state = LOW;
IRrecv irrecv(11);
decode_results results;
void setup()
{
pinMode(LED_PIN, OUTPUT); // Status on pin 3 LED
irrecv.enableIRIn(); // Start the receiver
}
void loop() {
if (irrecv.decode(&results)) {
if (1) { // Can check for a specific button here
if (state == LOW) {
state = HIGH; // Button pressed, so set state to HIGH
digitalWrite(LED_PIN, HIGH);
}
lastPressTime = millis();
}
irrecv.resume(); // Receive the next value
}
if (state == HIGH && millis() - lastPressTime > MAX_TIME) {
state = LOW; // Haven't heard from the button for a while, so not pressed
digitalWrite(LED_PIN, LOW);
}
}Note the line if (1). This will cause any button to work. To check for a specific button, replace it with something like if (results.value == 0x1234) where 0×1234 is the code associated with your desired button.
This sketch illustrates how to use a button on the remote as a toggle. Press a button once to turn the LED on, and press again to turn the LED off. A timer is used to determine if the button is held down, or is being pressed a new time. For a new press, the state is toggled.
#include <IRremote.h>
#define MAX_TIME 150
#define LED_PIN 3
IRrecv irrecv(11);
decode_results results;
long lastPressTime = 0;
int state = LOW;
void setup()
{
pinMode(LED_PIN, OUTPUT);
irrecv.enableIRIn(); // Start the receiver
}
void loop() {
if (irrecv.decode(&results)) {
if (1) {
if (millis() - lastPressTime > MAX_TIME) {
// It's been a while since the last press, so this
// must be a new press.
// Toggle the state
state = 1 - state;
digitalWrite(LED_PIN, state);
}
lastPressTime = millis();
}
irrecv.resume(); // Receive the next value
}
}