// Pin definitions
const int whiteLED = 9; // PWM pin for White LED
const int yellowLED = 6; // PWM pin for Yellow LED
const int redLED = 5; // PWM pin for Red LED
const int blueLED = 3; // PWM pin for Blue LED
void setup() {
pinMode(whiteLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(blueLED, OUTPUT);
}
void loop() {
unsigned long currentTime = millis() % 90000; // Loop time is 90 seconds
// White LED: Fade in and out
controlLED(whiteLED, 255, 0, 0, 25000, currentTime); // 0-25 sec: Fade out
controlLED(whiteLED, 0, 255, 65000, 90000, currentTime); // 65-90 sec: Fade in
// Yellow LED: Fade in and out
controlLED(yellowLED, 0, 255, 7500, 17500, currentTime); // 7.5-17.5 sec: Fade in
controlLED(yellowLED, 255, 0, 17500, 27500, currentTime); // 17.5-27.5 sec: Fade out
controlLED(yellowLED, 0, 255, 60000, 70000, currentTime); // 60-70 sec: Fade in
controlLED(yellowLED, 255, 0, 70000, 80000, currentTime); // 70-80 sec: Fade out
// Red LED: Fade in and out
controlLED(redLED, 0, 255, 20000, 27500, currentTime); // 20-27.5 sec: Fade in
controlLED(redLED, 255, 0, 27500, 35000, currentTime); // 27.5-35 sec: Fade out
controlLED(redLED, 0, 255, 55000, 62500, currentTime); // 55-62.5 sec: Fade in
controlLED(redLED, 255, 0, 62500, 70000, currentTime); // 62.5-70 sec: Fade out
// Blue LED: Fade in, stay, and fade out
controlLED(blueLED, 0, 255, 27500, 35000, currentTime); // 27.5-35 sec: Fade in
controlLED(blueLED, 255, 255, 35000, 55000, currentTime); // 35-55 sec: Stay at full brightness
controlLED(blueLED, 255, 0, 55000, 62500, currentTime); // 55-62.5 sec: Fade out
}
// Function to control LED brightness
void controlLED(int pin, int startBrightness, int endBrightness, unsigned long startTime, unsigned long endTime, unsigned long currentTime) {
// Check if the current time is within the active period for this LED's fade cycle
if (currentTime >= startTime && currentTime <= endTime) {
unsigned long duration = endTime - startTime;
// Map the current time to the appropriate brightness level based on start and end brightness
int brightness = map(currentTime - startTime, 0, duration, startBrightness, endBrightness);
// Ensure full fade-out in the last few milliseconds of the cycle, if fading out
// IF LEDS REMAIN SLIGHTLY ON AT THE END OF THE CYCLE, INCREASE THE 5MS THRESHOLD (E.G., TO 10MS)
if ((endTime - currentTime) < 5 && endBrightness == 0) {
brightness = 0; // Force brightness to 0 to fully turn off the LED
}
// Apply the calculated brightness to the LED
analogWrite(pin, brightness);
}
}