#include <FastLED.h>
#define NUM_LEDS 60
#define DATA_PIN 2
CRGB leds[NUM_LEDS];
#define NUM_SPARKS 50
float sparkPos[NUM_SPARKS];
float sparkVel[NUM_SPARKS];
float sparkCol[NUM_SPARKS];
float gravity = -0.004;
void setup() {
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
FastLED.clear();
FastLED.show();
randomSeed(analogRead(0)); // For true randomness
}
void loop() {
explode(random(20, NUM_LEDS - 20)); // Avoid edges
delay(random(500, 1500)); // Delay before next explosion
}
void explode(float centerPos) {
int n = NUM_SPARKS;
// Initialize sparks
for (int i = 0; i < n; i++) {
sparkPos[i] = centerPos;
sparkVel[i] = (random(-100, 100)) / 100.0; // -1.0 to 1.0
sparkCol[i] = abs(sparkVel[i]) * 400; // Used for color variation
}
// Animate sparks
for (int frame = 0; frame < 80; frame++) {
FastLED.clear();
for (int i = 0; i < n; i++) {
int pos = (int)sparkPos[i];
if (pos >= 0 && pos < NUM_LEDS) {
leds[pos] += CHSV((int)sparkCol[i] % 255, 255, 255 - frame * 3); // Color fades over time
}
sparkPos[i] += sparkVel[i];
sparkVel[i] += gravity;
}
FastLED.show();
delay(30);
}
}