Using IR Remote to enable/disable an LED and make it blink at a certain speed

Hello,

I am new to programming and just started writing some code to do the following:

Using IR Remote to enable/disable an LED and make it blink at a certain speed using following button layout:

case 0xFFA25D:  // IR Remote Button: Power button
             Serial.print("LED On: ");
             digitalWrite(ledPin, HIGH);
             break;      
             
         case 0xFF30CF:  // IR Remote Button: 1
             Serial.print("LED Blink Fast: ");
             digitalWrite(ledPin, HIGH);
             delay(100);

             digitalWrite(ledPin, LOW);
             delay(100);
             break;     
             
         case 0xFF18E7:  // IR Remote Button: 2
             Serial.print("LED Blink Medium: ");
             digitalWrite(ledPin, HIGH);
             delay(500);

             digitalWrite(ledPin, LOW);
             delay(500);
             break; 

         case 0xFF7A85:  // IR Remote Button: 3
             Serial.print("LED Blink Slow: ");
             digitalWrite(ledPin, HIGH);
             delay(1000);

             digitalWrite(ledPin, LOW);
             delay(1000);
             break; 

         case 0xFF6897:  // IR Remote Button: 0
             Serial.print("LED Off: ");
             digitalWrite(ledPin, LOW);          
             break;

Unfortunately, it is only disabling the LED when pressing the “0” button and enabling the LED using the “Power” button. When pressing buttons 1, 2 or 3 it is only running the code once and the the LED turns off.

Full code:

// Import the IR-remote Library
#include <IRremote.h>

int IrReceiverPin = 12;                 // configure variable "IrReceiverPin" on pin 12
IRrecv irrecv(IrReceiverPin);           // create a new instance of "irrecv" and save it in the variable "IRrecv"
decode_results results;                 // define the variable "results" to save the received button code
int ledPin = 10;                        // configure port 10 as a LED pin.

void setup()
{
    Serial.begin(9600);                 // Configure the serial monitor

    pinMode(ledPin, OUTPUT);            // configure digital pin ledPin as an output
    
    // Verify if the IR-remote library is properly loading
    Serial.println("IR-receiver is starting...");

    irrecv.enableIRIn();               // start the IR-receiver
    Serial.println("IR-receiver active");

    digitalWrite(ledPin, LOW);          // turn off the LED
}

void loop() {

    // If the IR-receiver receives a signal:
    if (irrecv.decode(&results)) {

        // Print the received value as hexadecimal       
        Serial.println(results.value, HEX);

        // Let the IR-receiver listen for new incoming signals
        irrecv.resume();

        // Define which button was pressed
        switch (results.value) {
            
            case 0xFFA25D:  // IR-Remote Button: Power
                Serial.print("LED On: ");
                digitalWrite(ledPin, HIGH);
                break;      
                
            case 0xFF30CF:  // IR-Remote Button: 1
                Serial.print("LED Blink Fast: ");
                digitalWrite(ledPin, HIGH);
                delay(100);

                digitalWrite(ledPin, LOW);
                delay(100);
                break;     
                
            case 0xFF18E7:  // IR-Remote Button: 2
                Serial.print("LED Blink Medium: ");
                digitalWrite(ledPin, HIGH);
                delay(500);

                digitalWrite(ledPin, LOW);
                delay(500);
                break; 

            case 0xFF7A85:  // IR-Remote Button: 3
                Serial.print("LED Blink Slow: ");
                digitalWrite(ledPin, HIGH);
                delay(1000);

                digitalWrite(ledPin, LOW);
                delay(1000);
                break; 

            case 0xFF6897:  // IR-Remote Button: 0
                Serial.print("LED Off: ");
                digitalWrite(ledPin, LOW);          
                break;      
         
        }
        
    delay(100);      // pause for 100ms

}}

Video and photos attached (WeTransfer Link - https://we.tl/t-xyOHyafumc), hope someone can aid me in my quest.

Best regards,

Koen

Please post your full code in a post, not as an attachment. Also read the stickies "how to use the forum"; you will find instructions to properly post your code using code tags.

It's doing exactly what your code says. What do you actually want? When you press 1,2 or 3 is it supposed to keep on blinking while the key is still pressed? Or until some other button is pressed? Or something else?

If that's what you need then you can't use delay() to do the timing. You will need to use the millis() technique from the BlinkWithoutDelay example in the IDE.

Steve

Move the blink routine out of the switch statement. In the switch statement assign an interger to a variable.
i.e. rate=1 or 2 or 3 based on what IR code was received.

When you break out of the switch you can have a routine that blinks the led based on the rate.

So once you get that working you will discover a different problem which is how to get the value of
rate changed.

Now that you are in a loop blinking the led on and off how do you get a new value of rate?
That is what blink without delay solves. So you are going to have to learn how to do that.