Detect button press inside a for() cycle I was working on a project with an LED strip, where I'd like to cycle through various effects with just a press of a button. The problem is that I don't know how to detect the press of the button to change effect while in for() cycling colors to have a rainbow effect (so it's a multitasking problem, I think).
Could you help me? :)

#include &lt;Adafruit_NeoPixel.h&gt;

#define pin 5
#define led 15
#define button 7
Adafruit_NeoPixel strip = Adafruit_NeoPixel (led,pin,NEO_RGB+NEO_KHZ800);

int doggo=0;

void setup(){
 strip.begin();
 pinMode(button,INPUT);
}
void loop(){


 int buttonVal=digitalRead(button);

 if(buttonVal==HIGH){
  doggo++;
  delay(300);
 }
 if (doggo&gt;2){
  doggo=0;
 }
 while (doggo==0){
  //whole strip red
  for (int i=0;i&lt;led;i++){
   strip.setPixelColor(i,255,0,0);
   strip.show();
  }
 }
 while (doggo==1){
  //rainbow effect
  uint16_t i, j;
  int rainbowSpeed=10;
  for(j=0; j&lt;256*5; j++) { 

   for(i=0; i&lt; strip.numPixels(); i++) {

    strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) &amp; 255));
    strip.show();

   }

  }

 delay(rainbowSpeed);


 }


 while (doggo==2){
  //whole strip blue
  for (int i=0;i&lt;led;i++){
   strip.setPixelColor(i,0,0,255);
   strip.show();
  }
 }

}


uint32_t Wheel(byte WheelPos) {
 WheelPos = 255 - WheelPos;

 if(WheelPos &lt; 85) {
  return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
 }
 if(WheelPos &lt; 170) {
  WheelPos -= 85;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
 }
 WheelPos -= 170;
 return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
You could use the Bounce2 library.

Call the update() method regularly and then check the state with read().

e.g.

for (...) {
  button.update();
  if (button.read() == HIGH) {
     //change pattern
     break;
  }
  // Do strip stuff.
}


As an added bonus the bounce library will debounce your button.