Go Down

### Topic: Comparing time with RTC clock (Read 1 time)previous topic - next topic

#### sleziak

##### Jun 08, 2012, 04:52 amLast Edit: Jun 08, 2012, 05:03 am by sleziak Reason: 1
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

#### Delta_G

#1
##### Jun 08, 2012, 05:24 am
Easy.  If the time is between 5:00 and 7:00 then water on.  Else water off.

Or to put it in math language, if the time is greater than 5:00 AND less than 7:00...
Ad hoc, ad loc, and quid pro quo.  So little time - so much to know!  ~Jeremy Hillary Boob Ph.D

#### PetaVolt

#2
##### Jun 08, 2012, 05:27 amLast Edit: Jun 08, 2012, 05:37 am by PetaVolt Reason: 1
Something like:

Code: [Select]
`if(currentHours > 5 && currentHours < 7){digitalWrite(pump, HIGH);}else{digitalWrite(pump, LOW);}`

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

#### Jack Christensen

#3
##### Jun 08, 2012, 05:31 amLast Edit: Jun 08, 2012, 05:33 am by Jack Christensen Reason: 1
Or perhaps

Code: [Select]
`if(currentHours >= 5 ...`

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

#### PetaVolt

#4
##### Jun 08, 2012, 05:32 amLast Edit: Jun 08, 2012, 05:37 am by PetaVolt Reason: 1

Or perhaps

Code: [Select]
`if(currentHours >= 5 ...`

Whoops, you are correct.

#### marco_c

#5
##### Jun 08, 2012, 06:05 am
Agree with all the above. I would also add that unless you want to deal with am/pm then use 24 hour clock.
Arduino Libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com
Arduino++ blog https://arduinoplusplus.wordpress.com

#### bperrybap

#6
##### Jun 08, 2012, 10:12 am
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

#### sleziak

#7
##### Jun 08, 2012, 12:32 pmLast Edit: Jun 08, 2012, 12:35 pm by sleziak Reason: 1
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
Thank you for your help and sorry for this dumb question, its obviously just if > and <

Regards

Go Up

Please enter a valid email to subscribe