Last active
April 17, 2026 16:22
-
-
Save Steven24K/7f356bf95c377a02dcc8af36837b95e0 to your computer and use it in GitHub Desktop.
Lots of animation for a WS2812B LED strip powered by Arduino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <FastLED.h> | |
| #define NUM_LEDS 30 | |
| #define LED_PIN 2 | |
| #define YELLOW_BTN_PIN 3 | |
| #define BLUE_BTN_PIN 4 | |
| const int LAST_MODE = 18; | |
| int current_mode = 0; | |
| CRGB leds[NUM_LEDS]; | |
| unsigned long lastDebounceTime = 0; | |
| const int debounceDelay = 200; | |
| void setup() { | |
| pinMode(YELLOW_BTN_PIN, INPUT_PULLUP); | |
| pinMode(BLUE_BTN_PIN, INPUT_PULLUP); | |
| FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip); | |
| FastLED.setBrightness(150); | |
| } | |
| void loop() { | |
| checkButtons(); | |
| switch (current_mode) { | |
| case 0: test(); break; | |
| case 1: warm_white(); break; | |
| case 2: warm_white_odd(); break; | |
| case 3: cold_white(); break; | |
| case 4: cold_white_odd(); break; | |
| case 5: circle_spinner(); break; | |
| case 6: breathe_warm(); break; | |
| case 7: pride(); break; | |
| case 8: standby(); break; | |
| case 9: heart_beat(); break; | |
| case 10: slow_rainbow(); break; | |
| case 11: meteor_rainbow(); break; | |
| case 12: double_meteor(); break; | |
| case 13: fill_hue(); break; | |
| case 14: solid_alice_blue(); break; | |
| case 15: horns_fade(25); break; | |
| case 16: confetti(); break; | |
| case 17: sinelon(); break; | |
| case 18: juggle(); break; | |
| } | |
| } | |
| void checkButtons() { | |
| if (millis() - lastDebounceTime > debounceDelay) { | |
| if (digitalRead(YELLOW_BTN_PIN) == LOW) { | |
| current_mode = (current_mode >= LAST_MODE) ? 0 : current_mode + 1; | |
| lastDebounceTime = millis(); | |
| } | |
| if (digitalRead(BLUE_BTN_PIN) == LOW) { | |
| current_mode = (current_mode <= 0) ? LAST_MODE : current_mode - 1; | |
| lastDebounceTime = millis(); | |
| } | |
| } | |
| } | |
| void warm_white() { | |
| fill_solid(leds, NUM_LEDS, CRGB::Tan); | |
| FastLED.show(); | |
| } | |
| void warm_white_odd() { | |
| FastLED.clear(); | |
| for (int i = 0; i < NUM_LEDS; i += 2) { | |
| leds[i] = CRGB::Tan; | |
| } | |
| FastLED.show(); | |
| } | |
| void cold_white() { | |
| fill_solid(leds, NUM_LEDS, CRGB(10, 50, 255)); // Helder koud wit/blauwachtig | |
| FastLED.show(); | |
| } | |
| void cold_white_odd() { | |
| FastLED.clear(); | |
| for (int i = 0; i < NUM_LEDS; i += 2) { | |
| leds[i] = CRGB(10, 50, 255); | |
| } | |
| FastLED.show(); | |
| } | |
| void circle_spinner() { | |
| static uint8_t hue = 0; | |
| static int pos = 0; | |
| static uint32_t lastUpdate = 0; | |
| uint32_t now = millis(); | |
| if (now - lastUpdate < 30) return; | |
| lastUpdate = now; | |
| fadeToBlackBy(leds, NUM_LEDS, 40); | |
| leds[pos] = CHSV(hue++, 255, 255); | |
| pos = (pos + 1) % NUM_LEDS; | |
| FastLED.show(); | |
| } | |
| void breathe_warm() { | |
| float intensity = beatsin8(15, 20, 255); | |
| fill_solid(leds, NUM_LEDS, CRGB(255, 147, 41)); | |
| FastLED.setBrightness(intensity); | |
| FastLED.show(); | |
| } | |
| void standby() { | |
| static uint8_t hue = 0; | |
| static int currentLED = 0; | |
| static unsigned long lastMove = 0; | |
| if (millis() - lastMove > 100) { | |
| lastMove = millis(); | |
| FastLED.clear(); | |
| leds[currentLED] = CHSV(hue, 255, 255); | |
| hue++; | |
| currentLED = (currentLED + 1) % NUM_LEDS; | |
| FastLED.show(); | |
| } | |
| } | |
| void heart_beat() { | |
| static uint8_t phase = 0; | |
| static unsigned long lastMove = 0; | |
| static uint8_t hue = 0; | |
| unsigned long interval; | |
| if (phase == 0) interval = 80; | |
| else if (phase == 1) interval = 200; | |
| else interval = 500; | |
| if (millis() - lastMove > interval) { | |
| lastMove = millis(); | |
| FastLED.clear(); | |
| if (phase < 2) { | |
| for (int i = phase; i < NUM_LEDS; i += 2) { | |
| leds[i] = CHSV(hue, 255, 255); | |
| } | |
| phase++; | |
| if (phase == 2) hue += 15; | |
| } else { | |
| phase = 0; | |
| } | |
| FastLED.show(); | |
| } | |
| } | |
| void slow_rainbow() { | |
| static uint8_t hue = 0; | |
| static unsigned long lastUpdate = 0; | |
| if (millis() - lastUpdate > 50) { | |
| lastUpdate = millis(); | |
| fill_rainbow(leds, NUM_LEDS, hue, 8); | |
| hue++; | |
| FastLED.show(); | |
| } | |
| } | |
| void test() { | |
| static uint8_t colorIndex = 0; | |
| static unsigned long lastChange = 0; | |
| FastLED.setBrightness(150); | |
| if (millis() - lastChange > 800) { | |
| colorIndex++; | |
| lastChange = millis(); | |
| } | |
| if (colorIndex % 3 == 0) fill_solid(leds, NUM_LEDS, CRGB::Red); | |
| else if (colorIndex % 3 == 1) fill_solid(leds, NUM_LEDS, CRGB::Green); | |
| else fill_solid(leds, NUM_LEDS, CRGB::Blue); | |
| FastLED.show(); | |
| } | |
| void pride() { | |
| static uint16_t sPseudotime = 0; | |
| static uint16_t sLastMillis = 0; | |
| static uint16_t sHue16 = 0; | |
| FastLED.setBrightness(150); | |
| uint8_t sat8 = beatsin88(87, 220, 250); | |
| uint8_t brightdepth = beatsin88(341, 96, 224); | |
| uint16_t brightnessthetainc16 = beatsin88(203, (25 * 256), (40 * 256)); | |
| uint8_t msmultiplier = beatsin88(147, 23, 60); | |
| uint16_t hue16 = sHue16; | |
| uint16_t hueinc16 = beatsin88(113, 1, 3000); | |
| uint16_t ms = millis(); | |
| uint16_t deltams = ms - sLastMillis; | |
| sLastMillis = ms; | |
| sPseudotime += deltams * msmultiplier; | |
| sHue16 += deltams * beatsin88(400, 5, 9); | |
| uint16_t brightnesstheta16 = sPseudotime; | |
| for (uint16_t i = 0; i < NUM_LEDS; i++) { | |
| hue16 += hueinc16; | |
| uint8_t hue8 = hue16 / 256; | |
| brightnesstheta16 += brightnessthetainc16; | |
| uint16_t b16 = sin16(brightnesstheta16) + 32768; | |
| uint16_t bri16 = (uint32_t)((uint32_t)b16 * (uint32_t)b16) / 65536; | |
| uint8_t bri8 = (uint32_t)(((uint32_t)bri16) * brightdepth) / 65536; | |
| bri8 += (255 - brightdepth); | |
| CRGB newcolor = CHSV(hue8, sat8, bri8); | |
| nblend(leds[(NUM_LEDS - 1) - i], newcolor, 64); | |
| } | |
| FastLED.show(); | |
| } | |
| void meteor_rainbow() { | |
| static uint8_t hue = 0; | |
| static int pos = 0; | |
| static unsigned long lastUpdate = 0; | |
| if (millis() - lastUpdate > 40) { | |
| lastUpdate = millis(); | |
| fadeToBlackBy(leds, NUM_LEDS, 60); | |
| leds[pos] = CHSV(hue++, 255, 255); | |
| pos = (pos + 1) % NUM_LEDS; | |
| FastLED.show(); | |
| } | |
| } | |
| void double_meteor() { | |
| static uint8_t hue = 0; | |
| static int pos = 0; | |
| static unsigned long lastUpdate = 0; | |
| if (millis() - lastUpdate > 50) { | |
| lastUpdate = millis(); | |
| fadeToBlackBy(leds, NUM_LEDS, 50); | |
| leds[pos] = CHSV(hue, 255, 255); | |
| leds[(NUM_LEDS - 1) - pos] = CHSV(hue + 128, 255, 255); | |
| hue++; | |
| pos = (pos + 1) % NUM_LEDS; | |
| FastLED.show(); | |
| } | |
| } | |
| void fill_hue() { | |
| static unsigned long lastUpdate = 0; | |
| static uint8_t hue = 0; | |
| static int pos = 0; | |
| if (millis() - lastUpdate > 80) { | |
| lastUpdate = millis(); | |
| leds[pos] = CHSV(hue, 255, 255); | |
| hue++; | |
| pos = (pos + 1) % NUM_LEDS; | |
| FastLED.show(); | |
| } | |
| } | |
| void solid_alice_blue() { | |
| FastLED.clear(); | |
| fill_solid(leds, NUM_LEDS, CHSV(148, 15, 255)); | |
| FastLED.show(); | |
| } | |
| void horns_fade(uint8_t speed) { | |
| static uint8_t state = 0; | |
| static int i = 100; | |
| static uint32_t lastUpdate = 0; | |
| uint32_t now = millis(); | |
| if (now - lastUpdate < speed) return; | |
| lastUpdate = now; | |
| switch (state) { | |
| case 0: // Clear and set base LEDs | |
| FastLED.clear(); | |
| for (int j = 0; j < 14; j++) { | |
| leds[j] = CRGB(10, 10, 10); | |
| } | |
| FastLED.show(); | |
| i = 100; | |
| state = 1; | |
| break; | |
| case 1: // Fade up | |
| if (i < 255) { | |
| uint8_t brightness = dim8_raw(i); | |
| for (uint8_t j = 14; j < NUM_LEDS; j++) { | |
| leds[j] = CRGB(brightness, brightness, brightness); | |
| } | |
| FastLED.show(); | |
| i++; | |
| } else { | |
| i = 255; | |
| state = 2; | |
| } | |
| break; | |
| case 2: // Fade down | |
| if (i > 100) { | |
| uint8_t brightness = dim8_raw(i); | |
| for (uint8_t j = 14; j < NUM_LEDS; j++) { | |
| leds[j] = CRGB(brightness, brightness, brightness); | |
| } | |
| FastLED.show(); | |
| i--; | |
| } else { | |
| state = 0; // Reset to beginning, or set to an idle state | |
| } | |
| break; | |
| } | |
| } | |
| void confetti() { | |
| static unsigned long lastUpdate = 0; | |
| static uint8_t hue = 0; | |
| static int pos = 0; | |
| if (millis() - lastUpdate > 80) { | |
| lastUpdate = millis(); | |
| fadeToBlackBy(leds, NUM_LEDS, 10); | |
| int pos = random16(NUM_LEDS); | |
| leds[pos] += CHSV(hue + random8(64), 200, 255); | |
| hue++; | |
| pos = (pos + 1) % NUM_LEDS; | |
| FastLED.show(); | |
| } | |
| } | |
| void sinelon() { | |
| static unsigned long lastUpdate = 0; | |
| static uint8_t hue = 0; | |
| static int pos = 0; | |
| if (millis() - lastUpdate > 80) { | |
| lastUpdate = millis(); | |
| fadeToBlackBy(leds, NUM_LEDS, 20); | |
| int pos = beatsin16(13, 0, NUM_LEDS - 1); | |
| leds[pos] += CHSV(hue, 255, 192); | |
| hue++; | |
| pos = (pos + 1) % NUM_LEDS; | |
| FastLED.show(); | |
| } | |
| } | |
| void juggle() { | |
| static unsigned long lastUpdate = 0; | |
| static int pos = 0; | |
| if (millis() - lastUpdate > 80) { | |
| lastUpdate = millis(); | |
| // eight colored dots, weaving in and out of sync with each other | |
| fadeToBlackBy(leds, NUM_LEDS, 20); | |
| byte dothue = 0; | |
| for (int i = 0; i < 8; i++) { | |
| leds[beatsin16(i + 7, 0, NUM_LEDS - 1)] |= CHSV(dothue, 200, 255); | |
| dothue += 32; | |
| } | |
| pos = (pos + 1) % NUM_LEDS; | |
| FastLED.show(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment