ON and OFF time control over 24 hour period in C, C++

  c++, rational-team-concert, time, timer

Using c, c++ (mbed, Arduino, etc), Is there a trick up c’s sleeve to be able to set an ON time and OFF time over a 24 hour period. For instance ‘ON’ at 20:00 hours and off at 06:30 hours following morning.
Timers are no good here if there is a nvic reset. If the device does fall over and restart’s at say 23:40 hours, we still need to service that 20:00 to 06:30 time frame.

Stuck on the going past midnight.

I’ve got this far using seconds but not quite working, but I’m sure I’m barking up the wrong tree so I would appreciate some clever input here.

lockStatus = 1 is ‘ON’
lockStatus = 0 is ‘OFF’

void autoLOCK()
{
  int hour_from, minute_from = 0;
  int seconds_from = 0 ;
  
  int hour_to, minute_to = 0;
  int seconds_to = 0;

  lockFrom  =  "20:00";   
  lockTo    =  "06:30";

  if (sscanf(lockFrom, "%d:%d", &hour_from, &minute_from) >= 2)
  {
    seconds_from = (hour_from * 3600 + minute_from * 60);
  }
  if (sscanf(lockTo, "%d:%d", &hour_to, &minute_to) >= 2)
  {
    seconds_to = (hour_to * 3600 + minute_to * 60);
  }

  
  lockStatus = 0;

  if (seconds_now >= seconds_from) {
    lockStatus = 1;
  }

  if (seconds_from > seconds_to) {
    lockStatus = 1;
  }

  if (seconds_now >= seconds_to && seconds_from >= seconds_to) {
    lockStatus = 0;
  }

  Serial.printf("Lock Status:  %dnn", lockStatus);
  
}

Source: Windows Questions C++

LEAVE A COMMENT