I am making progress. I now have LEDs flashing:
1 each second. This is simply on for 100ms and off for 900ms in a loop.
1 each minute. This is triggered by the RTC and flashes at the start of the minute.
1 each hour. This is triggered by the RTC and flashes at the start of the hour.
1 at midnight. This is triggered by the RTC and flashes at the start of the hour.
At midnight, they all flash at the same time, as required. The seconds, minute and hour LED stay on for the same duration, but for some reason, the midnight LED stays on for longer.
I had a variable that was set to 100ms and was used by all of the LEDs, but as the midnight LED misbehaved, I gave it its own variable. However, no matter what value I put in this variable, it still stays on for a longer time than the others. It was only when I set it to zero that it changed, flashing then briefly.
I have posted the code below and should welcome any suggestions as to the reason for this behaviour. I can only assume that I am missing something obvious.
void Seconds_Flasher() {
// Flash the first LED every second
unsigned long Present_Time = millis();
Elapsed_Time = Present_Time – Previous_Millis;
if (Elapsed_Time <= Period_On) {
State_of_LED = HIGH;
}
if (Elapsed_Time > Period_On) {
State_of_LED = LOW;
}
if (Elapsed_Time >= Period_On + Period_Off) {
Previous_Millis = Present_Time;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin1, State_of_LED);
}
void Minutes_Flasher() {
// Flash the second LED at the start of every minute without using delay
static unsigned long previousMillisMinutes = 0;
const long intervalMinutes = 100;
DateTime now = rtc.now();
This_is_the_Current_Minute = now.minute();
if (This_is_the_Current_Minute != This_is_the_Previous_Minute) {
This_is_the_Previous_Minute = This_is_the_Current_Minute;
previousMillisMinutes = millis();
digitalWrite(ledPin2, HIGH);
}
if (millis() – previousMillisMinutes >= intervalMinutes) {
digitalWrite(ledPin2, LOW);
}
}
void Hours_Flasher() {
// Flash the third LED at the start of every hour without using delay
static unsigned long previousMillisHours = 0;
const long intervalHours = 100;
DateTime now = rtc.now();
This_is_the_Current_Hour = now.hour();
if (This_is_the_Current_Hour != This_is_the_Previous_Hour) {
This_is_the_Previous_Hour = This_is_the_Current_Hour;
previousMillisHours = millis();
digitalWrite(ledPin3, HIGH);
}
if (millis() – previousMillisHours >= intervalHours) {
digitalWrite(ledPin3, LOW);
}
}
void Midnight_Flasher() {
// Flash the fourth LED at midnight to trigger the change of day without using delay
static unsigned long previousMillisMidnight = 0;
const long intervalMidnight = 100;
DateTime now = rtc.now();
if (now.hour() == 0 && now.minute() == 0 && now.second() == 0) {
previousMillisMidnight = millis();
digitalWrite(ledPin4, HIGH);
}
if (millis() – previousMillisMidnight >= intervalMidnight) {
digitalWrite(ledPin4, LOW);
}
}