// https://forum.arduino.cc/t/independent-neopixel-with-independent-buttons-single-arduino-nano/1430695/15
#include <Adafruit_NeoPixel.h>
int BUTTON_PIN[] = {2, 3, 4, 5, 6};
int PIXEL_PIN[] = {8, 9, 10, 11, 12};
#define PIXEL_COUNT 1 // Number of NeoPixels
Adafruit_NeoPixel strip[] = { // array of five objects
Adafruit_NeoPixel (PIXEL_COUNT, PIXEL_PIN[0], NEO_GRB + NEO_KHZ800),
Adafruit_NeoPixel (PIXEL_COUNT, PIXEL_PIN[1], NEO_GRB + NEO_KHZ800),
Adafruit_NeoPixel (PIXEL_COUNT, PIXEL_PIN[2], NEO_GRB + NEO_KHZ800),
Adafruit_NeoPixel (PIXEL_COUNT, PIXEL_PIN[3], NEO_GRB + NEO_KHZ800),
Adafruit_NeoPixel (PIXEL_COUNT, PIXEL_PIN[4], NEO_GRB + NEO_KHZ800),
};
bool oldState[] = {HIGH, HIGH, HIGH, HIGH, HIGH}, newState[5];
int mode[] = {0, 0, 0, 0, 0};
long firstPixelHue;
unsigned long timer[5], timeout[] = {10, 10, 10, 10, 10};
void setup() {
Serial.begin(115200);
for (byte i = 0; i < 5; i++) {
pinMode(BUTTON_PIN[i], INPUT_PULLUP);
strip[i].begin();
strip[i].show();
}
}
void loop() {
// Get current button state.
for (byte i = 0; i < 5; i++) {
newState[i] = digitalRead(BUTTON_PIN[i]);
// Check if state changed from high to low (button press).
if ((newState[i] == LOW) && (oldState[i] == HIGH)) {
delay(20); // debounce
// Check if button is still low after debounce.
newState[i] = digitalRead(BUTTON_PIN[i]);
if (newState[i] == LOW) { // Yes, still low
if (++mode[i] > 10)
mode[i] = 0; // Advance to next mode, wrap around after #8
switch (mode[i]) { // Start the new animation...
case 0:
colorWipe(strip[i].Color(0, 0, 0), 50, i); // Black/off
break;
case 1:
colorWipe(strip[i].Color(255, 0, 0), 50, i); // Red
break;
case 2:
colorWipe(strip[i].Color(0, 255, 0), 50, i); // Green
break;
case 3:
colorWipe(strip[i].Color(0, 0, 255), 50, i); // Blue
break;
case 4:
colorWipe(strip[i].Color(255, 255, 255), 50, i); // White
break;
case 5:
colorWipe(strip[i].Color(255, 255, 0), 50, i); // Yellow
break;
case 6:
colorWipe(strip[i].Color(255, 25, 125), 50, i); // Hot Pink
break;
case 7:
colorWipe(strip[i].Color(255, 150, 50), 50, i); // Warm White
break;
case 8:
colorWipe(strip[i].Color(255, 65, 255), 50, i); // Purple
break;
case 9:
colorWipe(strip[i].Color(50, 200, 255), 50, i); // Light Blue
break;
case 10:
rainbow(10, i);
break;
default: break;
}
}
}
// Set the last-read button state to the old state.
oldState[i] = newState[i];
}
}
void colorWipe(uint32_t color, int wait, int s) {
for (int i = 0; i < strip[s].numPixels(); i++) { // For each pixel in strip...
strip[s].setPixelColor(i, color); // Set pixel's color (in RAM)
strip[s].show(); // Update strip to match
}
}
// Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
void rainbow(int wait, int s) {
for (long firstPixelHue = 0; firstPixelHue < 3 * 65536; firstPixelHue += 256) {
strip[s].setPixelColor(0, strip[s].gamma32(strip[s].ColorHSV(firstPixelHue)));
strip[s].show(); // Update strip with new contents
delay(wait); // Pause for a moment
}
// This is the timer that would make each LED color-fade, but for the one-shot call
// if (millis() - timer[s] > timeout[s]) {
// timer[s] = millis();
// if ((firstPixelHue += 256) > (3 * 65536L))
// firstPixelHue = 0;
// strip[s].setPixelColor(0, strip[s].gamma32(strip[s].ColorHSV(firstPixelHue)));
// strip[s].show(); // Update strip with new contents
// }
}