// Define pin numbers for the sunlight and nightlight LEDs
#define sunlightLED 9
#define nightlightLED 10
// Variables to store the start time, brightness levels, and phase
unsigned long startTime;
int sunBrightness = 255;
int nightBrightness = 0;
int phase = 0; // 0: Daytime, 1: Transition to Night, 2: Nighttime
void setup() {
// Set the LED pins as OUTPUT
pinMode(sunlightLED, OUTPUT);
pinMode(nightlightLED, OUTPUT);
// Initialize serial communication for debugging
Serial.begin(9600);
// Record the initial time
startTime = millis();
}
void loop() {
// Daytime to night phase
if (millis() - startTime <= 22500 && phase == 0) {
// Calculate the new brightness value based on elapsed time
sunBrightness = map(millis() - startTime, 0, 22500, 255, -1);
// Set the sunlight LED brightness
analogWrite(sunlightLED, sunBrightness);
// Display sunlight brightness for debugging
Serial.print("Sunlight Brightness: ");
Serial.println(sunBrightness);
} else if (phase == 0) {
// Transition to the next phase
phase = 1;
}
if (millis() - startTime >= 21500 && phase == 0) {
// Calculate the new brightness value based on elapsed time
nightBrightness = map(millis() - startTime, 21500, 22500, 0, 64);
// Set the nightlight LED brightness
analogWrite(nightlightLED, nightBrightness);
// Display nightlight brightness for debugging
Serial.print("Nightlight Brightness: ");
Serial.println(nightBrightness);
}
// Delay during the night
if(phase == 1) {
Serial.println("Waiting during night");
delay(15000);
phase = 2; // Set to Night phase
startTime = millis(); // Record the start time for the new phase
}
// Nighttime to day phase
if (millis() - startTime <= 22500 && phase == 2) {
// Calculate the new brightness value based on elapsed time
sunBrightness = map(millis() - startTime, 0, 22500, 0, 255);
// Set the sunlight LED brightness
analogWrite(sunlightLED, sunBrightness);
// Display sunlight brightness for debugging
Serial.print("Sunlight Brightness: ");
Serial.println(sunBrightness);
} else if (phase == 2) {
// Transition to the next phase
phase = 0;
startTime = millis(); // Record the start time for the new phase
}
if (millis() - startTime <= 1000 && phase == 2) {
// Calculate the new brightness value based on elapsed time
nightBrightness = map(millis() - startTime, 0, 1000, 64, -1);
// Set the nightlight LED brightness
analogWrite(nightlightLED, nightBrightness);
// Display nightlight brightness for debugging
Serial.print("Nightlight Brightness: ");
Serial.println(nightBrightness);
}
}