I have a small project working with a ds3231 rtc using the ds3231 library to schedule some relays.
Everything is working, but after tinkering and getting everything up, I realized that the triggering event for the relays is the exact minute I've set in the onhour/off hour constants. If I restart the system after the onhour time, there is no trigger to initiate the relay until the onhour cycles around again tomorrow morning.
Is there a way to say that the onhour is between onhour and offhour so that the relay is triggered at any point during the on period?
Ultimately, this is not a make or break problem, but just one of those cool little problems that help illustrate more of the inner workings of the logic. Happy to have this problem
You could save the on/off state in EEPROM so that when the project is started you can set the on/off state as it was. Save the state whenever it changes and read and set the state in setup().
I'm not sure if that will help, but instead of working with hours it's easier to work with seconds, this way you can easily check if the current time is within the on or off period
It is easy to use "minutes since midnight" for timed tasks.
The following function determines whether a specific time is within an interval, and works if the interval spans midnight (e.g. if you want a lamp on between 11 PM and 1 AM the next day).
// this function determines whether the current time in minutes past midnight
// is within the timed interval start and start+duration, also in minutes past midnight
// range may span midnight
#define MIN_PER_DAY 1440
byte state(unsigned int start, unsigned int duration, unsigned int now) {
unsigned int time_on = (now - start + 2*MIN_PER_DAY) % MIN_PER_DAY; //multiply minutes per day by two for safety
if (time_on < duration) return 1; //within interval
return 0; //not within interval
}