Hi,
I am up to make a project where certain things will be done but in a specific frame of time every day.
Firstly, it was ok to have only hours time frame:
if (hr >= startHour && hr < stopHour){
do something
}else{
don't do something
}
So if I have startHour = 8 and stopHour = 12, the thing will be done from 8 up to 12h. This is fine.
Now, I have to add minutes. Tried something like this:
if (hr >= startHour && hr < stopHour){
if(min >= startMin && min < stopMin){
do something
}else{
don't do something
}
}else{
don't do something
}
This works if the startMin is less than the stopMin. Say from 8:10 to 12:20, but it will not work if it is from 8:20 up to 12:10.
Anyone of you have messed with this logic? Any help would be nice.
@who_took_my_nick you might find it slightly easier to measure time as hours * 100 + minutes. Then you can write 810 for 08:10 and 1220 for 12:20. It saves you a little mental arithmetic!
PS. don't write 0810 for 08:10 because in C, any number beginning with a zero is in octal, not decimal
rather than using custom logic for each case consider a data driven approach using a table to define the conditions for multiple actions. see Creation of OS Scheduler
and figure out if your time library has an 'epoch' style variable.
I'd try to help more, but you haven't posted a complete code, just snippets, so I have no idea which library you're using. Too bad.
The solution is to compare startTime and stopTime to find out which of the three cases is relevant.
If stopTime is larger than startTime Working Time is the interval between startTime and stopTime.
If stopTime is less than startTime the sketch checks for the interval between stopTime and startTime (which is "Free Time") and inverts the result (Working Time = !Free Time).
something needs to be executed each iteration of loop()?
something needs to be turned on at the start of the period and turned of at the end of the period?
for the first case, can something be enabled at the start of the period and if enabled, dis-abled at the end of the period? does the system identify a period or two separate events that happen to control, start/stop the same something
the 2nd case could also be handled by recognizing events at specific times.
so a something could be enabled at 23:50 and disabled at 0:10