#include <Adafruit_NeoPixel.h>
#define LED_PIN 5
#define LED_COUNT 24
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
int currentAnimation = 1;
unsigned long lastSwitch = 0;
const unsigned long animationDuration = 5000;
unsigned long lastUpdate = 0;
void setup() {
strip.begin();
strip.show();
}
void loop() {
if (millis() - lastSwitch > animationDuration) {
currentAnimation++;
if (currentAnimation > 16) currentAnimation = 1;
strip.clear();
strip.show();
lastSwitch = millis();
}
if (millis() - lastUpdate > 50) {
lastUpdate = millis();
runAnimation(currentAnimation);
}
}
void runAnimation(int id) {
switch (id) {
case 1: colorWipe(strip.Color(255, 0, 0)); break;
case 2: colorWipe(strip.Color(0, 255, 0)); break;
case 3: colorWipe(strip.Color(0, 0, 255)); break;
case 4: rainbowCycle(); break;
case 5: theaterChase(strip.Color(127, 127, 127)); break;
case 6: pulse(strip.Color(255, 0, 255)); break;
case 7: sparkle(strip.Color(255, 255, 0)); break;
case 8: bounce(strip.Color(0, 255, 255)); break;
case 9: scanner(strip.Color(255, 0, 0)); break;
case 10: fireEffect(); break;
case 11: rainbowWipe(); break;
case 12: comet(strip.Color(255, 255, 255)); break;
case 13: twinkle(strip.Color(0, 255, 255)); break;
case 14: gradientRotate(); break;
case 15: lavaLamp(); break;
case 16: heartbeat(strip.Color(255, 0, 0)); break;
}
}
// --- Helper Function ---
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if (WheelPos < 85) return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
if (WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
// --- Animation Functions ---
void colorWipe(uint32_t color) {
static int i = 0;
strip.setPixelColor(i, color);
strip.show();
i = (i + 1) % LED_COUNT;
}
void rainbowCycle() {
static uint16_t j = 0;
for (int i = 0; i < LED_COUNT; i++) {
strip.setPixelColor(i, Wheel((i * 256 / LED_COUNT + j) & 255));
}
strip.show();
j++;
}
void theaterChase(uint32_t color) {
static int q = 0;
for (int i = 0; i < LED_COUNT; i++) {
strip.setPixelColor(i, ((i + q) % 3 == 0) ? color : 0);
}
strip.show();
q = (q + 1) % 3;
}
void pulse(uint32_t color) {
static int brightness = 0;
static int fadeAmount = 5;
for (int i = 0; i < LED_COUNT; i++) {
strip.setPixelColor(i, strip.Color(
(color >> 16 & 0xFF) * brightness / 255,
(color >> 8 & 0xFF) * brightness / 255,
(color & 0xFF) * brightness / 255));
}
strip.show();
brightness += fadeAmount;
if (brightness <= 0 || brightness >= 255) fadeAmount = -fadeAmount;
}
void sparkle(uint32_t color) {
strip.clear();
strip.setPixelColor(random(LED_COUNT), color);
strip.show();
}
void bounce(uint32_t color) {
static int pos = 0;
static int dir = 1;
strip.clear();
strip.setPixelColor(pos, color);
strip.show();
pos += dir;
if (pos == 0 || pos == LED_COUNT - 1) dir = -dir;
}
void scanner(uint32_t color) {
static int pos = 0;
static int dir = 1;
strip.clear();
strip.setPixelColor(pos, color);
if (pos > 0) strip.setPixelColor(pos - 1, strip.Color(64, 0, 0));
if (pos < LED_COUNT - 1) strip.setPixelColor(pos + 1, strip.Color(64, 0, 0));
strip.show();
pos += dir;
if (pos == 0 || pos == LED_COUNT - 1) dir = -dir;
}
void fireEffect() {
for (int i = 0; i < LED_COUNT; i++) {
int r = random(180, 255);
int g = random(0, 100);
strip.setPixelColor(i, strip.Color(r, g, 0));
}
strip.show();
}
void rainbowWipe() {
static int i = 0;
strip.setPixelColor(i, Wheel(i * 256 / LED_COUNT));
strip.show();
i = (i + 1) % LED_COUNT;
}
void comet(uint32_t color) {
static int head = 0;
strip.clear();
for (int i = 0; i < 5; i++) {
int pos = head - i;
if (pos >= 0 && pos < LED_COUNT) {
uint8_t fade = 255 - i * 50;
strip.setPixelColor(pos, strip.Color(
(color >> 16 & 0xFF) * fade / 255,
(color >> 8 & 0xFF) * fade / 255,
(color & 0xFF) * fade / 255));
}
}
strip.show();
head = (head + 1) % LED_COUNT;
}
void twinkle(uint32_t color) {
for (int i = 0; i < LED_COUNT; i++) {
strip.setPixelColor(i, (random(10) < 2) ? color : 0);
}
strip.show();
}
void gradientRotate() {
static int offset = 0;
for (int i = 0; i < LED_COUNT; i++) {
int pos = (i + offset) % LED_COUNT;
strip.setPixelColor(i, Wheel(pos * 256 / LED_COUNT));
}
strip.show();
offset++;
}
void lavaLamp() {
for (int i = 0; i < LED_COUNT; i++) {
int r = 128 + sin(i + millis() / 100.0) * 127;
int g = 64 + cos(i + millis() / 150.0) * 64;
strip.setPixelColor(i, strip.Color(r, g, 0));
}
strip.show();
}
void heartbeat(uint32_t color) {
static int phase = 0;
int brightness = (phase < 10) ? phase * 25 : (20 - phase) * 25;
for (int i = 0; i < LED_COUNT; i++) {
strip.setPixelColor(i, strip.Color(
(color >> 16 & 0xFF) * brightness / 255,
(color >> 8 & 0xFF) * brightness / 255,
(color & 0xFF) * brightness / 255));
}
strip.show();
phase = (phase + 1) % 20;
}