Hello, I`m trying to make simpel ledstrip code, so I can change the colors and patterns with a button. I want to do this while an other code is running for the colors. This works but when it is finiched the previous function goes again. Its just like its in a waiting list. How Can I fix this ?
Im using arduino uno and aWS2812B ledstrip.
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define commonPin 2
#define PIXEL_PIN 3
#define PIXEL_COUNT 300
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
int mode = 0;
unsigned long lastFire = 0;
void setup() {
pinMode(commonPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(commonPin), pressInterrupt, FALLING);
strip.begin();
strip.show();
Serial.begin(9600);
}
void loop() {
}
void pressInterrupt() {
if (millis() - lastFire < 200) {
return;
}
lastFire = millis();
Serial.println("druk");
if(++mode > 8) mode = 0;
switch(mode) {
case 0:
colorWipe(strip.Color( 0, 0, 0), 50); // Black/off
break;
case 1:
colorWipe(strip.Color(255, 0, 0), 50); // Red
break;
case 2:
colorWipe(strip.Color( 0, 255, 0), 50); // Green
break;
case 3:
colorWipe(strip.Color( 0, 0, 255), 50); // Blue
break;
case 4:
theaterChase(strip.Color(127, 127, 127), 50); // White
break;
case 5:
theaterChase(strip.Color(127, 0, 0), 50); // Red
break;
case 6:
theaterChase(strip.Color( 0, 0, 127), 50); // Blue
break;
case 7:
rainbow(10);
break;
case 8:
theaterChaseRainbow(50);
break;
}
}
void colorWipe(uint32_t color, int wait) {
for(int i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, color);
strip.show();
delay(wait);
}
}
void theaterChase(uint32_t color, int wait) {
for(int a=0; a<10; a++) {
for(int b=0; b<3; b++) {
strip.clear();
for(int c=b; c<strip.numPixels(); c += 3) {
strip.setPixelColor(c, color);
}
strip.show();
delay(wait);
}
}
}
void rainbow(int wait) {
for(long firstPixelHue = 0; firstPixelHue < 3*65536; firstPixelHue += 256) {
for(int i=0; i<strip.numPixels(); i++) {
int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
}
strip.show();
delay(wait);
}
}
void theaterChaseRainbow(int wait) {
int firstPixelHue = 0;
for(int a=0; a<30; a++) {
for(int b=0; b<3; b++) {
strip.clear();
for(int c=b; c<strip.numPixels(); c += 3) {
int hue = firstPixelHue + c * 65536L / strip.numPixels();
uint32_t color = strip.gamma32(strip.ColorHSV(hue));
strip.setPixelColor(c, color);
}
strip.show();
delay(wait);
firstPixelHue += 65536 / 90;
}
}
}
So when I change from function to a new function while the other function is running. And the new function is finiched the old starts to continuou.
I hope someone can help me.