#include <FastLED.h>
#define NUM_LEDS 12 // Change this to the number of LEDs in your strip
#define DATA_PIN 2 // Change this to your data pin
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS); // Change WS2812 to your LED type if necessary
}
void loop() {
fadeToSunset(15000); // Fade over 15 seconds
}
void fadeToSunset(unsigned long duration) {
unsigned long startTime = millis();
unsigned long endTime = startTime + duration;
while (millis() < endTime) {
// Calculate the current time and progress
unsigned long currentTime = millis();
float progress = (float)(currentTime - startTime) / duration;
// Calculate the brightness level (from 255 to 0)
uint8_t brightness = (1.0 - progress) * 255;
// Calculate color transitions
CRGB color;
if (progress < 0.5) {
// Transition from white (daylight) to orange
float t = progress * 2; // Scale progress to [0, 1] for first half
color = blend(CRGB::White, CRGB::Orange, t * 255);
} else {
// Transition from orange to blue (moonlight)
float t = (progress - 0.5) * 2; // Scale progress to [0, 1] for second half
color = blend(CRGB::Orange, CRGB::Blue, t * 255);
}
// Set the color and brightness to the LEDs
fill_solid(leds, NUM_LEDS, color);
fadeToBlackBy(leds, NUM_LEDS, 255 - brightness); // Adjust brightness
FastLED.show();
delay(10); // Short delay for smooth transition
}
// Turn off all LEDs after the fade
fill_solid(leds, NUM_LEDS, CRGB::Black);
FastLED.show();
delay(1000); // Wait for a second before restarting
}