#include <IRremote.h>
int irResult; // received result of irDecode
const int RECEIVER_PIN = 14; // analog input A1
const int ledLeft = 11;
const int ledRight = 3;
const int leftIR = 0xE0; // simulation IR code
const int rightIR = 0x90; // simulation IR code
unsigned long previousMillis = 0;
unsigned long currentMillis;
unsigned long interval = 400;
void setup() {
Serial.begin(9600);
pinMode(ledLeft, OUTPUT);
pinMode(ledRight, OUTPUT);
IrReceiver.begin(RECEIVER_PIN, ENABLE_LED_FEEDBACK);
}
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) { // if result is LEFT or 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 { // not 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
Serial.print("irResult: 0x"); // show the received code
if (irResult < 0x10)
Serial.print("0");
Serial.println(irResult, HEX);
IrReceiver.resume();
}
}