Infrared Remote -Blinking LEDS

Hi All! I’d like to start off by saying Hi to everyone.
I have like 1% knowledge with coding and using the Arduino board.

I’m trying to control the LEDs on/off with a Infrared Remote. Also while its on, I would want the LEDs to blink. So far after playing around with the codes, I’ve only managed to either have the LEDs blink continuously but I cant control with the remote.
Or doesn’t blink at all but I can control on/off with the remote.

Am I using the proper code for this? I’ve provided the code i’m currently using. Any help would be appreciated! Am I missing something for the “while” code??

Thanks!

#include <IRremote.h>
int RECV_PIN = 2;
int ALL_LED = 8;

long ALL_LED_OFF = 0xFF629D;
long ALL_LED_ON = 0xFFA25D;

IRrecv irrecv(RECV_PIN);
decode_results results;

void setup() {

irrecv.enableIRIn();
pinMode (RECV_PIN, INPUT);
pinMode (ALL_LED, OUTPUT);
Serial.begin(9600);

}

void loop()
{
while (digitalRead(RECV_PIN) == HIGH) {

}

digitalWrite(ALL_LED, HIGH);
delay(1000);
digitalWrite(ALL_LED, LOW);
delay(1000);

if (irrecv.decode(&results))

irrecv.resume();

switch(results.value){
case 0xFF629D:
Serial.println(" led off");
digitalWrite(ALL_LED, LOW);
break;
case 0xFFA25D:
Serial.println(" led on ");
digitalWrite(ALL_LED, HIGH);
break;
}

}

Hello

Have a look at the blink without delay example

That will set you on the right track

Some suggestions:

#include <IRremote.h>

////////// If it shouldn't ever change, add the 'const' keyword
const int RECV_PIN = 2;
const int ALL_LED = 8;

const long ALL_LED_OFF = 0xFF629D;
const long ALL_LED_ON = 0xFFA25D;

IRrecv irrecv(RECV_PIN);
decode_results results;
boolean blinking = true;

void setup() {
  irrecv.enableIRIn();
  pinMode (RECV_PIN, INPUT);
  pinMode (ALL_LED, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  /////// Let the library take care of the receive pin
  // while (digitalRead(RECV_PIN) == HIGH) {
  //
  // }

  ////////// Added a variable the IR can control to turn the blinking on or off
  if (blinking) {
    digitalWrite(ALL_LED, HIGH);
    delay(1000);
    digitalWrite(ALL_LED, LOW);
    delay(1000);
  }


  if (irrecv.decode(&results))
  { //////// <- You forgot this and its match.  No need to run the switch statement if you don't have new data.
    irrecv.resume();

    switch (results.value) {
      case ALL_LED_OFF:
        Serial.println(" led off");
        blinking = false;
        break;

      case ALL_LED_ON:
        Serial.println(" led on ");
        blinking = true;
        break;
    }
  }  ///////// <-- Its match
}

@john

The use of 2s delays for blinking means that the remote control will be heavily lagging. This is best handled with millis() and the blink without delay model

J-M-L:
The use of 2s delays for blinking means that the remote control will be heavily lagging. This is best handled with millis() and the blink without delay model

Yes, in almost any other case having two seconds worth of delay in the loop would produce unacceptable results. In this case there is no lag going from OFF to ON because when blinking is off there are no delays. When going from ON to OFF the visible delay may be as much as one second: if the delays are reached just as the button is pressed the LED will stay on for one full second and then stay off (1 second for the delay and then remaining off because blinking has been set to false).
If the BlinkWithoutDelay method is used it is possible for the LED to be ON at the time 'blinking' is set to false. An additional line is needed to turn off the LED when blinking is stopped.

Agree when off, but when blinking if you press the remote when entering the if (blinking) then you will get your 2 second lag, isn't it?

J-M-L:
Agree when off, but when blinking if you press the remote when entering the if (blinking) then you will get your 2 second lag, isn't it?

Well... you WILL get a two second delay in processing the IR message BUT, as I said before, you will only see the LED ON for at most one second after you press the OFF button. Then the LED will turn off and stay off until you press the ON button.
Things might act strangely if you try to turn the blinking OFF and ON very frequently. You may lose button presses.

Right fair point