#include <IRremote.h>
const int irReceivePin = 11;
const int LedPin = 9;
const int numberOfkeys = 5;
long irKeyCodes[numberOfkeys];
IRrecv irrecv(irReceivePin);
decode_results results;
void setup() {
Serial.begin(9600);
pinMode(irReceivePin, INPUT);
pinMode(LedPin, OUTPUT);
irrecv.enableIRIn();
IearnKeycodes(); // here is hang up
Serial.println("Press a Remote key");
}
void loop()
{
long key;
int brightness;
if (irrecv.decode(&results))
{
//if data received
{
irrecv.resume();
key = convertCodeToKey(results.value);
if (key >= 0)
{
Serial.print("got key");
Serial.println(key);
brightness = map(Key, 0, numberOfKeys - 1, 0, 255);
analogWrite(LedPin, brightness);
}
}
}
void LearnKeycodes()
{
while (irrecv.decode(&results))
irrecv.resume();
Serial.println("Ready to learn remote codes");
Long prevValue = -1;
int L = 0;
while (L < numberOfKeys)
{
Serial.print("press remote ley");
Serial.print(L);
while (true)
{
if (irrecv.decode(&results))
{
if (results.value != -1 &&
results.decode_type != UNKNOWEN &&
results.value != prevValue)
{
showReceivedData();
Serial.printLn(results.value);
irKeyCodes[L] = results.value;
I = I + 1;
prevValue = results.value;
irrecv; .resume();
break;
}
irrecv.resume();
}
}
}
Serial.printing("learning complete");
}
int convertCodeToKey(long code)
{
for (int i = 0; i < numberOfKeys; i++)
{
If(code == irKeyCodes[i])
{
return I;
}
}
return -1
}
void showReceivedData()
{
if (results.decode_type == UNKNOWEN)
{
Serial.printin("COULD NOT DECODE MESSAGE");
}
else
{
If (results.decode_type == NEC) {
Serial.print("decoded NEC;")
}
else If (results.decode_type == SONY) {
Serial.print("decoded sony:");
}
else If (results.decode_type == RC5) {
Serial.print("decoded RC5");
}
else If (results.decode_type == RC6) {
Serial.print("decoded RC6");
}
Serial.print("hex value=");
Serial.printin(results.value, HEX);
}
}