Hello,
I am doing my first project with a remote and an IR receiver.
I want to control 3 LEDs with the remote, so that the LED is while the button is pressed and off after it is released.
So far I get to the point that the LED is on when I press the button, but it stays on even after it is released.
If I uncomment the last else statement in the code below the LED is off after release the button, but it is flickering at each loop when the button is hold.
Here is my code:
#include <IRremote.h>
const int led1 = 5;
const int led2 = 6;
const int led3 = 9;
const int recvPin = 7;
unsigned long lastCode;
IRrecv irrecv(recvPin);
decode_results results;
void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(recvPin, INPUT);
irrecv.enableIRIn();
}
void loop() {
if(irrecv.decode(&results))
{
if(results.value == 0xFFFFFFFF)
{
results.value = lastCode;
}
if(results.value == 0xFF30CF)
{
lastCode = results.value;
digitalWrite(led3, LOW);
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
}
if(results.value == 0xFF18E7)
{
lastCode = results.value;
digitalWrite(led3, LOW);
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
}
if(results.value == 0xFF7A85)
{
lastCode = results.value;
digitalWrite(led3, HIGH);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
}
delay(20);
irrecv.resume();
}
// else{
// digitalWrite(led3, LOW);
// digitalWrite(led1, LOW);
// digitalWrite(led2, LOW);
//
// }
}