IR Controlled LED Strip Fade and Blink

Hey there! I'm currently trying to program an LED strip to blink twice then begin fading in and out at the press of one button, and have it just fade in and out at another. The second button is working fine, but when I press the first one the LED just starts blinking infinitely. I'm not sure exactly where I've gone wrong, so any help would be much appreciated.

#include <IRremote.h>

#define first_key 48703
#define second_key 58359
int receiver_pin = 8;
int brightness = 0;  
int fadeAmount = 5; 
int led_pin = 7;
int led[] = {0,0};
IRrecv receiver(receiver_pin);
decode_results output;
void blink3()
{
  for (int count = 0; count < 3; count++)
  {
    digitalWrite(led_pin, brightness);
      brightness = 250;
    delay(500);
    digitalWrite(led_pin, brightness);
      brightness = 0;
    delay(500);
  }
}

void setup()
{
Serial.begin(9600);
receiver.enableIRIn();
pinMode(led_pin, OUTPUT);
}

void loop() {
if (receiver.decode(&output)) {
unsigned int value = output.value;
switch(value) {
case first_key:
if(led[1] == 1) {
digitalWrite(led_pin, brightness);
  brightness = 0;
led[1] = 0;
} else {
void blink3();
digitalWrite(led, brightness);
  brightness = brightness + fadeAmount;
  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }
    delay(30);
  led[1] = 1;
}
break;
case second_key:

if(led[2] == 1) {
digitalWrite(led_pin, brightness);
  brightness = 0;
led[2] = 0;
} else {
digitalWrite(led, brightness);
  brightness = brightness + fadeAmount;
  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }
    delay(30);
led[2] = 1;
}
break;
}
Serial.println(value);
receiver.resume();
}
}

The first thing you can do to help yourself and others is properly format your code... Ctrl-T in the IDE will tidy it right up.

#include <IRremote.h>

#define first_key 48703
#define second_key 58359
int receiver_pin = 8;
int brightness = 0;
int fadeAmount = 5;
int led_pin = 7;
int led[] = {0, 0};
IRrecv receiver(receiver_pin);
decode_results output;
void blink3()
{
  for (int count = 0; count < 3; count++)
  {
    digitalWrite(led_pin, brightness);
    brightness = 250;
    delay(500);
    digitalWrite(led_pin, brightness);
    brightness = 0;
    delay(500);
  }
}

void setup()
{
  Serial.begin(9600);
  receiver.enableIRIn();
  pinMode(led_pin, OUTPUT);
}

void loop() {
  if (receiver.decode(&output)) {
    unsigned int value = output.value;
    switch (value) {
      case first_key:
        if (led[1] == 1) {
          digitalWrite(led_pin, brightness);
          brightness = 0;
          led[1] = 0;
        } else {
          void blink3();
          digitalWrite(led, brightness);
          brightness = brightness + fadeAmount;
          if (brightness <= 0 || brightness >= 255) {
            fadeAmount = -fadeAmount;
          }
          delay(30);
          led[1] = 1;
        }
        break;
      case second_key:

        if (led[2] == 1) {
          digitalWrite(led_pin, brightness);
          brightness = 0;
          led[2] = 0;
        } else {
          digitalWrite(led, brightness);
          brightness = brightness + fadeAmount;
          if (brightness <= 0 || brightness >= 255) {
            fadeAmount = -fadeAmount;
          }
          delay(30);
          led[2] = 1;
        }
        break;
    }
    Serial.println(value);
    receiver.resume();
  }
}

That is not calling the function blink3(), it is just creating a declaration of it. remove the 'void' to actually call it.

You do not have and led[2] so you are accessing memory out of bounds. Your led array contains 2 elements and you access them with index 0 and 1.

If you brightness is out of bounds, you want to switch direction AND bring it back into bounds or else you will get an odd blink in there (not with a fadeAmount of 5, but if you change that, you will)

  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
    brightness = constrain(brightness, 0, 255 );
  }

The brightness of an LED can not be set by

digitalWrite(led_pin, brightness);

If brightness is zero, the Led will be off, any other value will switch it on. You need to use a PWM pin instead and analogWrite().

Here is an example (your sketch may still not do what you finally want but it should be a step in the right direction). Be aware that I changed it to fit to the supported IRremote lib as used in Wokwi; I did not consider the inputs from @blh64. It might be worth to check and consider them also!!

#include <IRremote.h>

#define first_key 48 //48703
#define second_key 24 // 58359
int receiver_pin = 8;
int brightness = 0;
int fadeAmount = 5;
int led_pin = 6;
int led[] = {0, 0};
IRrecv receiver(receiver_pin);
decode_results output;

void blink3()
{
  for (int count = 0; count < 3; count++)
  {
    digitalWrite(led_pin, brightness);
    brightness = 250;
    delay(500);
    digitalWrite(led_pin, brightness);
    brightness = 0;
    delay(500);
  }
}

void setup()
{
  Serial.begin(9600);
  receiver.enableIRIn();
  pinMode(led_pin, OUTPUT);
}

void loop() {
  if (receiver.decode()) {
    unsigned int value = receiver.decodedIRData.command ;
    switch (value) {
      case first_key:
        if (led[1] == 1) {
//          digitalWrite(led_pin, brightness);
          analogWrite(led_pin,brightness);
          brightness = 0;
          led[1] = 0;
        } else {
          blink3();
//          digitalWrite(led, brightness);
          analogWrite(led_pin,brightness);
          brightness = brightness + fadeAmount;
          if (brightness <= 0 || brightness >= 255) {
            fadeAmount = -fadeAmount;
          }
          delay(30);
          led[1] = 1;
        }
        break;
      case second_key:
        if (led[2] == 1) {
//        digitalWrite(led_pin, brightness);
          analogWrite(led_pin,brightness);
          brightness = 0;
          led[2] = 0;
        } else {
//          digitalWrite(led, brightness);
          analogWrite(led_pin,brightness);
          brightness = brightness + fadeAmount;
          if (brightness <= 0 || brightness >= 255) {
            fadeAmount = -fadeAmount;
          }
          delay(30);
          led[2] = 1;
        }
        break;
    }
    Serial.println(value);
    receiver.resume();
  }
}

Just check it out on Wokwi.

Thank you so much! I made all of the changes you recommended (and a few changes in terms of delay length), but now the LED strip is just staying turned on and not responding to any input from the remote.

#include <IRremote.h>

#define first_key 48703
#define second_key 58359
int receiver_pin = 7;
int brightness = 0;
int fadeAmount = 5;
int led_pin = 5;
int led[] = { 0, 0 };
IRrecv receiver(receiver_pin);
decode_results output;
void blink3() {
  for (int count = 0; count < 2; count++) {
    digitalWrite(led_pin, brightness);
    brightness = 250;
    delay(10);
    digitalWrite(led_pin, brightness);
    brightness = 0;
    delay(10);
  }
}

void setup() {
  Serial.begin(9600);
  receiver.enableIRIn();
  pinMode(led_pin, OUTPUT);
}

void loop() {
  if (receiver.decode(&output)) {
    unsigned int value = output.value;
    switch (value) {
      case first_key:
        if (led[1] == 1) {
          digitalWrite(led_pin, brightness);
          brightness = 0;
          led[1] = 0;
        } else {
          blink3();
          digitalWrite(led, brightness);
          brightness = brightness + fadeAmount;
          if (brightness <= 0 || brightness >= 255) {
            fadeAmount = -fadeAmount;
            brightness = constrain(brightness, 0, 255);
          }
          delay(30);
          led[1] = 1;
        }
        break;
        {
          case second_key:

            if (led[2] == 1) {
              digitalWrite(led_pin, brightness);
              brightness = 0;
              led[1] = 0;
            } else {
              digitalWrite(led, brightness);
              brightness = brightness + fadeAmount;
              if (brightness <= 0 || brightness >= 255) {
                fadeAmount = -fadeAmount;
                brightness = constrain(brightness, 0, 255);
              }
            }
            delay(30);
            led[1] = 1;
        }
        break;
    }
    Serial.println(value);
    receiver.resume();
  }
}

You are still accessing led[2] which is beyond the end of the array.

#include <IRremote.h>

#define first_key 48703
#define second_key 58359
int receiver_pin = 7;
int brightness = 0;
int fadeAmount = 5;
int led_pin = 5;
int led[] = { 0, 0 };
IRrecv receiver(receiver_pin);
decode_results output;
void blink3() {
  for (int count = 0; count < 2; count++) {
    digitalWrite(led_pin, LOW);
    delay(250);
    digitalWrite(led_pin, HIGH);
    delay(250);
  }
}

void setup() {
  Serial.begin(9600);
  receiver.enableIRIn();
  pinMode(led_pin, OUTPUT);
}

void loop() {
  if (receiver.decode(&output)) {
    unsigned int value = output.value;
    switch (value) {
      case first_key:
        Serial.println("First Key");
        // blink 3 times and then fade
        blink3();
        brightness = 255;
        fadeAmount = -5;
        break;

      case second_key:
        Serial.println("Second Key");

        // just fade
        brightness = 255;
        fadeAmount = -5;
        break;
    }
  }
  receiver.resume();
  analogWrite(led, brightness);
  brightness += fadeAmount;
  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
    brightness = constrain(brightness, 0, 255);
  }
  delay(30);
}

That was definitely a move in the right direction, the serial monitor is showing that everything should be operating, but the LED's are just simply not turning on at all now. I'm wondering now if this is a problem with the wiring. Please let me know if you see the issue, and once again thank you so much for all the help!

Post a detailed circuit diagram to see how we can help.

Write a simple test sketch that does nothing but set the pins to output and turn them on, delay, turn them off. See if that works to verify your wiring.

You don't have to stick to your main sketch to debug things...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.