Async?

Hello,
I am currently working on a project where i have to initiate a function, that has 2 for loops in it:

void turnleft() {
  for(int i = 3; i >= 0; i--){
    leds[i] = CRGB(255, 255, 0);
    FastLED.show();
    delay(300);
  }
  for(int i = 3; i >= 0; i--){
    leds[i] = CRGB(0, 0, 0);
    FastLED.show();
    delay(300);
  }
}

And i need to check if a button was pressed while this was initiated:

while (digitalRead(leftPin) == LOW || digitalRead(rightPin) == LOW  || digitalRead(emerPin) == LOW ) {
      turnleft();
    }

Is there any way that i can make the reaction instant, like if i press a button than the function stops?

Study the Blink Without Delay tutorial to learn the basic concept of writing non-blocking code:

See Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

If you happen to be using a ESP32, the built in OS, freeRTOS, will easily handle this task

xjulek:
And i need to check if a button was pressed while this was initiated:

If you want a responsive program you should not use FOR or WHILE as they are blocking loops. Just use IF and allow loop() to do the iteration.

Of course if the FOR or WHILE always completes in a few millisecs it may not be a problem.

Have a look at how the code is organized in Several Things at a Time

Note how each function runs very briefly and returns to loop() so the next one can be called. None of the functions tries to complete a task in one call. And there may be dozens of calls to a function before it is actually time for it to do anything.

...R