#include <IRremote.h>
int irResult; // make an IRremote object to control
const int RECEIVER_PIN = 14; // analog input A1
const int ledLeft = 12;
const int ledRight = 13;
const int leftIR = 0xE0; // simulation IR code
const int rightIR = 0x90; // simulation IR code
// const int leftIR = 0x08; // original
// const int rightIR = 0x5A; // original
unsigned long previousMillis = 0;
unsigned long currentMillis;
unsigned long interval = 400;
void setup() {
Serial.begin(9600);
IrReceiver.begin(RECEIVER_PIN);
pinMode(ledLeft, OUTPUT);
pinMode(ledRight, OUTPUT);
}
void loop() {
if (IrReceiver.decode()) // if the IR receiver found something...
decodeResults(); // determine which button was pressed
leftOrRight(irResult); // "blink" the correct side
}
//********************************************************************************
//* FUNCTIONS
//********************************************************************************
void blinkLED(int whichSide) {
currentMillis = millis(); // start an event timer
if ((currentMillis - previousMillis) > interval) { // compare "last" timer to "this" timer
previousMillis = currentMillis; // if greater thean "interval" save the "last" timer
digitalWrite (whichSide, !digitalRead(whichSide)); // and blink the correct LED
}
}
void leftOrRight(int irResult) {
if (irResult == leftIR || irResult == rightIR) { // compare the results to LEFT and RIGHT
if (irResult == leftIR) {
digitalWrite(ledRight, LOW); // turn off the other LED
blinkLED(ledLeft); // blink this LED
}
if (irResult == rightIR) {
digitalWrite(ledLeft, LOW); // turn off the other LED
blinkLED(ledRight); // blink this LED
}
} else { // did not see LEFT or RIGHT
digitalWrite(ledLeft, LOW); // turn off both LEDs
digitalWrite(ledRight, LOW); // turn off both LEEDs
}
}
void decodeResults() {
irResult = (IrReceiver.decodedIRData.command); // find the code
if (irResult) { // if the code was other than 0x00
IrReceiver.resume();
Serial.print("irResult: 0x"); // show the received code
Serial.println(irResult, HEX);
}
}