Need help inverting an action upon recieving same input

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

Of course, you could have edited the original post (don't worry, I've deleted it now) and added code tags.

digitalWrite(led, !digitalRead(led))

marco_c:
digitalWrite(led, !digitalRead(led))

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.

marco_c:
digitalWrite(led, !digitalRead(led))

WIERD!! The LED turns off when i start serial monitor on my pc ! ( my board is powered through USB ).

You can try a longer version

if (digitalRead(led)==HIGH) digitalWrite(led, LOW); else digitalWrite(led,HIGH);

Serial monitor resets the board when turned on.

WIERD!!

Wierd

marco_c: You can try a longer version

if (digitalRead(led)==HIGH) digitalWrite(led, LOW); else digitalWrite(led,HIGH);

Serial monitor resets the board when turned on.

Thanks for your help, but still the same problem! Anything else ?

Have you got brackets around the new 'if' statement? This groups it under the check for the IR code.

The logic is pretty simple here so not much else to suggest. Maybe repost the modified code? Use the # button when posting to get code tags

Just tried this code

void setup()
{
  pinMode(13,OUTPUT);
}

void loop()
{
  digitalWrite(13, !digitalRead(13));
  delay(500);
}

and it does what I expect - flash a LED with a 1 second cycle.

marco_c:
Have you got brackets around the new ‘if’ statement? This groups it under the check for the IR code.

The logic is pretty simple here so not much else to suggest. Maybe repost the modified code? Use the # button when posting to get code tags

The modified code is as below:

#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)==HIGH)
  digitalWrite(led, LOW);
else
  digitalWrite(led,HIGH);
  
}
}
}

is everything all right?

I tried a new code:

#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.

 if  (digitalRead(led)==LOW);

BZZZZZZT!

If we remove the extraneous braces and apply the auto-format tool, thisis what we get:

#include <IRremote.h>

const byte led = 12;
const byte 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);
}

AWOL:

 if  (digitalRead(led)==LOW);

BZZZZZZT!

If we remove the extraneous braces and apply the auto-format tool, thisis what we get:

#include <IRremote.h>

const byte led = 12;
const byte 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);
}

But the problem still remains the same.

Does the extraneous semicolon still remain?

AWOL: Does the extraneous semicolon still remain?

No, i used the same code as given by you.

Your logic is not as you intended

  if(results.value == 0xFF22DD)
  {
    if  (digitalRead(led)==LOW)
    {
      delay(5);
      digitalWrite(led, HIGH);
      delay(5);  
    }
    else
    {
      digitalWrite(led,LOW);
      delay(5);
    }
}

It really helps if you get into the habit of putting brackets and indentations in the right way. The flow of the program becomes much clearer.

No, i used the same code as given by you.

I left the extraneous semicolon in, to show how the auto-format tool helps reveal logical errors

FFFFFFFF is the repeat code of the IR NEC protocol

If you keep the key pressed you will get one numeric code followed by one or more FFFFFFFF, depending on how long you keep it pressed.

So just add a line to you code to ignore FFFFFFFF for your application.

Hope this helps