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 <Adafruit_NeoPixel.h>
#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>2){
doggo=0;
}
while (doggo==0){
//whole strip red
for (int i=0;i<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<256*5; j++) {
for(i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
strip.show();
}
}
delay(rainbowSpeed);
}
while (doggo==2){
//whole strip blue
for (int i=0;i<led;i++){
strip.setPixelColor(i,0,0,255);
strip.show();
}
}
}
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}