#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define LED_PIN 6 // Arduino pin connected to NeoPixel data line
#define NUM_PIXELS 60 // Number of pixels in your strip
Adafruit_NeoPixel pixels(NUM_PIXELS, LED_PIN, NEO_GRB + NEO_KHZ800);
// Function to generate a random color
uint32_t randomColor() {
return pixels.Color(random(0, 256), random(0, 256), random(0, 256));
}
// Function to dim a color (factor between 0 and 1)
uint32_t dimColor(uint32_t color, float factor) {
uint8_t r = (uint8_t)(((color >> 16) & 0xFF) * factor); // Red component
uint8_t g = (uint8_t)(((color >> 8) & 0xFF) * factor); // Green component
uint8_t b = (uint8_t)((color & 0xFF) * factor); // Blue component
return pixels.Color(r, g, b);
}
// Function to launch the firework (rising pixel)
void launchFirework(uint32_t color, int targetPosition) {
for (int i = 0; i <= targetPosition; i++) {
pixels.clear();
pixels.setPixelColor(i, color);
pixels.show();
delay(random(10, 30)); // Adjust speed of launch
}
}
// Function for the burst effect
void burst(int centerPixel, int burstRadius, uint32_t color) {
for (int i = -burstRadius; i <= burstRadius; i++) {
int pixelIndex = centerPixel + i;
if (pixelIndex >= 0 && pixelIndex < NUM_PIXELS) {
pixels.setPixelColor(pixelIndex, color);
}
}
pixels.show();
delay(50); // Duration of burst flash
}
// Function to run stars outwards from the blast
void runStars(int centerPixel, uint32_t color) {
int starLength = 15; // Maximum length of stars
for (int i = 1; i <= starLength; i++) {
pixels.clear(); // Clear for each frame of star movement
// Burst center again (optional, for persistent center)
int burstRadius = random(5, 15); // Smaller burst radius for center persistence
for (int j = -burstRadius; j <= burstRadius; j++) {
int pixelIndex = centerPixel + j;
if (pixelIndex >= 0 && pixelIndex < NUM_PIXELS) {
pixels.setPixelColor(pixelIndex, dimColor(color, 0.7)); // Dimmed center
}
}
// Stars to the right
for (int j = 1; j <= i; j++) {
int pixelIndexRight = centerPixel + j;
if (pixelIndexRight < NUM_PIXELS) {
pixels.setPixelColor(pixelIndexRight, dimColor(color, (float)(starLength - j +1) / starLength)); // Fade out stars
}
}
// Stars to the left
for (int j = 1; j <= i; j++) {
int pixelIndexLeft = centerPixel - j;
if (pixelIndexLeft >= 0) {
pixels.setPixelColor(pixelIndexLeft, dimColor(color, (float)(starLength - j + 1) / starLength)); // Fade out stars
}
}
pixels.show();
delay(random(20, 50)); // Adjust star speed
}
}
void fadeOut() {
for (int fadeCount = 255; fadeCount >= 0; fadeCount--) {
for (int i = 0; i < NUM_PIXELS; i++) {
uint32_t currentColor = pixels.getPixelColor(i);
pixels.setPixelColor(i, dimColor(currentColor, (float)fadeCount / 255.0));
}
pixels.show();
delay(5); // Adjust fade speed
}
pixels.clear();
pixels.show();
}
void setup() {
pixels.begin();
pixels.clear();
pixels.show();
randomSeed(analogRead(A0)); // Seed random number generator
}
void loop() {
// 1. Choose random burst position and size
int burstPosition = random(10, NUM_PIXELS - 10); // Avoid edges for burst
int burstRadius = random(10, 25); // Burst radius 10 to 25 pixels (diameter 20 to 50)
// 2. Choose random colors for firework and stars
uint32_t fireworkColor = randomColor();
uint32_t starColor = randomColor();
// 3. Launch Firework
launchFirework(fireworkColor, burstPosition);
delay(random(50, 200)); // Small pause before burst
// 4. Burst Effect
burst(burstPosition, burstRadius, fireworkColor);
delay(random(100, 300)); // Pause after burst flash
// 5. Run Stars
runStars(burstPosition, starColor);
delay(random(200, 500)); // Pause after stars
// 6. Fade Out
fadeOut();
delay(random(500, 1500)); // Delay before next firework
}