I have a relay that is currently triggered once the RTC is equal to the time set in the code, then turned off once that time equals the off time during that code. The issue i have is, if the arduino loses power during that on period of time, it does not trigger the relay again. Is it possible to specify a range of time for it to be on?
This is my existing code:
#include <DS3231.h>
int Relay = 8;
DS3231 rtc(SDA, SCL);
Time t;
const int OnHour = 16;
const int OnMin = 30;
const int OffHour = 22;
const int OffMin = 30;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
rtc.begin();
pinMode(Relay, OUTPUT);
digitalWrite(Relay, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
t = rtc.getTime();
if(t.hour == OnHour && t.min == OnMin){
digitalWrite(Relay, HIGH);}
else if(t.hour == OffHour && t.min == OffMin){
digitalWrite(Relay, LOW);}
}
Thank you John, yes ON time is always earlier in the day than the OFF time, i'll try that code out. This is still all new to me so what is the "||" function or is that just meant to be representing spacing?
Hi John, whilst the code has been great for ensuring the device triggers the relay should it lose power during the ON period. It seems to be failing to trigger the OFF state now, i can only assume it has to be in relation to the if statement but can't seem to work it out.
Strangest thing just occurred, it turned off once it hit 11pm instead of the 10:30pm set. I can't see anything obvious in the code that would of delayed it by 30 minutes.