Go Down

Topic: Comparing time with RTC clock (Read 858 times) previous topic - next topic

sleziak

Jun 08, 2012, 04:52 am Last 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

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

PetaVolt

#2
Jun 08, 2012, 05:27 am Last 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 am Last 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.
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

PetaVolt

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

Or perhaps

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


Whoops, you are correct.

marco_c

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<br />Parola for Arduino http://parola.codeplex.com

bperrybap

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

Regards

Go Up