Problem with IRControl Program

Hi,
I am tring to press a key on my remote controller to activate a blinking LED. The Problem is at “ir_recv.resume();” The Code seems to stop (wait) here, so the blinking isn’t done.

Do you please could help me with tips how to avoid that problem?

Later this should work with 3 LED’s. Pressing Key1 LED1 is blinking, pressing Key2 LED2 is blinking and so on.

Here’s my sketch :

#include <IRremote.h> // Load IR Library
const long ir_key1 = 16724175; // IR Code Key 1
const int ir_pin = 12;
IRrecv ir_recv(ir_pin);
decode_results ir_results;

const int led1_pin = 9; // Red LED
const int led1_intervall = 100;
int led1_onoff = LOW;
int led1_status = LOW;
unsigned long led1_time;

void setup() {
  // IR Stuff
  Serial.begin(9600);
  ir_recv.enableIRIn(); // Start the IR Receiver
  
  // LED Stuff
  pinMode(led1_pin, OUTPUT);
  led1_time = millis(); // Safe time to Var
}

void loop() {
  if(ir_recv.decode(&ir_results)){
    Serial.println(ir_results.value, DEC);
    
    if(ir_results.value == ir_key1){
      led1_onoff = !led1_onoff; // Toggle LED blink on / off
    }
    if(led1_onoff == HIGH){
      if((millis() - led1_time) >= led1_intervall){
        led1_time = millis(); // Get new time
        led1_status = !led1_status;
        digitalWrite(led1_pin, led1_status); // LED on/off
      }
    }
    ir_recv.resume();
  } 
}

Thank you,
Mag

The Problem is at “ir_recv.resume();” The Code seems to stop (wait) here, so the blinking isn’t done.

Wrong. The code toggles the LED when a button on the remote control is pressed. It does NOT set a flag that says “Hey, blinking should be happening”.

You need to investigate state machines. Each button on the IR should result in a state change.

Independently, you need to check the state (LEDOneBlinking, LEDOneNotBlinking, LEDTwoBlinking, etc.), and perform the appropriate action. You can, of course, have multiple independent states.

Your " if(led1_onoff == HIGH){" is INSIDE the " if(ir_recv.decode(&ir_results)){". Your blinking only gets executed once each time a new keypress arrives. Move it OUTSIDE the if.

johnwasser:
Your " if(led1_onoff == HIGH){" is INSIDE the " if(ir_recv.decode(&ir_results)){". Your blinking only gets executed once each time a new keypress arrives. Move it OUTSIDE the if.

Perfect!

Thank you.