Comparing time with RTC clock

Hello, i'm currently working on an auto watering system for my plants, I'm currently stuck with some logic ..

Let's say I want the pump to start at 5:00 water for 2 hours and stop at 7:00, what would be the best way to do the math?

Please note that it also has to work if the arduino has been turned on when the watering should already be in progress

P.S. I have a RTC attached to my arduino, I'm able to get year, month, day, hour, minute, second, unix time , ...

Regards

Something like:

if(currentHours > 5 && currentHours < 7)
{
digitalWrite(pump, HIGH);
}

else
{
digitalWrite(pump, LOW);
}

should do it. Of course, replace pump and currentHours.

Or perhaps

if(currentHours >= 5 ...

assuming currentHours is a variable that just contains the hour, i.e. 0-23.

Whoops, you are correct.

Agree with all the above. I would also add that unless you want to deal with am/pm then use 24 hour clock.

I'd stay away from local time and all its issues.
Trying to calculate deltas and durations with a broken
down local time like HH:MM:SS MM/DD/YY is an absolute mess.
Epoch based timing makes things so much simpler so it is often best
to stick with something like Unix time.

I'd recommending using
using Michael's Arduino time & timealarms library:
http://www.arduino.cc/playground/Code/Time
which does all that for you.

With that you can simply set an alarm to call a function.
In your case you could have it call a function to start the watering and another to stop the watering.
The timealarms library has some useful features that allow you do things based times
every day or only on particular days of the week.

--- bill

First 5 replies:

Yes, I got that far too but what if I want to change the shedule to 22:00 - 02:00

bperrybap, Alarms won't work for me since it is possible that I will turn the system on while it should be watering already

Regards

EDIT: Nevermind, the simple solution actually works best, it was 5am when I was trying to solve this :smiley:
Thank you for your help and sorry for this dumb question, its obviously just if > and <

Regards