Shop OBEX P1 Docs P2 Docs Learn Events
Solving logical equation in 24 hour format, tricky! — Parallax Forums

Solving logical equation in 24 hour format, tricky!

CuriousOneCuriousOne Posts: 931
edited 2014-06-05 07:43 in General Discussion
Hello.

I'm doing simple Basic Stamp light controller, where user inputs time in 24 hour format (hours & minutes) to turn lights on and off. The problem is, how to do logic with 24 hours?

I have DS1302 and getting time from it, so, code should look like this:
USERONHR=8 'On hour
USERONMN=35 'On minutes
USEROFFHR=19 'Off hour
USEROFFMN=18 'Off minutes
GOSUB GETTIME 'Query DS1302 and get hours and minutes into variables
IF HOURS=>USERONHR AND MINUTES=>USERONMN AND HOURS<=USEROFFHR AND MINUTES<=USEROFFMN THEN HIGH LED1 'main logic code

The above code will work, but only in case if ON minutes are less than OFF minutes.

With above provided examples, the LED1 will never come on, since MINUTES can't be less than 18 and greater than 35 at same time.

Any solution to this?

Comments

  • bomberbomber Posts: 297
    edited 2014-06-05 02:07
    One thing that I do in programs that deal with time is have a single variable that stores the time, and it is usually in seconds or in your case minutes would work the best. The variable is normally a larger number (for 24 hours like you are using and with an accuracy of 1 minute that would be a maximum value of 1440). In your case you would have two variables, one for on time and one for off time. You might get the on time by:
    TIMEON = USERONHR * 60 + USERONMN
    
    This makes the logic simpler so instead of:
    [COLOR=#3E3E3E][FONT=Parallax]IF HOURS=>USERONHR AND MINUTES=>USERONMN[/FONT][/COLOR]
    
    You could do:
    [COLOR=#3E3E3E][FONT=Parallax]IF TIME=>TIMEON[/FONT][/COLOR]
    

    Hope this made sense, typing this at 2AM and if it doesn't I'll answer any clarifications after some sleep

    -Roger
  • CuriousOneCuriousOne Posts: 931
    edited 2014-06-05 02:48
    thanks, this seems so easy! but I was not able to figure it by myself :)
  • Hal AlbachHal Albach Posts: 747
    edited 2014-06-05 07:43
    I think using the "equal or greater" comparison makes the logic more difficult. I would pursue a simpler, less elegant form of comparing the current time to time_on or time_off depending on if the light is already on or off. Sort of like "If LIGHT = OFF AND hour = hour_on AND minute = minute_on THEN LIGHT = ON" and conversely to turn light off.
Sign In or Register to comment.