Light up a led as long as you press a button on a remote control

Hi guys,

I am trying to light up two leds using a remote control. The - button for the green led and the + button for the red one. It all works fine but the leds toggle on and off everytime I press the related button. What I want is to have the leds lighted up as long as I press the button and to have them go off when I release the button.

This seems to be tricky for which I miss the experience yet.

Anyone of you to help?

I know that it can be done. Somewhere I have seen something similar but using a different and older RC library so that wasn't a big help for me.

Any help is appreciated. Below I have pasted a listing of the code.

Regards,

Marcel

/IR remote Example/
#include "IRremote.h"//include IRremote library to decode the IR signals
/Declare Constants/
int IRreceiver = 12; //pin connected to IR receiver
int ledRed = 13; //pin connected to anode of led
int ledGreen = 11; //pin connected to anode of led
/Declare objects/
IRrecv irrecv(IRreceiver);//create instance of 'IRrecv'
decode_results results;//create instance of 'decode_results'
/Setup/
void setup()
{
Serial.begin(9600);//initialize Serial communication
Serial.println("IR signals decoded");
irrecv.enableIRIn(); //start the IR receiver
pinMode(ledRed, OUTPUT);//initialize the led as output pin
pinMode(ledGreen, OUTPUT);//initialize the led as output pin
}
/loop/
void loop()
{
if (irrecv.decode(&results)) //check if it is received an IR signal
{
Serial.println(results.value, HEX);//print on Serial monitor the value received in hexadecimal.Read the value of the button that you want use and put it into the switch case
IRsignal();//call the function IRsignal()
irrecv.resume(); //receive the next value
delay(200);//wait 200 millisecond
}
}
void IRsignal() //takes action based on IR code received
{
int status_ledRed=0; //state of Red led
int status_ledGreen=0; //state of Green led
status_ledRed = digitalRead(ledRed); //read the status of ledRed
status_ledGreen = digitalRead(ledGreen); //read the status of ledGreen

switch(results.value) //check the IR signal received
{
case 0xFF906F: // Button code : +
if (status_ledRed==0){

Serial.println();
Serial.println("Turn on Red Led "); //print on Serial monitor:"Turn on Red Led"
digitalWrite(ledRed, HIGH); //turn on Red led
status_ledRed=1;
Serial.println();
Serial.print("Red Led ");
Serial.println(status_ledRed); //print the status of Red Led
Serial.print("Green Led ");
Serial.println(status_ledGreen); //print the status of Green Led
Serial.println();

break;
}
if (status_ledRed==1){

Serial.println();
Serial.println("Turn off Red Led "); //print on Serial monitor:"Turn off Red Led"
digitalWrite(ledRed, LOW); //turn off Red led
status_ledRed=0;
Serial.println();
Serial.print("Red Led ");
Serial.println(status_ledRed); //print the status of led Red
Serial.print("Green Led ");
Serial.println(status_ledGreen); //print the status of Green Led
Serial.println();

break;
}
case 0xFFA857: //Button code : -
if (status_ledGreen==0){

Serial.println();
Serial.println("Turn on Green Led "); //print on Serial monitor:"Turn on Green Led"
digitalWrite(ledGreen, HIGH); //turn on Green led
status_ledGreen=1;
Serial.println();
Serial.print("Green Led ");
Serial.println(status_ledGreen); //print the status of Green Led
Serial.print("Red Led ");
Serial.println(status_ledRed); //print the status of Red Led
Serial.println();

break;
}
if (status_ledGreen==1){

Serial.println();
Serial.println("Turn off Green Led "); //print on Serial monitor:"Turn off Green Led"
digitalWrite(ledGreen, LOW); //turn off Green led
status_ledGreen=0;
Serial.println();
Serial.print("Green Led ");
Serial.println(status_ledGreen); //print the status of Green Led
Serial.print("Red Led ");
Serial.println(status_ledRed); //print the status of Red Led
Serial.println();

break;
}

default:
Serial.println("Other button");
Serial.println();
}
}

What I want is to have the leds lighted up as long as I press the button

The remote sends a code when a button becomes pressed. If the button is held down, the remote sends a DIFFERENT code that means repeat the last discrete action.

There is NOTHING sent when you release the button.

So, your expectation are unrealistic.

Hi Paul,

Thanks for your reply.

I know that only once a normal code is sent when a button is pressed on the RC and that it goes to 0xFFFFFF when the button is being kept pressed. But this behaviour could be used I guess to understand when the button is released and with that also the lighting of the led.

Grtzzz, Marcel

But this behaviour could be used I guess to understand when the button is released and with that also the lighting of the led.

If you assume that your noticing that the repeat code is no longer being sent is close enough to when the switch is released, go for it.