IR Remote control LEDs

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);


void loop() {


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);



// else{
// digitalWrite(led3, LOW);
// digitalWrite(led1, LOW);
// digitalWrite(led2, LOW);
// }


Your problem is that "while the button is pressed" can't be known completely by the receiver, since it only acts on individual commands. So you have to have some timing logic to maintain the LED state for some minimum time after receiving a key press event. It's a job for millis().

misschien helpt het als u zegt (in uw code) dat de led moet uitschakelen als u nogmaals op de knop drukt?


Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Is this associated with this thread?

Thanks.. Tom... :slight_smile: