Go Down

Topic: start/stop timer using RTC issue (Read 705 times) previous topic - next topic



Complete newbie here. After hours and hours of reading I finally got my first skectch working and am quit impressed with myself.  Well for the most part.  What i'm trying to do is turn on 2 relays at a certain time.  I want one to turn on and stay on then turn on another for a few minutes, then turn it off for a few minutes over and over until a certain time to shut everything off until the next day.  Everything works as it should until i simulate a power outage.  Depending on the time the relay may or may not start.  This is the part of the code where it checks the time and where the problem is
Code: [Select]
DateTime now = RTC.now();  // Read in what our current datestamp is from RTC
           if ((now.hour() >= startHour && now.minute() >= startMinute) &&
         (now.hour() <= endHour)) 

so if I have the start time 7, 10, and end time of 22, 15 it starts on time and runs all day until the end hour, but if I cycle the power at 3, 01 it wont start back up til 10 after.  I cant figure out how to tell it that a 01 now.minute is ok as long as now.hour is after the startHour
I tried using the TimeAlarm library as well but found the same issue.  Im sure there a simple solution that I'm missing and maybe it would come to me if I took a break form it for a few days but thought I would ask for a bit of help first.  I could live with the way it is but hate it when things don't work just right.  Thanks for taking the time to help


You can't do the comparison that simply. The start minute is only relevant during the first hour. You would need something like this to tell whether the time was after the start:

Code: [Select]

if( ((now.hour() == startHour && now.minute() >= startMinute) || (now.hour() > startHour)) )

I'm not clear whether you have an 'end minute' parameter so I haven't tried to code the second half of the condition, but you get the idea.

This seems like a problem that would be much easier to solve if you converted the current time and start/end times into 'minutes since midnight' so that you could just do a couple of direct comparisons.


You'd probably find it easier to compare the times if you specify them in minutes since midnight so that you do something like this:
Code: [Select]

DateTime now = RTC.now();  // Read in what our current datestamp is from RTC
int now_mins = now.hour()*60 + now.minute();
int start_mins = 7*60 + 10;  // start at 0710
int end_mins = 22*60 + 15;  // end at 2215

  if(now_mins >= start_mins && now_mins < end_mins)

You wouldn't necessarily declare all the variables where I've shown. In particular, start_mins and end_mins could be declared globally so that you can access them elsewhere in the code too.

Don't send me technical questions via Private Message.


Awesome, I'll give it a try.  I figured there had to be a easier way. Thanks for the help

Go Up