In this example we connect the IR reciever to pin 3 and 8 LEDs from pin 4 to pin 11.
We will press a key from 1 to 8 and that will switch the respective LED on, any other key switches the LEDs off
Code
#include “IRremote.h”
int receiver = 3;
IRrecv irrecv(receiver);
decode_results results;
const int NumberLeds = 8;
const int LedPins[] = {4,5,6,7,8,9,10,11};
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn();
// set pins as outputs
for (int led = 0; led < NumberLeds; led++)
{
pinMode(LedPins[led], OUTPUT);
digitalWrite(LedPins[led], HIGH);
}
}
void loop()
{
if (irrecv.decode(&results))
{
decodeIR();
irrecv.resume();
}
}
void decodeIR()
{
switch(results.value)
{
case 0xFF30CF:
digitalWrite(4, LOW);
break;
case 0xFF18E7:
digitalWrite(5, LOW);
break;
case 0xFF7A85:
digitalWrite(6, LOW);
break;
case 0xFF10EF:
digitalWrite(7, LOW);
break;
case 0xFF38C7:
digitalWrite(8, LOW);
break;
case 0xFF5AA5:
digitalWrite(9, LOW);
break;
case 0xFF42BD:
digitalWrite(10, LOW);
break;
case 0xFF4AB5:
digitalWrite(11, LOW);
break;
default:
AllOff();
}
delay(500);
}
void AllOff()
{
for (int led = 0; led < NumberLeds; led++)
{
pinMode(LedPins[led], OUTPUT);
digitalWrite(LedPins[led], HIGH);
}
}