Signal on during period of time?

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);}
  }

Welcome

See this : Trying to make the perfect time On/off code or function - #15 by guix

Yes, but what you wrote is a minute for it to turn on and a minute for it to turn off.

If the ON time is always earlier in the day than the OFF time:

   boolean afterOnTime =
    (t.hour > OnHour)
    || (t.hour == OnHour && t.min >= OnMin);

  boolean beforeOffTime =
    (t.hour <= OffHour)
    || (t.hour == OffHour &&  t.min < OffMin);

  if (afterOnTime && beforeOffTime)
  {
    digitalWrite(Relay, HIGH);
  }
  else
  {
    digitalWrite(Relay, LOW);
  }

Thanks for that,

If i'm understanding correctly, i should be doing the following:

if ((OnHour <= t.hour()) && (t.hour() <= OffHour)){
  digitalWrite(Relay, HIGH);}

else {
  digitalWrite(Relay, LOW);
}

Not sure how i would go about using both the hour and minutes, is this where converting to seconds is necessary?

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?

It is the boolean OR operator. You can use the keyword 'or' if you like that better. The result is 'true' if either or both operands are 'true'.

The &&/'and' operator results in 'true' only if both operands are 'true'.

You may find it easier to calculate a "seconds since midnight" like this:

long ssm = (t.hour() * 100 + t.minute() ) * 100 + t.second();

Why did I multiply by 100 instead of 60? Because then you can conveniently specify your on & off times like this:

if (ssm >= 163000 && ssm < 205959) { ...

where "163000" means 16:30:00

1 Like

Thank you for the explanation, the code you suggested worked a charm.

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.

It is never turning off? Please show me your sketch.

No, it is no turning off. It's currently past the set OffHour & OffMin.

#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();
  
  boolean afterOnTime =
    (t.hour > OnHour)
    || (t.hour == OnHour && t.min >= OnMin);

  boolean beforeOffTime =
    (t.hour <= OffHour)
    || (t.hour == OffHour && t.min <= OffMin);
  
  if (afterOnTime && beforeOffTime)
  {
    digitalWrite(Relay, HIGH);
  }  
  else
  {
    digitalWrite(Relay, LOW);
  }
}

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.

I see my mistake:

(t.hour <= OffHour)

should have been:

(t.hour < OffHour)

That had the effect of keeping it on until the end of OffHour.

1 Like

Makes sense when you break it down like that. Thank you again for your help.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.