#include <FastLED.h>
// --- Configuration ---
// C H A N G E T H E S E V A L U E S T O M A T C H Y O U R S E T U P
#define NUM_LEDS 60 // How many LEDs are in your strip?
#define DATA_PIN 2 // What pin is your data line connected to?
#define LED_TYPE WS2812B // What type of LED strip do you have? (e.g., WS2811, WS2812B, NEOPIXEL)
#define COLOR_ORDER GRB // What is the color order of your strip? (GRB, RGB, etc.)
#define BRIGHTNESS 150 // Set the master brightness level (0-255)
#define FRAMES_PER_SECOND 60 // How many animation frames per second to render
// --- Global Variables ---
CRGB leds[NUM_LEDS]; // This is the array that holds the color of each LED
uint8_t gHue = 0; // The global hue value that slowly cycles through the rainbow
// --- Arduino Setup Function ---
// This function runs once when the Arduino is powered on or reset.
void setup() {
// A 2-second delay to give you time to open the Serial Monitor
// or for the power supply to stabilize.
delay(2000);
// Initialize the FastLED library.
FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS)
.setCorrection(TypicalLEDStrip); // Use typical color correction
// Set the master brightness for the whole strip.
FastLED.setBrightness(BRIGHTNESS);
// Clear all LEDs to black at the start.
FastLED.clear();
FastLED.show();
}
// --- Main Arduino Loop ---
// This function runs over and over again, creating the animation.
void loop() {
// 1. Call the confetti animation function to update the LED colors in memory.
confetti();
// 2. Send the updated LED data to the physical strip.
FastLED.show();
// 3. Wait a small amount of time to control the animation speed.
FastLED.delay(1000 / FRAMES_PER_SECOND);
// 4. Slowly cycle the base color through the rainbow.
// This makes the confetti colors change over time.
EVERY_N_MILLISECONDS(20) { gHue++; }
}
// --- Confetti Animation Function ---
// This is the core of the twinkling effect.
void confetti() {
// Step 1. Fade all existing pixels down by a small amount.
// This is what makes the sparkles fade away smoothly.
// The number '10' controls the fade speed; a higher number means a faster fade.
fadeToBlackBy(leds, NUM_LEDS, 10);
// Step 2. Pick a random LED position.
int pos = random16(NUM_LEDS);
// Step 3. Add a new, bright, randomly colored sparkle to that position.
// It uses the global hue 'gHue' and adds a random offset to it,
// creating a splash of related colors.
leds[pos] += CHSV(gHue + random8(64), 200, 255);
}