How to use IR Remote to break out of a loop?

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(); 
  }
}

Karma for using code tags in your first post.

Can you give a better description of what you want to achieve? I understand that you want to 'stop' something but not sure what.

your code needs to be re-designed fundamentally.
The following explanation gives an overview how it works.
It does not explain all details. Most tutorials about the code don't give this overwiew

The code you have posted above reads in the ir-code and then execute different functions dependeing on the received ir-code. These functions run through until the end using for-loops.

As long as the for-loop is executed nothing else can be done.

Your code needs to be re-designed to work like this
one big loop which is already given by function loop
inside this loop
proceed a single step of the light-pattern
check for new ir-code beeing received
if ir-code received stop actual light-pattern and change to new light-pattern

the basic pricciple is

void loop() {
  LightPattern();
  checkForIRSignal()
}

each call of function lightPattern runs through very quickly to enable a real short reaction-time on a new IR-remote-command. Very very quickly.

OK But your light-pattern shall change LEDs let's say every 2 seconds. How shall this work?
with medium -advanced programming technique using timing based on the function millis()

The basic principle of this is like cooking spaghetti
If the water boils you put the spaghetti into the pot and you take a look on your watch
let's say it is 13:10. The spaghetti need 12 minutes so they are al dente at 13:22

then while the spaghetti are cooking you take a repeated look onto your watch

looking at your watch 13:10 compare it with "target-time" 13:22 not reached yet
looking at your watch 13:11 compare it with "target-time" 13:22 not reached yet
looking at your watch 13:12 compare it with "target-time" 13:22 not reached yet
looking at your watch 13:13 compare it with "target-time" 13:22 not reached yet
....
looking at your watch 13:22 compare it with "target-time" 13:22 yippie! spaghetti al dente

In programming the "comparing with target-time" is done through calculating time-difference since start

StartTime = millis()

if (CurrentTime - StartTime >= cookingTime) {
take next action
}

with the numbers from above
CookingTime = 12

if (CurrentTime - StartTime >= CookingTime) {
take next action
}

if (13:10 - 13:10 >= 12) { // 13:10 - 13:10 = 0 lesser then 12 if-condition is false not yet time to "take next action"
take next action
}

if (13:11 - 13:10 >= 12) { // 13:10 - 13:10 = 1 lesser then 12 if-condition is false not yet time to "take next action"
take next action
}

if (13:10 - 13:10 >= 12) { // 13:10 - 13:10 = 2 lesser then 12 if-condition is false not yet time to "take next action"
take next action
}
.........
if (13:22 - 13:10 >= 12) { // 13:22 - 13:10 = 12 equals 12 if-condition is TRUE time to "take next action"
take next action
}

inside a light-pattern " take next action" is executing the code inside your for-loop without the delay()

the delaying is realisied by this kind of -if-conditions like described above

This enables delayed execution of certain parts of code with running through the main-loop at a high frequency

This technique is combined with a functionality called statemachine
when ever a new IR-code is decoded switch state of state-machine to a different-state
= execute a different light-pattern

delayed execution based on function millis()

if you have any questions to any details just post them here with adding your complete sketch.
Going on by

  • reading tutorials or examples
  • modifying your code and
  • asking new questions

is the ideal way how this user-forum shall work

best regards Stefan