I enter the given code
#include <DS3231.h>
#include <time.h>
unsigned long MyTestTimer = 0; // Timer-variables MUST be of type unsigned long
const byte OnBoard_LED = 13; // onboard-LED uno, mega
const byte Relay_pin = 8;
DS3231 rtc (SDA, SCL);
Time t;
const byte NumOfTimePoints = 3;
const unsigned long OnSecsOfDay[NumOfTimePoints] = {
(11UL * 3600 + 2 * 60 + 0),
(11UL * 3600 + 3 * 60 + 10),
(11UL * 3600 + 4 * 60 + 20)
};
const unsigned long OffSecsOfDay[NumOfTimePoints] = {
(11UL * 3600 + 2 * 60 + 5),
(11UL * 3600 + 3 * 60 + 15),
(11UL * 3600 + 4 * 60 + 25)
};
unsigned long secondsOfDay;
/*
const int OnHours[NumOfTimePoints] = {14, 14, 14}; // Array of activation hours
const int OnMins[NumOfTimePoints] = {05, 06, 07}; // Array of activation minutes
const int OnSecs[NumOfTimePoints] = {0x00, 0x10, 0x20}; // Array of activation seconds
const int OffHours[NumOfTimePoints] = {14, 14, 14}; // Array of deactivation hours
const int OffMins[NumOfTimePoints] = {05, 06, 07}; // Array of deactivation minutes
const int OffSecs[NumOfTimePoints] = {0x05, 0x15, 0x25}; // Array of deactivation seconds
*/
bool executedOn[NumOfTimePoints] = {false, false, false}; // Array indicating whether timed events have been executed
bool executedOff[NumOfTimePoints] = {false, false, false}; // Array indicating whether timed events have been executed
void setup() {
Serial.begin(9600);
Serial.println("Setup-Start");
PrintFileNameDateTime();
rtc.begin();
digitalWrite(Relay_pin, LOW);
pinMode(Relay_pin, OUTPUT);
}
void loop() {
BlinkHeartBeatLED(OnBoard_LED, 250); // non-blocking blinking
// non-blocking timing
// check if 1000 milliseconds of time have passed by
if ( TimePeriodIsOver(MyTestTimer, 1000) ) {
// when REALLY 1000 milliseconds of time HAVE passed by
// execute code below
t = rtc.getTime();
//t = rtc.getDateTime();
Serial.print(t.hour);
Serial.print(" hour(s), ");
Serial.print(t.min);
Serial.print(" minute(s)");
Serial.print(t.sec);
Serial.print(" second(s)");
Serial.println();
secondsOfDay = t.hour * 3600UL + t.min * 60 + t.sec;
// delay (1000); // no blocking delay needed
}
for (int i = 0; i < NumOfTimePoints; i++) {
if (secondsOfDay >= OnSecsOfDay[i] ) {
digitalWrite(Relay_pin, HIGH);
Serial.println("MOTOR ON");
executedOn[i] = true; // Indicates that the event has been executed
executedOff[i] = false; // Indicates that the event has been executed
}
if (secondsOfDay >= OffSecsOfDay[i] ) {
digitalWrite(Relay_pin, LOW);
Serial.println("MOTOR OFF");
executedOn[i] = false; // Indicates that the event has been executed
executedOff[i] = true; // Indicates that the event has been executed
}
}
}
// helper-functions
void PrintFileNameDateTime() {
Serial.println( F("Code running comes from file ") );
Serial.println( F(__FILE__) );
Serial.print( F(" compiled ") );
Serial.print( F(__DATE__) );
Serial.print( F(" ") );
Serial.println( F(__TIME__) );
}
// easy to use helper-function for non-blocking timing
// explanation see here
// https://forum.arduino.cc/t/example-code-for-timing-based-on-millis-easier-to-understand-through-the-use-of-example-numbers-avoiding-delay/974017
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - startOfPeriod >= TimePeriod ) {
// more time than TimePeriod has elapsed since last time if-condition was true
startOfPeriod = currentMillis; // a new period starts right here so set new starttime
return true;
}
else return false; // actual TimePeriod is NOT yet over
}
void BlinkHeartBeatLED(int IO_Pin, int BlinkPeriod) {
static unsigned long MyBlinkTimer;
pinMode(IO_Pin, OUTPUT);
if ( TimePeriodIsOver(MyBlinkTimer, BlinkPeriod) ) {
digitalWrite(IO_Pin, !digitalRead(IO_Pin) );
}
}
The program is now working, it also turns on at the first value
(11UL * 3600 + 2 * 60 + 0)
However, when it should be turned off, the relay starts clicking very quickly (turns on and off in milliseconds) and does not stop
The Serial monitor:
11 hour(s), 2 minute(s)57 second(s)
MOTOR OFF
MOTOR ON
MOTOR OFF
MOTOR ON
MOTOR OFF
MOTOR ON
MOTOR OFF
MOTOR OFF
MOTOR ON
MOTOR OFF
MOTOR ON
MOTOR OFF
MOTOR ON
MOTOR OFF
11 hour(s), 2 minute(s)58 second(s)