Pages: [1]   Go Down
Author Topic: Comparing time with RTC clock  (Read 768 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: June 07, 2012, 10:03:30 pm by sleziak » Logged

Offline Offline
God Member
*****
Karma: 19
Posts: 781
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Something like:

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

else
{
digitalWrite(pump, LOW);
}

should do it.  Of course, replace pump and currentHours.
« Last Edit: June 07, 2012, 10:37:06 pm by PetaVolt » Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4086
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Or perhaps

Code:
if(currentHours >= 5 ...

assuming currentHours is a variable that just contains the hour, i.e. 0-23.
« Last Edit: June 07, 2012, 10:33:04 pm by Jack Christensen » Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Or perhaps

Code:
if(currentHours >= 5 ...

Whoops, you are correct.
« Last Edit: June 07, 2012, 10:37:27 pm by PetaVolt » Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1273
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Dallas, TX USA
Offline Offline
Faraday Member
**
Karma: 67
Posts: 2710
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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-grin
Thank you for your help and sorry for this dumb question, its obviously just if > and <

Regards
« Last Edit: June 08, 2012, 05:35:48 am by sleziak » Logged

Pages: [1]   Go Up
Jump to: