#include <Adafruit_NeoPixel.h>
#define LED_PIN 6 // Pin connected to the NeoPixels
#define LED_COUNT 60 // Number of NeoPixels in the ring
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
unsigned long startTime; // Start time of the animation
//unsigned long duration = 3600000; // Duration of the animation in milliseconds
unsigned long duration = 1000; // Duration of the animation in milliseconds
unsigned long animationStartTime; // Start time of the animation
unsigned long animationDuration = 5000; // Duration of the animation in milliseconds
void setup() {
strip.begin();
strip.show(); // Initialize all pixels to 'off'
//delay(500); // Delay for visual effect
startTime = millis(); // Record the start time
animationStartTime = millis(); // Record the start time
}
void loop() {
unsigned long currentTime = millis();
unsigned long elapsedTime = currentTime - startTime;
if (elapsedTime <= duration) {
// Calculate the percentage of completion
float percentage = (float)elapsedTime / duration;
// Calculate the number of LEDs to light up
int numLeds = (int)(percentage * LED_COUNT);
// Turn on the LEDs up to 'numLeds'
for (int i = 0; i < numLeds; i++) {
strip.setPixelColor(i, strip.Color(0, 0, 255)); // Set color to white (full brightness)
}
strip.show(); // Update the strip with the new colors
} else {
performAnimation(); // Rainbow Animation after the ring fills up
}
}
void performAnimation() {
unsigned long currentTime = millis();
unsigned long elapsedTime = currentTime - animationStartTime;
// Calculate the percentage of completion
float percentage = (float)elapsedTime / animationDuration;
float fadePercentage = 20; // Fade speed for the black to color transition
// Calculate the number of LEDs to light up
int numLeds = (int)(percentage * LED_COUNT);
// Set the color of each LED based on its position in the rainbow
for (int i = 0; i < LED_COUNT; i++) {
int hue = (int)(currentTime / 10 + i * 256 / LED_COUNT) % 256;
int fadeValue = (int)(255 * (percentage - fadePercentage));
if (fadeValue < 0) fadeValue = 0;
if (i < numLeds) {
strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(hue * 65536L / 256)));
} else {
strip.setPixelColor(i, strip.gamma32(strip.Color(0, 0, 0, fadeValue)));
}
}
strip.show(); // Update the strip with the new colors
if (elapsedTime >= animationDuration) {
// Reset animation and continue
animationStartTime = currentTime;
}
}