[SOLVED] On/OFF time and how to calculate if timeNow is in between?

Oh my... I even don't know how to search for my question so I ask here for some help!

I have an ON and OFF time and I need to calculate if a relay should go on after setting those ON/OFF times in a menu system.

In EVERY case, when setting those time, I need to calculate if the Relay should activate.
So the check need to be in retrospect so to say.

It's way more easy to check if the time passes the set on/off time but NO, I need to check the relay activation while the ON time already has passed.

So for example:

  • Time is 11:35 PM (23:35) and we enter in the menu system:

On Time: 11:00 PM (23:00)
Off Time: 06:00 AM (06:00)

  • I save the above time settings and now the relay should go ON until off time

The catch is that in every combination of ON/OFF time, I need to determine if the relay should be on or off at that very moment, not after passing on/off time.

I must be SO SIMPLE but I can't figure it out and as I said, I don't know how to search this out with Google...

Many people use "minutes past midnight" or "seconds past midnight" for day timers, alarms, etc.

Here is a simple function and Arduino test program that allows you to determine whether a given time is within an interval, regardless of whether midnight has passed:

void setup() {
  Serial.begin(9600);

  unsigned int start_minutes = 23 * 60 + 58;  //start at 23:58
  unsigned int duration = 4; //off after 4 minutes at 00:01

  Serial.print("start time in minutes past midnight ");
  Serial.print(start_minutes);
  Serial.print(", duration ");
  Serial.println(duration);

// loop index i replaces RTC time input
  for (int i = 0; i < 10; i++) { //count some minutes, beginning at 23:57, modulo 1440
    int now_minutes = (23 * 60 + 57 + i) % 1440;  //time, minutes past midnight
    Serial.print(now_minutes);
    Serial.print("\t");
    unsigned int t = (now_minutes - start_minutes + 1440) % 1440; //time since start time, modulu 1440
    Serial.print( t );
    
    // check time and set fan state 
    Serial.print("\tfan state = ");
    if ( t < duration) fan(1);  //fan should be on
    else fan(0);  //fan should be off
    
    // does the interval check function agree?
    Serial.print (" interval check function returns ");
    Serial.println(within_interval(start_minutes, duration, now_minutes));
  }
}

// function to "turn on fan"
void fan(int on) {
  Serial.print(on);
}

// 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 within_interval(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
}

void loop() {}

1 Like

Doing all time functions using Epoch Time will make your life much easier.

1 Like

Thank you so much!!
I could not have figured that out myself...

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