Hi everyone, I hope you're doing well.
I am very new to arduino programming, so this may seem like a very amateur question.
I have an LED light strip and an IR sensor/remote. I am trying to use the remote to switch between colors and effects.
The problem is, some of my effects use loops. For example, in oneRunningLightBack(), the leds light up one by one and the previous one turns off, making a running motion. This is a loop that goes on forever, as I have a while loop with (1<2) as the condition.
I would like to use the IR remote to switch to different effects, but do not know how to break out of this loop when a signal is detected. I would love some help on how to do this. My code is very small and I am willing to rewrite it.
Thank you!
#include <IRremote.h>
// LED Setup
#include <FastLED.h>
#define NUM_LEDS 60 // Stores number of LEDs you have.
#define LED_PIN 7 // Pin number your green wire is plugged into.
CRGB led[NUM_LEDS]; // Needed for every setup.
CRGB blue = CRGB(0, 0, 255);
CRGB red = CRGB(255, 0, 0);
CRGB green = CRGB(0, 255, 0);
CRGB lightOrange = CRGB(238, 168, 78);
CRGB lightPink = CRGB(230, 174, 206);
CRGB lightPurple = CRGB(209, 157, 239);
CRGB darkOrange = CRGB(204, 121, 0);
CRGB darkPurple = CRGB(30, 0, 80);
CRGB yellow = CRGB(200, 255, 0);
CRGB ledOff = CRGB(0, 0, 0);
// End LED
const int RECV_PIN = 2;
IRrecv irrecv(RECV_PIN);
decode_results results;
unsigned long key_value = 0;
void setup(){
Serial.begin(9600);
irrecv.enableIRIn();
irrecv.blink13(true);
// LED Setup II
FastLED.addLeds<NEOPIXEL, LED_PIN>(led, NUM_LEDS); // Needed for every setup.
for (int i = 0; i < NUM_LEDS; i++) {
led[i] = CRGB(255, 0, 0);
}
FastLED.show();
// End LED Setup II
}
void loop(){
checkForIRSignal(); // At bottom of page.
}
// LED Methods
void setColor(CRGB color) {
for (int i = 0; i < NUM_LEDS; i++) {
led[i] = color;
}
FastLED.show();
}
void oneRunningLightBack(CRGB color, int delayInt) {
for (int i = 0; i < NUM_LEDS; i++) {
led[i] = color;
FastLED.show();
delay(delayInt);
led[i] = ledOff;
}
for (int i = NUM_LEDS - 1; i >= 0; i--) {
led[i] = color;
FastLED.show();
delay(delayInt);
led[i] = ledOff;
}
}
// End LED Methods
void checkForIRSignal() {
if (irrecv.decode(&results)){
if (results.value == 0XFFFFFFFF)
results.value = key_value;
switch(results.value){
case 0xFFA25D:
Serial.println("CH-");
setColor(blue);
break;
case 0xFF629D:
Serial.println("CH");
setColor(red);
break;
case 0xFFE21D:
Serial.println("CH+");
setColor(green);
break;
case 0xFF22DD:
Serial.println("|<<");
oneRunningLightBack(blue, 20);
break;
case 0xFF02FD:
Serial.println(">>|");
break ;
case 0xFFC23D:
Serial.println(">|");
break ;
case 0xFFE01F:
Serial.println("-");
break ;
case 0xFFA857:
Serial.println("+");
break ;
case 0xFF906F:
Serial.println("EQ");
break ;
case 0xFF6897:
Serial.println("0");
break ;
case 0xFF9867:
Serial.println("100+");
break ;
case 0xFFB04F:
Serial.println("200+");
break ;
case 0xFF30CF:
Serial.println("1");
break ;
case 0xFF18E7:
Serial.println("2");
break ;
case 0xFF7A85:
Serial.println("3");
break ;
case 0xFF10EF:
Serial.println("4");
break ;
case 0xFF38C7:
Serial.println("5");
break ;
case 0xFF5AA5:
Serial.println("6");
break ;
case 0xFF42BD:
Serial.println("7");
break ;
case 0xFF4AB5:
Serial.println("8");
break ;
case 0xFF52AD:
Serial.println("9");
break ;
}
key_value = results.value;
irrecv.resume();
}
}