#include <Adafruit_NeoPixel.h>
#define PIN 2 // Neopixel data pin
#define NUMPIXELS 256 // Total number of pixels in the matrix
#define BUTTON_PIN 9 // Pin for the button
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
// Function declarations
void rainbowCycle(uint8_t wait);
void colorWipe(uint32_t color, int wait);
void comet(uint32_t color, uint8_t tail, uint16_t delay_ms);
// Current pattern index
int currentPattern = 0;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP); // Set button pin as input with internal pull-up resistor
pixels.begin(); // Initialize Neopixel matrix
}
void loop() {
if (digitalRead(BUTTON_PIN) == LOW) {
// Button is pressed, switch to the next pattern
currentPattern = (currentPattern + 1) % 3;
delay(200); // Debouncing delay
}
// Execute the current pattern
switch (currentPattern) {
case 0:
rainbowCycle(20);
break;
case 1:
colorWipe(pixels.Color(255, 0, 0), 50); // Red
colorWipe(pixels.Color(0, 255, 0), 50); // Green
colorWipe(pixels.Color(0, 0, 255), 50); // Blue
break;
case 2:
comet(pixels.Color(255, 255, 255), 5, 50); // White, tail length 5, delay 50ms
break;
}
}
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
for (i = 0; i < pixels.numPixels(); i++) {
pixels.setPixelColor(i, Wheel(((i * 256 / pixels.numPixels()) + j) & 255));
}
pixels.show();
delay(wait);
}
}
void colorWipe(uint32_t color, int wait) {
for (int i = 0; i < pixels.numPixels(); i++) {
pixels.setPixelColor(i, color);
pixels.show();
delay(wait);
}
}
void comet(uint32_t color, uint8_t tail, uint16_t delay_ms) {
for (int i = 0; i < pixels.numPixels() + tail; i++) {
pixels.clear();
for (int j = 0; j < tail; j++) {
if (i - j >= 0 && i - j < pixels.numPixels()) {
pixels.setPixelColor(i - j, color);
}
}
pixels.show();
delay(delay_ms);
}
}
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if (WheelPos < 85) {
return pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if (WheelPos < 170) {
WheelPos -= 85;
return pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}