Toggle leds on and off with IR remote control problem

So I wrote a sketch to toggle on and off 3 leds with my IR remote control. For each led I have 1 on and 1 off button. I found a sketch that uses a switch case for the different remote control values and modified it for my use. It was fairly simple and works.
Now I want to use the same button to toggle on and of the led, I have tried lots of different things but I can't get it to work. Anyone that want to help me?
This is the first sketch that works, 1 button for on and 1 button to turn of the led:

#include <IRremote.h> // use the library for IR

int receiver = 11; // pin 1 of IR receiver to Arduino digital pin 11
const int led1 =  1;
const int led2 =  2;
const int led3 =  3;
IRrecv irrecv(receiver); // create instance of 'irrecv'

decode_results results;

void setup()

{
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  
  irrecv.enableIRIn(); // Start the receiver

}

void translateIR() // takes action based on IR code received

// describing Car MP3 IR codes on LCD module

{

  switch(results.value)

  {

    case 0xFF30CF:  digitalWrite(led1, HIGH);   break;

    case 0xFF18E7:  digitalWrite(led2, HIGH);   break;

    case 0xFF7A85:  digitalWrite(led3, HIGH);   break;
    
    case 0xFF10EF:  digitalWrite(led1, LOW);   break;

    case 0xFF38C7:  digitalWrite(led2, LOW);   break;

    case 0xFF5AA5:  digitalWrite(led3, LOW);   break;

  }
}

void loop()

{

  if (irrecv.decode(&results)) // have we received an IR signal?

  {

    translateIR();

       irrecv.resume(); // receive the next value
 
  }

}

This is the sketch in which I wanted to use the 1 button to turn on and of the led. It compiles but it doesn't work, nothing happens when I push the buttons on the remote control :frowning:

#include <IRremote.h> // use the library for IR

const int receiver = 11; // pin 1 of IR receiver to Arduino digital pin 11
const int led1 =  1;
const int led2 =  2;
const int led3 =  3;

IRrecv irrecv(receiver); // create instance of 'irrecv'

decode_results results;

void setup()

{  
  pinMode(led1, OUTPUT);
  irrecv.enableIRIn();
}

boolean lightState1 = false;
boolean lightState2 = false;
boolean lightState3 = false;
unsigned long last1 = millis();
unsigned long last2 = millis();
unsigned long last3= millis();

void loop()
{
  if (irrecv.decode(&results) == 0xFF30CF)
  {
    if (millis() - last1 > 300) {
      lightState1 =!lightState1;
      digitalWrite(led1,lightState1);
    }
    last1 = millis();
    }
  if (irrecv.decode(&results) == 0xFF18E7)
  {
    if (millis() - last2 > 300) {
      lightState2 =!lightState2;
      digitalWrite(led2,lightState2);
    }
    last2 = millis();
    }
    if (irrecv.decode(&results) == 0xFF7A85)
  {
    if (millis() - last3 > 300) {
      lightState3 =!lightState3;
      digitalWrite(led3,lightState3);
    }
    last3 = millis();
    }
 irrecv.resume();
}

In this code:

    if (millis() - last1 > 300) {
      lightState1 =!lightState1;
      digitalWrite(led1,lightState1);
    }
    last1 = millis();

you are resetting last1 to the current time every time through here, so millis()-last1 will never be greater than 300 (except perhaps the very first time through).
You should do this:

    if (millis() - last1 > 300) {
      lightState1 =!lightState1;
      digitalWrite(led1,lightState1);
      last1 = millis();
    }

You should do the same for last2 and last3 as well.

Also, at the very end of the code:

    }
 irrecv.resume();
}

You are calling resume() every time through loop(). I suspect that you should only do that when the library returns a decoded result, in which case you need to put this inside each of the if statements, for example the first one should be this:

  if (irrecv.decode(&results) == 0xFF30CF)
  {
    if (millis() - last1 > 300) {
      lightState1 =!lightState1;
      digitalWrite(led1,lightState1);
      last1 = millis();
    }
    irrecv.resume();
  }

And, again, do a similar thing with the other two if statements.

Pete

This is the first sketch that works, 1 button for on and 1 button to turn of the led:

So. eliminate 3 cases. Add three byte variables, ledState1, ledState2, and ledState3. In each case, add:

ledStateN = !ledStateN;

where N is 1, 2, or 3. Then, use ledStateN in the digitalWrite() statement, instead of HIGH or LOW.

Thanks for your replies, I have tried both of your solutions but I can't get it to work.
I think my problem is that when I write the following simple sketch it doesn't work, the strange thing is the sketch (that I posted first) with the switch case does work.

void setup()

{  
  pinMode(led1, OUTPUT);
  irrecv.enableIRIn();
}



void loop()
{
  if (irrecv.decode(&results) == 0xFF30CF)
  { 
      digitalWrite(led1,HIGH);
      }
      irrecv.resume();
  }

The FF30CF is the HEX code that I receive when I push the 1 button on my remote. I have checked and double checked that.

the strange thing is the sketch (that I posted first) with the switch case does work.

Because you've got the resume() in the right place.

In your latest code you've got resume() in the wrong place (assuming that the test for 0xFF30CF is correct).

Pete

I think my problem is that when I write the following simple sketch it doesn't work, the strange thing is the sketch (that I posted first) with the switch case does work.

That sketch won't even compile. The irrecv object is not defined. Post your whole sketch.

Learn to use Serial.begin(), Serial.print(), and Serial.println() to debug your code.

Do not, unless you absolutely must, use digital pins 0 or 1 for anything except serial input/output.

This:

  if (irrecv.decode(&results) == 0xFF30CF)

is wrong. The decode() function returns 0 or 1 (true or false) to indicate success/failure to decode what the device received. The value that is received is stored in the struct results. The results.value variable may or may not contain the 0xFF30CF value.

Look at your working code to see how to use the decode function.

Thanks very much for the help. I finally got it working :slight_smile:
This is the working sketch.

#include <IRremote.h> // use the library for IR

const int receiver = 11; // pin 1 of IR receiver to Arduino digital pin 11
const int led1 =  3;
const int led2 =  4;
const int led3 =  5;

IRrecv irrecv(receiver); // create instance of 'irrecv'

decode_results results;

boolean lightState1 = false;
boolean lightState2 = false;
boolean lightState3 = false;
unsigned long last1 = millis();
unsigned long last2 = millis();
unsigned long last3 = millis();

void setup()

{  
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  irrecv.enableIRIn();
}



void loop()
{
  if (irrecv.decode(&results))
  {
    if (results.value == 0xFF30CF)
    {
      if (millis() - last1 > 300) 
      {
      lightState1 =!lightState1;
      digitalWrite(led1,lightState1);
      last1 = millis();
      }
    }
    
      
    if (results.value == 0xFF18E7)
    {
      if (millis() - last2 > 300)
      {
      lightState2 =!lightState2;
      digitalWrite(led2,lightState2);
      last2 = millis();  
      }
    }

    if (results.value == 0xFF7A85)
      {
       if (millis() - last3 > 300) 
       {
       lightState3 =!lightState3;
       digitalWrite(led3,lightState3);
       last3 = millis();
       }
      }
irrecv.resume();  
  }
}