// https://forum.arduino.cc/t/color-changing-neopixel-with-button-ending-with2-white-leds/987308
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
#define BUTTON_PIN 2
#define PIXEL_PIN 6 // Digital IO pin connected to the NeoPixels.
#define PIXEL_COUNT 12 // Number of NeoPixels
unsigned long interval = 1000;
unsigned long previousMillis=0;
#define INTERVAL_LENGTH1 5000
unsigned long time_1 = 0;
unsigned long time_2 = time_1;
unsigned long time_3 = time_2;
unsigned long time_4 = time_3;
// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
boolean oldState = HIGH;
int mode = 0; // Currently-active animation mode, 0-9
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
strip.begin(); // Initialize NeoPixel strip object (REQUIRED)
strip.show(); // Initialize all pixels to 'off'
}
void loop() {
unsigned long currentMillis = millis(); //grab current time
// Get current button state.
boolean newState = digitalRead(BUTTON_PIN);
// Check if state changed from high to low (button press).
if((newState == LOW) && (oldState == HIGH)) {
// Short delay to debounce button.
delay(20);
// Check if button is still low after debounce.
newState = digitalRead(BUTTON_PIN);
if(newState == LOW) { // Yes, still low
if(++mode > 2) mode = 0; // Advance to next mode, wrap around after #1
switch(mode) { // Start the new animation...
case 0:
colorWipe(strip.Color( 0, 0, 0), 0); // Black/off
delay(0) ;
break;
case 1:
colorWipe(strip.Color(255, 0, 0), 50); // Red
// check if 'interval' time has passed (1000 milliseconds)
if (currentMillis - previousMillis >= interval) { // we've waited "interval" lamount of time, so lets do something
colorWipe(strip.Color(255, 40, 0), 50); // Orange across all pixels
// save the "current" time
previousMillis = currentMillis;
}
else {
}
break;
case 2:
strip.clear();
colorWipe(strip.Color(255, 255, 0), 0); // Yellow
delay(0) ;
break;
}
}
}
// Set the last-read button state to the old state.
oldState = newState;
}
void colorWipe(uint32_t color, int wait) {
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
strip.setPixelColor(i, color); // Set pixel's color (in RAM)
strip.show(); // Update strip to match
delay(wait); // Pause for a moment
}
}