https://forum.arduino.cc/t/easiest-way-to-slowly-ramp-led-strip/1329379
# include <Wire.h>
# include <RTClib.h>
# include <FastLED.h>
# define LED_PIN 2
# define NUM_LEDS 12
# define BRIGHTNESS 255
CRGB leds[NUM_LEDS];
RTC_DS3231 rtc;
// Time settings
int sunriseHour = 6; // Default sunrise time hour
int sunriseMinute = 0; // Default sunrise time minute
int sunsetHour = 18; // Default sunset time hour
int sunsetMinute = 0; // Default sunset time minute
unsigned long sunriseStartMillis;
unsigned long sunsetStartMillis;
bool isSunrise = false;
bool isSunset = false;
void setup() {
Serial.begin(9600);
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
rtc.begin();
// Set the RTC time if needed (uncomment the next line to set)
rtc.adjust(DateTime(2024, 12, 8, 5, 59, 0));
// Set default sunrise/sunset times if needed
// rtc.adjust(DateTime(2023, 10, 1, 12, 0, 0)); // Set time for testing
}
void loop() {
DateTime now = rtc.now();
int currentHour = now.hour();
int currentMinute = now.minute();
// Check if it's time for sunrise or sunset
if (currentHour == sunriseHour && currentMinute == sunriseMinute) {
isSunrise = true;
isSunset = false;
sunriseStartMillis = millis();
} else if (currentHour == sunsetHour && currentMinute == sunsetMinute) {
isSunset = true;
isSunrise = false;
sunsetStartMillis = millis();
}
if (isSunrise) {
performSunrise();
} else if (isSunset) {
performSunset();
}
FastLED.show();
delay(1000); // Adjust delay as needed
}
void performSunrise() {
unsigned long elapsed = millis() - sunriseStartMillis;
if (elapsed < 1800000) { // 30 minutes in milliseconds
float progress = (float)elapsed / 1800000.0;
// Brightness from 0 to 255
uint8_t brightness = progress * BRIGHTNESS;
// Color transition from orange to white
CRGB color = blend(CRGB(255, 165, 0), CRGB(255, 255, 255), brightness);
fill_solid(leds, NUM_LEDS, color);
FastLED.setBrightness(brightness);
} else {
// Maintain full brightness white after sunrise
fill_solid(leds, NUM_LEDS, CRGB(255, 255, 255));
FastLED.setBrightness(BRIGHTNESS);
}
}
void performSunset() {
unsigned long elapsed = millis() - sunsetStartMillis;
if (elapsed < 1800000) { // 30 minutes in milliseconds
float progress = (float)elapsed / 1800000.0;
// Brightness from 255 to 0
uint8_t brightness = BRIGHTNESS * (1.0 - progress);
// Color transition from white to dark orange to magenta to blue
CRGB color1 = CRGB(255, 255, 255);
CRGB color2 = CRGB(255, 165, 0);
CRGB color3 = CRGB(128, 0, 128); // Magenta
CRGB color4 = CRGB(0, 0, 255); // Blue
if (progress < 0.33) {
// First third: white to dark orange
CRGB color = blend(color1, color2, progress * 3 * 100);
fill_solid(leds, NUM_LEDS, color);
} else if (progress < 0.66) {
// Second third: dark orange to magenta
CRGB color = blend(color2, color3, (progress - 1.0 / 3.0) * 3 * 100);
fill_solid(leds, NUM_LEDS, color);
} else {
// Last third: magenta to blue
CRGB color = blend(color3, color4, (progress - 2.0 / 3.0) * 3 * 100);
fill_solid(leds, NUM_LEDS, color);
}
FastLED.setBrightness(brightness);
} else {
// Turn off LEDs after sunset
fill_solid(leds, NUM_LEDS, CRGB::Black);
FastLED.setBrightness(0);
isSunset = false;
}
}
// Function to set the time on the RTC (call this function with new time)
void setTime(int year, int month, int day, int hour, int minute, int second) {
rtc.adjust(DateTime(year, month, day, hour, minute, second));
}
// Functions to set sunrise and sunset times
void setSunriseTime(int hour, int minute) {
sunriseHour = hour;
sunriseMinute = minute;
}
void setSunsetTime(int hour, int minute) {
sunsetHour = hour;
sunsetMinute = minute;
}