|
|
View previous topic :: View next topic |
Author |
Message |
Markdem
Joined: 24 Jun 2005 Posts: 206
|
Light timer |
Posted: Sat Jan 28, 2006 3:15 am |
|
|
Hi All, i am tring to get a simple light timer working. I am testing the time from a ds1307 by the following statment
Code: |
(currenthrs >= on_hour[i] && currenthrs <= off_hour[i] && currentmin >= on_min[i] && currentmin < off_min[i])
|
This all works OK, it the off_min is bigger then the on_min
on time 19.10 off time 19.50.
But if i try
on time 19.10 off time 20.01, it will not work. I can seem to come up with a good test statment.
Any one have any ideas??
Thanks, Mark |
|
|
Eric Minbiole
Joined: 10 Jun 2004 Posts: 16 Location: USA
|
|
Posted: Sat Jan 28, 2006 7:29 am |
|
|
To make the logic easier, I would make a small helper routine that simply compares two times, taking into account that hours are more significant than minutes. The example below returns +1 if A>B, -1 if A<B, and 0 if they are equal:
Code: |
signed int8 CompareTime(int8 hourA, int8 minA, int8 hourB, int8 minB)
{
// compare hours first
if (hourA > hourB)
return 1; // time A > time B
if (hourA < hourB)
return -1 // time A < time B
// hours equal: compare minutes
if (minA > minB)
return 1; // time A > time B
if (minA < minB)
return -1; // time A < time B
// both times equal
return 0
}
|
Then, in your main code, do something like this:
Code: |
if ((CompareTime(currenthrs, currentmin, on_hour[i], on_min[i]) >= 0) &&
(CompareTime(currenthrs, currentmin, off_hour[i], off_min[i]) <= 0))
{
// current time is within start/stop times: turn on lamp
}
|
Also, creating a structure that has both hours & minutes together might help clean up the code, too. Hope this helps! |
|
|
J1M
Joined: 15 Feb 2005 Posts: 21
|
|
Posted: Sat Jan 28, 2006 8:04 am |
|
|
Hi, in my page www.hobbypic.com ive uploaded a project called TerraPIC, its a controller for terrariums or small conservatories ;), project has a hygrostat, termostat, and two timers. This timers makes that you are looking for ;)
best regards |
|
|
Markdem
Joined: 24 Jun 2005 Posts: 206
|
|
Posted: Sun Jan 29, 2006 1:32 am |
|
|
Hello all, Eric Minbiole, thanks for the code, but it is still doing the same as my code. If the off min time is leas then the on min time, it will not go on.
J1M, i cant seem to download your code for the ftp. Could you please upload it here??
Thanks Mark |
|
|
J1M
Joined: 15 Feb 2005 Posts: 21
|
|
Posted: Sun Jan 29, 2006 3:23 am |
|
|
Hi Mark,
Try clickin secondary button and click on 'Save Link As', works ok
regards |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|