I am working with an IR receiver, and would like to switch a LED on and off.
When I press the button the LED should turn on.
When I release the button the LED should turn off.
Here is the code for turning on the LED.
#include <IRremote.h>
int RECV_PIN = 2; // IR Reciever
int LED_PIN = 6; // LED
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup(){
irrecv.enableIRIn(); // Start the receiver
pinMode(LED_PIN, OUTPUT);
}
void loop() {
if (irrecv.decode(&results)) {
if (results.value == 3672802284){
digitalWrite(LED_PIN, HIGH);
}
irrecv.resume(); // Receive the next value
}
}
No problem turning on the LED.
How do I turn the LED of, when I release the button?
If your remote control sends continuous pulses while you hold the button down, then what you need to do is turn the LED off if you haven't received any pulses for a certain amount of time. If your remote only sends one pulse, then you can't detect when the button is released.
btw in case it's of any use to you, I have a PCB design (and spare PCBs) for a 1-button remote control, based on the ATtiny45. I built it to turn my TV, PVR and audio systems on or off in one go.