Here is the latest. I've included all the animations this time as I removed them prior to shorten it down a bit. It has the same issue for the other animations. I've managed to get the 'if (mode != 0)' working to bypass the extra pass for the first few animations but that is only a workaround not a solution.
Thanks again for your help.
#include <Adafruit_NeoPixel.h>
//#include <EEPROM.h>
#define LED_PIN0 5
#define LED_PIN1 6
#define LED_PIN2 7
#define LED_PIN3 8
int LED_COUNT = 20;
#define LED_SELECT 0
int address = 0;
//BUTTON
const int Button = 2;
int volatile ButtonActive = LOW;
int volatile ButtonHold;
int volatile ButtonLong = 0;
int volatile ButtonShort = 0;
unsigned long volatile ButtonPreviousMillis = 0;
const int ButtonHoldInterval = 1000;
unsigned long volatile currentMillis;
unsigned long previousMillis = 0;
const long interval = 500;
int singlestrip = 0;
//Adafruit_NeoPixel strip1(LED_COUNT, LED_PIN3, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip[] = {
Adafruit_NeoPixel(LED_COUNT, LED_PIN0),
Adafruit_NeoPixel(LED_COUNT, LED_PIN1),
Adafruit_NeoPixel(LED_COUNT, LED_PIN2),
Adafruit_NeoPixel(LED_COUNT, LED_PIN3),
};
size_t numStrips = sizeof(strip) / sizeof(Adafruit_NeoPixel);
int volatile mode;
void setup() {
Serial.begin(9600);
pinMode(Button, INPUT_PULLUP);
for (uint8_t a = 0; a < numStrips; a++) {
strip[a].begin();
strip[a].clear();
strip[a].setBrightness(20);
strip[a].show();
}
attachInterrupt(digitalPinToInterrupt(Button), ButtonControl, LOW);
//mode = EEPROM.read(address);
mode = 0; //to be able to override EEPROM
}
void loop() {
//Serial.print("The mode is ");
//Serial.println(mode);
switch (mode) {
case 0:
rainbow(10);
break;
case 1:
drip(random(200, 1000));
//dripTogether(random(200, 1000));
break;
case 2:
raindrops(250);
break;
case 3:
lightning(random(10, 2000));
break;
case 4:
snake(50);
break;
case 5:
breathebright(17);
break;
case 6:
breathesat(17);
break;
case 7:
theaterChaseRainbow(100);
break;
case 8:
theaterChase(strip[0].Color(127, 127, 127), 150); // White, half brightness
theaterChase(strip[0].Color(127, 0, 0), 150); // Red, half brightness
theaterChase(strip[0].Color(0, 0, 127), 150); // Blue, half brightness
break;
case 9:
colorWipe(strip[0].Color(255, 0, 0), 50); // Red
colorWipe(strip[0].Color(0, 255, 0), 50); // Green
colorWipe(strip[0].Color(0, 0, 255), 50); // Blue
break;
case 10:
rainbowrange(10);
break;
}
}
void rainbow(int wait) {
for (long firstPixelHue = 0; firstPixelHue < 1 * 65536; firstPixelHue += 256) {
if (mode != 0) {
Serial.println("trying0");
break;
}
for (int i = 0; i < LED_COUNT; i++) {
int pixelHue = firstPixelHue + (i * 65536L / LED_COUNT);
strip[0].setPixelColor(i, strip[0].gamma32(strip[0].ColorHSV(pixelHue)));
strip[1].setPixelColor(i, strip[1].gamma32(strip[1].ColorHSV(pixelHue)));
strip[2].setPixelColor(i, strip[2].gamma32(strip[2].ColorHSV(pixelHue)));
strip[3].setPixelColor(i, strip[3].gamma32(strip[3].ColorHSV(pixelHue)));
}
strip[0].show();
strip[1].show();
strip[2].show();
strip[3].show();
delay(wait);
}
}
void drip(int freq) {
uint32_t color = strip[0].Color(110, 110, 255);
int wait = 500;
int dripmode = random(0, 4);
int maxspeed = 20;
if (mode != 1) {
Serial.println("trying1");
return;
}
switch (dripmode) {
case 0:
for (int i = LED_COUNT; i >= 0; i--) {
//ButtonControl();
int accel = pow(2, i);
strip[0].setPixelColor(i, color);
strip[0].show();
strip[0].clear();
delay(wait);
wait = (max(wait - sqrt(wait), maxspeed)) / 1.8;
}
break;
case 1:
for (int i = LED_COUNT; i >= 0; i--) {
//ButtonControl();
int accel = pow(2, i);
strip[1].setPixelColor(i, color);
strip[1].show();
strip[1].clear();
delay(wait);
wait = (max(wait - sqrt(wait), maxspeed)) / 1.8;
}
break;
case 2:
for (int i = LED_COUNT; i >= 0; i--) {
//ButtonControl();
int accel = pow(2, i);
strip[2].setPixelColor(i, color);
strip[2].show();
strip[2].clear();
delay(wait);
wait = (max(wait - sqrt(wait), maxspeed)) / 1.8;
}
break;
case 3:
for (int i = LED_COUNT; i >= 0; i--) {
//ButtonControl();
int accel = pow(2, i);
strip[3].setPixelColor(i, color);
strip[3].show();
strip[3].clear();
delay(wait);
wait = (max(wait - sqrt(wait), maxspeed)) / 1.8;
}
break;
}
strip[0].clear();
strip[0].show();
strip[1].clear();
strip[1].show();
strip[2].clear();
strip[2].show();
strip[3].clear();
strip[3].show();
delay(freq);
}
void dripTogether(int freq) {
uint32_t color = strip[0].Color(110, 110, 255);
int b = 500;
int wait = b;
for (int i = LED_COUNT; i >= 0; i--) {
//ButtonControl();
int accel = pow(2, i);
strip[0].setPixelColor(i, color);
strip[0].show();
strip[0].clear();
strip[1].setPixelColor(i, color);
strip[1].show();
strip[1].clear();
strip[2].setPixelColor(i, color);
strip[2].show();
strip[2].clear();
strip[3].setPixelColor(i, color);
strip[3].show();
strip[3].clear();
delay(wait);
wait = (max(wait - sqrt(b), 6)) / 1.8;
b = wait;
}
strip[0].clear();
strip[0].show();
strip[1].clear();
strip[1].show();
strip[2].clear();
strip[2].show();
strip[3].clear();
strip[3].show();
delay(freq);
}
void raindrops(int wait) {
uint32_t color = strip[0].Color(150, 150, 255);
int brightness = 20;
strip[0].setBrightness(brightness);
strip[1].setBrightness(brightness);
strip[2].setBrightness(brightness);
strip[3].setBrightness(brightness);
int qty = LED_COUNT / 5;
int array0[qty];
int array1[qty];
int array2[qty];
int array3[qty];
for (int i = 0; i < qty; i++) {
if (mode != 2) {
Serial.println("trying2");
break;
}
//ButtonControl();
int a = random(0, LED_COUNT);
int b = random(0, LED_COUNT);
int c = random(0, LED_COUNT);
int d = random(0, LED_COUNT);
strip[0].setPixelColor(a, color);
strip[0].show();
strip[0].setPixelColor(array0[i], (255, 0, 0));
strip[0].show();
array0[i] = a;
strip[1].setPixelColor(b, color);
strip[1].show();
strip[1].setPixelColor(array1[i], (255, 0, 0));
strip[1].show();
array1[i] = b;
strip[2].setPixelColor(c, color);
strip[2].show();
strip[2].setPixelColor(array2[i], (255, 0, 0));
strip[2].show();
array2[i] = c;
strip[3].setPixelColor(d, color);
strip[3].show();
strip[3].setPixelColor(array3[i], (255, 0, 0));
strip[3].show();
array3[i] = d;
delay(wait);
}
}
void lightning(int freq) {
int lightmode = random(0, 4);
int brightness = 255; //0-255 brightness scale
uint32_t color = strip[0].Color(brightness, brightness, brightness);
//ButtonControl();
switch (lightmode) {
case 0:
for (int i = LED_COUNT; i >= 0; i--) {
//ButtonControl();
strip[0].setPixelColor(i, color);
strip[0].show();
}
strip[0].clear();
strip[0].show();
break;
case 1:
for (int i = LED_COUNT; i >= 0; i--) {
//ButtonControl();
strip[1].setPixelColor(i, color);
strip[1].show();
}
strip[1].clear();
strip[1].show();
break;
case 2:
for (int i = LED_COUNT; i >= 0; i--) {
//ButtonControl();
strip[2].setPixelColor(i, color);
strip[2].show();
}
strip[2].clear();
strip[2].show();
break;
case 3:
for (int i = LED_COUNT; i >= 0; i--) {
//ButtonControl();
strip[3].setPixelColor(i, color);
strip[3].show();
}
strip[3].clear();
strip[3].show();
break;
}
delay(freq);
}
void snake(int speed) {
//go down one strip then up the next
snakedirection(speed, 5, 0, 0);
snakedirection(speed, 7, 1, 1);
snakedirection(speed, 9, 0, 2);
snakedirection(speed, 11, 1, 3);
snakedirection(speed, 13, 0, 2);
snakedirection(speed, 15, 1, 1);
snakedirection(speed, 17, 0, 0);
snakedirection(speed, 19, 1, 1);
snakedirection(speed, 21, 0, 2);
snakedirection(speed, 23, 1, 3);
snakedirection(speed, 25, 0, 2);
snakedirection(speed, 27, 1, 1);
snakedirection(speed, 25, 0, 0);
snakedirection(speed, 23, 1, 1);
snakedirection(speed, 21, 0, 2);
snakedirection(speed, 19, 1, 3);
snakedirection(speed, 17, 0, 2);
snakedirection(speed, 15, 1, 1);
snakedirection(speed, 13, 0, 0);
snakedirection(speed, 11, 1, 1);
snakedirection(speed, 9, 0, 2);
snakedirection(speed, 7, 1, 3);
snakedirection(speed, 5, 0, 2);
snakedirection(speed, 3, 1, 1);
}
void snakedirection(int wait, int qty, int stripdirection, int stripno) {
uint32_t color = strip[stripno].Color(0, 255, 20);
//int qty = 10;
switch (stripdirection) {
case 0:
for (int i = LED_COUNT; i >= 0; i--) {
//ButtonControl();
int a = i + 1;
strip[stripno].setPixelColor(i, color);
strip[stripno].show();
strip[stripno].setPixelColor(i + qty, (255, 0, 0));
strip[stripno].show();
if (i == 0) {
for (int a = qty; a >= -1; a--) {
strip[stripno].setPixelColor(a, (255, 0, 0));
strip[stripno].show();
delay(wait);
}
}
delay(wait);
}
break;
case 1:
for (int i = 0; i <= LED_COUNT; i++) {
//ButtonControl();
int a = i + 1;
strip[stripno].setPixelColor(i, color);
strip[stripno].show();
strip[stripno].setPixelColor(i - qty, (255, 0, 0));
strip[stripno].show();
if (i == LED_COUNT) {
for (int a = LED_COUNT - qty; a <= LED_COUNT + 1; a++) {
strip[stripno].setPixelColor(a, (255, 0, 0));
strip[stripno].show();
delay(wait);
}
}
delay(wait);
}
break;
}
}
void breathebright(int wait) {
int color = 45000;
for (long fade = 100; fade < 255; fade += 1) {
for (int i = 0; i < 100; i++) {
//ButtonControl();
strip[0].setPixelColor(i, strip[0].gamma32(strip[0].ColorHSV(color, 150, fade)));
strip[1].setPixelColor(i, strip[1].gamma32(strip[1].ColorHSV(color, 150, fade)));
strip[2].setPixelColor(i, strip[2].gamma32(strip[2].ColorHSV(color, 150, fade)));
strip[3].setPixelColor(i, strip[3].gamma32(strip[3].ColorHSV(color, 150, fade)));
}
strip[0].show();
strip[1].show();
strip[2].show();
strip[3].show();
delay(wait);
}
for (long fade = 255; fade > 100; fade -= 1) {
for (int i = 100; i >= 0; i--) {
//ButtonControl();
strip[0].setPixelColor(i, strip[0].gamma32(strip[0].ColorHSV(color, 150, fade)));
strip[1].setPixelColor(i, strip[1].gamma32(strip[1].ColorHSV(color, 150, fade)));
strip[2].setPixelColor(i, strip[2].gamma32(strip[2].ColorHSV(color, 150, fade)));
strip[3].setPixelColor(i, strip[3].gamma32(strip[3].ColorHSV(color, 150, fade)));
}
strip[0].show();
strip[1].show();
strip[2].show();
strip[3].show();
delay(wait);
}
}
void breathesat(int wait) {
int color = 45000;
for (long fade = 0; fade < 255; fade += 2) {
for (int i = 0; i < 100; i++) {
//ButtonControl();
strip[0].setPixelColor(i, strip[0].gamma32(strip[0].ColorHSV(color, fade, 150)));
strip[1].setPixelColor(i, strip[1].gamma32(strip[1].ColorHSV(color, fade, 150)));
strip[2].setPixelColor(i, strip[2].gamma32(strip[2].ColorHSV(color, fade, 150)));
strip[3].setPixelColor(i, strip[3].gamma32(strip[3].ColorHSV(color, fade, 150)));
}
strip[0].show();
strip[1].show();
strip[2].show();
strip[3].show();
delay(wait);
}
for (long fade = 255; fade > 0; fade -= 2) {
for (int i = 100; i >= 0; i--) {
//ButtonControl();
strip[0].setPixelColor(i, strip[0].gamma32(strip[0].ColorHSV(color, fade, 150)));
strip[1].setPixelColor(i, strip[1].gamma32(strip[1].ColorHSV(color, fade, 150)));
strip[2].setPixelColor(i, strip[2].gamma32(strip[2].ColorHSV(color, fade, 150)));
strip[3].setPixelColor(i, strip[3].gamma32(strip[3].ColorHSV(color, fade, 150)));
}
strip[0].show();
strip[1].show();
strip[2].show();
strip[3].show();
delay(wait);
}
}
void theaterChaseRainbow(int wait) {
int firstPixelHue = 0;
for (int a = 0; a < 30; a++) {
for (int b = 0; b < 4; b++) {
strip[0].clear();
strip[1].clear();
strip[2].clear();
strip[3].clear();
for (int c = b; c < LED_COUNT; c += 4) {
//ButtonControl();
int hue = firstPixelHue + c * 65536L / LED_COUNT;
uint32_t color = strip[0].gamma32(strip[0].ColorHSV(hue));
strip[0].setPixelColor(c, color);
strip[1].setPixelColor(c, color);
strip[2].setPixelColor(c, color);
strip[3].setPixelColor(c, color);
}
strip[0].show();
strip[1].show();
strip[2].show();
strip[3].show();
delay(wait);
firstPixelHue += 65536 / 90;
}
}
}
void theaterChase(uint32_t color, int wait) {
for (int a = 0; a < 10; a++) {
for (int b = 0; b < 3; b++) {
strip[0].clear();
strip[1].clear();
strip[2].clear();
strip[3].clear();
for (int c = b; c < LED_COUNT; c += 3) {
//ButtonControl();
strip[0].setPixelColor(c, color);
strip[1].setPixelColor(c, color);
strip[2].setPixelColor(c, color);
strip[3].setPixelColor(c, color);
}
strip[0].show();
strip[1].show();
strip[2].show();
strip[3].show();
delay(wait);
}
}
}
void colorWipe(uint32_t color, int wait) {
for (int i = 0; i < LED_COUNT; i++) {
strip[0].setPixelColor(i, color);
strip[0].show();
strip[1].setPixelColor(i, color);
strip[1].show();
strip[2].setPixelColor(i, color);
strip[2].show();
strip[3].setPixelColor(i, color);
strip[3].show();
//ButtonControl();
delay(wait);
}
}
void rainbowrange(int wait) {
for (long firstPixelHue = 15000; firstPixelHue < 25000; firstPixelHue += 156) {
for (int i = 0; i < LED_COUNT; i++) {
//ButtonControl();
int pixelHue = firstPixelHue + (i * 25000L / LED_COUNT);
strip[0].setPixelColor(i, strip[0].gamma32(strip[0].ColorHSV(pixelHue)));
strip[1].setPixelColor(i, strip[1].gamma32(strip[1].ColorHSV(pixelHue)));
strip[2].setPixelColor(i, strip[2].gamma32(strip[2].ColorHSV(pixelHue)));
strip[3].setPixelColor(i, strip[3].gamma32(strip[3].ColorHSV(pixelHue)));
}
strip[0].show();
strip[1].show();
strip[2].show();
strip[3].show();
delay(wait);
}
for (long firstPixelHue = 25000; firstPixelHue > 15000; firstPixelHue -= 156) {
for (int i = LED_COUNT; i >= 0; i--) {
//ButtonControl();
int pixelHue = firstPixelHue + (i * 25000L / LED_COUNT);
strip[0].setPixelColor(i, strip[0].gamma32(strip[0].ColorHSV(pixelHue)));
strip[1].setPixelColor(i, strip[1].gamma32(strip[1].ColorHSV(pixelHue)));
strip[2].setPixelColor(i, strip[2].gamma32(strip[2].ColorHSV(pixelHue)));
strip[3].setPixelColor(i, strip[3].gamma32(strip[3].ColorHSV(pixelHue)));
}
strip[0].show();
strip[1].show();
strip[2].show();
strip[3].show();
delay(wait);
}
}
void ButtonControl() {
unsigned long volatile currentMillis = millis();
if (digitalRead(Button) == LOW && ButtonActive == 0) {
ButtonActive = 1;
ButtonPreviousMillis = currentMillis;
ButtonShort = 1;
ButtonLong = 0;
}
//else{return;}
if (currentMillis - ButtonPreviousMillis >= ButtonHoldInterval && ButtonActive == 1) {
ButtonPreviousMillis = currentMillis;
ButtonLong = 1;
ButtonShort = 0;
}
while (digitalRead(Button) == LOW) {
//Serial.println("Holding");
delay(500);
if (digitalRead(Button) == HIGH) {
Serial.println("Released");
//break;
}
//return;
}
if (digitalRead(Button) == HIGH) {
ButtonActive = 0;
if (ButtonShort == 1) {
//Short button press
Serial.println("short press");
ButtonLong = 0;
ButtonShort = 0;
if (++mode > 10) mode = 0;
Serial.println(mode);
strip[0].clear();
strip[1].clear();
strip[2].clear();
strip[3].clear();
strip[0].show();
strip[1].show();
strip[2].show();
strip[3].show();
delay(1000);
//EEPROM.update(address, mode);
loop();
//Serial.println(mode);
//delay(200);
} else if (ButtonLong == 1) {
//Long button press
Serial.println("Long press");
}
}
}