Best way to measure if a given hour and minutes is between two times

Hi, I am trying to check if a given time, say time.now( )its between two times, in this case, starting time and ending time. What I am trying to achieve is to turn a led on based on and off based on the time inputs.

Is using Unix time good for that? Say I convert time.now() but using the starting and ending time as hardcoded values and then also turn time.now() in Unix and just check if that value is between them? I have this alternative because when the time inputs are for examen start: 23:00 and end 1:00, some problems arise that you guys may now already.

So, is there another way to do this? Maybe an easier or that uses fewer resources?

jerrymedi:
Is using Unix time good for that?

Yes.

jerrymedi:
So, is there another way to do this? Maybe an easier or that uses fewer resources?

How do you know using Unix time uses too many resources?

How do you know using Unix time uses too many resources?

I just assume that calculating that time takes some resources, but if it doesn't, then I am fine and will proceed to do it that way

The only "resource" used to compare two Epoch times, is a 32 bit subtraction. There is some overhead in conversion to/from YYMMDDHHMMSS values, but if you are using Epoch times at all, you should use them for all timing and just use YYMM.. values for human readable input/output.

For time periods limited to hour and minute settings, it is convenient to use minutes since midnight. You don't need to use unix time for that, just a 24 hour clock:

minutes_since_midnight = hour*60 + minute;

If the timed interval spans midnight, this short test program includes the function within_interval() to handle the rollover at midnight properly.

If you need seconds precision, use seconds past midnight instead (86400 per 24h) and unsigned long ints for times.

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() {}

The more you are doing with time, the better using Epoch time will be. That is because seconds are a common factor of all the longer time units. Some sketches have such simple demands, that it's not worth it. You might have an aquarium light that has only to turn on at 8am and off at 5pm, for example. Really, you can even use only hours directly from an RTC register for that. As you ramp up the features, it makes more and more sense to go with Epoch time.

jremington:
For time periods limited to hour and minute settings, it is convenient to use minutes since midnight. You don't need to use unix time for that, just a 24 hour clock:

minutes_since_midnight = hour*60 + minute;

If the timed interval spans midnight, this short test program includes the function within_interval() to handle the rollover at midnight properly.

If you need seconds precision, use seconds past midnight instead (86400 per 24h) and unsigned long ints for times.

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() {}




It seems this code only works if I now how much time will the led be on, what about if the user inserts start: 23:00 and end 8:00

what about if the user inserts start: 23:00 and end 8:00

It is no problem to change the check function to use start and end times, rather than start and duration. Consider that a challenge.

jerrymedi:
Hi, I am trying to check if a given time, say time.now( )its between two times, in this case, starting time and ending time. What I am trying to achieve is to turn a led on based on and off based on the time inputs.

Most real-time clocks give you an hour and a minute. Multiply the hour by 60 to give you a minute-in-the-day serial, and work with that.