I'm using a DS3231 RTC module together with a relay to switch an AC pump on and off during certain time periods. My code looks like this at this moment
#include <DS3231.h>
DS3231 rtc(SDA, SCL);
Time t;
#define relay 2
// this is where the on and off time is set
int onHour = 5;
int onMin = 0;
int onSec = 0;
int offHour = 22;
int offMin = 0;
int offSec = 0;
void setup() {
rtc.begin();
pinMode(relay, OUTPUT);
digitalWrite(relay, LOW);
}
void loop() {
int relayState = LOW;
rtc.getTime()
if (t.hour == onHour && t.min = onMin && t.sec == onSec){
relayState = HIGH;
} if (t.hour == offHour && t.min == offMin && t.sec == offSec){
relayState = LOW;
} else {
relayState = relayState;
}
digitalWrite(relay, relayState);
}
I thought this would work, but it didn't. After re-reading my code I noticed that the relay can only turn HIGH when it's exactly at the specified "on" time. If the current time is at, for example, 09:00 when the sketch is uploaded, it's not specified as the "on" time when it is actually at that range.
How do I set a "time range" between the "on" time and the "off" time, for example: on at 05:00 to 21:59 then off at 22:00 to 04:59?
tl;dr how do I program a time range?