#include <IRremote.h>
const int IR_RECEIVE_PIN = 2; // Define the pin number for the IR Sensor
String lastDecodedValue = ""; // Variable to store the last decoded value
const int LED_PINS[] = {3, 4, 5, 6};
void setup() {
Serial.begin(9600); // Start serial communication at 9600 baud rate
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK); // Start the IR receiver
for (int i = 0; i < sizeof(LED_PINS) / sizeof(LED_PINS[0]); i++) {
pinMode(LED_PINS[i], OUTPUT);
}
}
void loop() {
if (IrReceiver.decode()) {
String decodedValue = decodeKeyValue(IrReceiver.decodedIRData.command);
// Serial.println(IrReceiver.decodedIRData.command, HEX);
if (decodedValue != "ERROR" && decodedValue != lastDecodedValue) {
Serial.println(decodedValue);
lastDecodedValue = decodedValue; // Update the last decoded value
for (int i = 0; i < sizeof(LED_PINS) / sizeof(LED_PINS[0]); i++) {
digitalWrite(LED_PINS[i], LOW);
}
if (decodedValue == "1" || decodedValue == "2" || decodedValue == "3" || decodedValue == "4") {
int index = decodedValue.toInt() - 1;
Serial.println((index));
digitalWrite(LED_PINS[index], HIGH);
}
}
IrReceiver.resume(); // Enable receiving of the next value
}
}
String decodeKeyValue(long result)
{
switch(result){
case 0x68:
return "0";
case 0x30:
return "1";
case 0x18:
return "2";
case 0x7A:
return "3";
case 0x10:
return "4";
case 0x38:
return "5";
case 0x5A:
return "6";
case 0x42:
return "7";
case 0x4A:
return "8";
case 0x52:
return "9";
case 0x2:
return "+";
case 0x98:
return "-";
// case 0x7:
// return "EQ";
// case 0xD:
// return "U/SD";
// case 0x19:
// return "CYCLE";
case 0xA8:
return "PLAY/PAUSE";
case 0x90:
return "FORWARD";
case 0xE0:
return "BACKWARD";
case 0xA2:
return "POWER";
// case 0x47:
// return "MUTE";
case 0xE2:
return "MENU";
case 0x0:
return "ERROR";
default :
return "ERROR";
}
}