Hi,
I am a noob & this is my second post. Please ignore my mistakes, if any.
I have a sketch with me which turns on an led after recieving an IR signal from an NEC coded IR remote. Upon recieving the code (0xFF22DD) from remote, led on pin 3 turns on. Now i want to turn off this led by pressing the same button on the remote again (Upon recieving the same code again). What change in the sketch is needed ? Please Help..... :~
Sketch::
#include <IRremote.h>
int led = 3;
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
{
pinMode(led, OUTPUT);
}
}
void loop()
{
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
irrecv.resume(); // Receive the next value
}
{
if(results.value == 0xFF22DD) digitalWrite(led, HIGH);
}
}
By using "digitalWrite(led, !digitalRead(led))", the led turns on after recieving the code, but wont turn off. (Ultimately, same as before). I have attached the screenshot of serial monitor as well as the edited sketch. Hope you will solve my problem.
#include <IRremote.h>
int led = 12;
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
{
pinMode(led, OUTPUT);
}
}
void loop()
{
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
irrecv.resume(); // Receive the next value
}
{
if(results.value == 0xFF22DD)
{
if (digitalRead(led)==LOW);
delay(5);
{
digitalWrite(led, HIGH);
delay(5);
}
}
else
digitalWrite(led,LOW);
delay(5);
}
}
I noticed something. My remote transmits FFFFFFFF along with any code if the key is even a bit longpressed. When i press the key & if the code is recieved without FFFFFFFF, the led switched its state. But to again switch the state of led, the code must contain FFFFFFFF. Any other keypress also switches the led state ( as the code is different).
So, FF22DD only turns the led ON, but the led is turned off by any other code. Why so ?
At least the led is turning on & off by remote. But i want it to turn on & off by the same key regardless the key is longpressed or shortpressed.
Any help is appreciated.