ArduinoArduino UnoExact chip

IR remote and three LEDs

IRremote decodes the remote: keys 1, 2 and 3 toggle three LEDs, and every code is printed.

IR remote and three LEDs in Argynix Lab

The code

sketch.inoC++
// An IR receiver on pin 2 and the 20-key remote: IRremote decodes NEC. Keys 1, 2, 3
// toggle the LEDs on 9, 10, 11; power turns them all off. Press the remote's keys on
// the bench (or in the inspector).
#include <IRremote.hpp>

const int LEDS[] = {9, 10, 11};
bool on[3];

void setup() {
  Serial.begin(115200);
  for (int pin : LEDS) pinMode(pin, OUTPUT);
  IrReceiver.begin(2);
  Serial.println("Press a key on the remote");
}

void loop() {
  if (!IrReceiver.decode()) return;
  uint16_t cmd = IrReceiver.decodedIRData.command;
  bool repeat = IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT;
  IrReceiver.resume();
  if (repeat) return;
  Serial.print("Key code ");
  Serial.println(cmd);
  int which = cmd == 48 ? 0 : cmd == 24 ? 1 : cmd == 122 ? 2 : -1; // keys 1, 2, 3
  if (which >= 0) {
    on[which] = !on[which];
    digitalWrite(LEDS[which], on[which]);
  } else if (cmd == 162) {                                         // power
    for (int i = 0; i < 3; i++) digitalWrite(LEDS[i], on[i] = false);
  }
}
libraries.txtC++
IRremote