// https://forum.arduino.cc/t/leds-with-multiple-sequences/1433129/12
#include <Adafruit_NeoPixel.h>
#define BUTTON 3 // button pin
#define PIXPIN 2 // WS2812B data pin
#define PIXNUM 60 // number of pixels
Adafruit_NeoPixel pixel(PIXNUM, PIXPIN, NEO_GRB + NEO_KHZ800); // define object
int gap = 50, wait = 2000; // time between pixels, time between sequences
unsigned long timer, timeout = 200; // wait-for-button timer
void setup() {
pixel.begin(); // start NeoPixel object
pinMode(BUTTON, INPUT_PULLUP); // configure button for LOW = PRESSED
pinMode(LED_BUILTIN, OUTPUT); // configure LED_BUILTIN for output
}
void loop() {
while (digitalRead(BUTTON)) { // wait for button press
signal(); // flash LED_BUILTIN
};
// call the sequencer
sequence(0, 9, gap, wait, 255, 0, 0); // red
sequence(10, 19, gap, wait, 0, 255, 0); // grn
sequence(20, 29, gap, wait, 0, 0, 255); // blu
sequence(30, 39, gap, wait, 255, 0, 255); // mag
sequence(40, 49, gap, wait, 255, 255, 0); // yel
sequence(50, 59, gap, wait, 0, 255, 255); // cyn
}
void sequence(int start, int stop, int gap, int wait, int r, int g, int b) {
for (int i = start; i < stop + 1; i++) {
blackout(); // clear pixel buffer
pixel.setPixelColor(i, pixel.Color(r, g, b)); // color the pixel
pixel.show(); // display pixel buffer
delay(gap); // wait for next pixel
}
delay(wait); // wait for next sequence
blackout(); // clear last pixel (all pixels)
}
void blackout() {
for (int i = 0; i < PIXNUM; i++) { // count all pixels
pixel.setPixelColor(i, pixel.Color(0, 0, 0)); // color BLK
}
pixel.show(); // display buffer
}
void signal() { // flash LED_BUILTIN while waiting for button
if (millis() - timer > timeout) {
timer = millis(); // reset timer
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}
}PRESS