CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Light timer

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Markdem



Joined: 24 Jun 2005
Posts: 206

View user's profile Send private message Send e-mail

Light timer
PostPosted: Sat Jan 28, 2006 3:15 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Sat Jan 28, 2006 7:29 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Sat Jan 28, 2006 8:04 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Sun Jan 29, 2006 1:32 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Sun Jan 29, 2006 3:23 am     Reply with quote

Hi Mark,

Try clickin secondary button and click on 'Save Link As', works ok Smile

regards
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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