Shop OBEX P1 Docs P2 Docs Learn Events
24 hour timer code anyone? — Parallax Forums

24 hour timer code anyone?

CuriousOneCuriousOne Posts: 931
edited 2014-09-19 08:44 in BASIC Stamp
Hello, I'm designing a device, which will allow user to enable light at certain time and disable it at also specified time. I'm using DS1302 as clock source and using 24 hour mode for easier math. My algorithm regarding the on-off thing is as follows:
'DRO = Time in minutes (received from DS1302)
'STARTH1=start time in hours
'STARTM1=start time in minutes
'ENDH1=end time in hours
'ENDM1=end time in minutes
'RELE1=relay port
'FLAG1=character value for display, displays asterisk when enabled, displays space, when disabled
 

IF DRO=>STARTH1*60+STARTM1 AND DRO=<ENDH1*60+ENDM1 THEN 'CHECK IF TIME IS WITHIN FRAME, IF YES, THEN ENABLE, IF NOT, THEN DISABLE
HIGH RELE1 'ENABLE RELAY
FLAG1=42 'SET DISPLAY FLAG FOR PORT
ELSE
LOW RELE1 'DISABLE 
FLAG1=32 'SET FLAG TO EMPTY
ENDIF

this code works perfectly, if start time is less than end time (say start time is 18:00 and end time is 21:00). But it does not works properly when end time is "smaller" than start time (say start time is 21:00 and end time is 02:00). I've asked a friend, and she wrote the "proper" code in C, but I have no idea how to translate it to BS2, any simple addition to above code might solve the problem?

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2014-09-18 06:50
    The trick is to recognize that time is a repeating interval. 02:00 is a different day than the previous night's 21:00 even though time itself is a continuum. When you set the start time and end time, check to see if the end time is less than the start time. If so, add 24 to the end time hours.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-09-18 09:13
    I wonder if the code in this old Digital Thermostat project might be of any help? http://www.savagecircuits.com/digitalthermostat
  • CuriousOneCuriousOne Posts: 931
    edited 2014-09-18 11:07
    Yes, my friend wrote a code, which will give me amount of time (properly calculated), I have to wait after turning on the lights, before turning off, but if I add such code, I still get problem, since DS1302 output becomes 0 at midnight, this means I have to start another counter and count minutes by myself
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-09-18 11:50
    If you're trying to create your own count in terms in seconds/minutes and not using real time you simply increment/decrement your counter each time the RTC seconds change. I can't find a link at the moment, but that's what we did with a pool timer project listed on here years ago.
  • SapphireSapphire Posts: 496
    edited 2014-09-18 17:43
    It's not that difficult. First, check to see if the end time is higher than the start time. If it is, use the code you have. If it isn't, then use a different routine, just like the one you have except substitute OR for AND, like this:
    IF DRO=>STARTH1*60+STARTM1 OR DRO=<ENDH1*60+ENDM1 THEN 'CHECK IF TIME IS WITHIN FRAME, IF YES, THEN ENABLE, IF NOT, THEN DISABLE
    
  • CuriousOneCuriousOne Posts: 931
    edited 2014-09-18 21:53
    The problem is, that in certain cases, even if I detect that end time is higher than start time, the "time" itself, transits trough 0, so messes things around. For example, this is the code I've modelled after your code, it does not works:
    [TABLE]
    [TR]
    [TD="class: number"][/TD]
    [TD="class: content"]'PORT 1 [/TD]
    [/TR]
    [/TABLE]
    [TABLE]
    [TR]
    [TD="class: number"]02[/TD]
    [TD="class: content"]START1 = STARTH1*60+STARTM1   [/TD]
    [/TR]
    [/TABLE]
    
    [TABLE]
    [TR]
    [TD="class: number"]03[/TD]
    [TD="class: content"]END1 = ENDH1*60+ENDM1   [/TD]
    [/TR]
    [/TABLE]
    
    [TABLE]
    [TR]
    [TD="class: number"]04[/TD]
    [TD="class: content"]IF END1 > START1 THEN   [/TD]
    [/TR]
    [/TABLE]
    
    [TABLE]
    [TR]
    [TD="class: number"]05[/TD]
    [TD="class: content"]IF DRO => START1 AND DRO =< END1 THEN   [/TD]
    [/TR]
    [/TABLE]
    
    [TABLE]
    [TR]
    [TD="class: number"]06[/TD]
    [TD="class: content"]HIGH RELE1  [/TD]
    [/TR]
    [/TABLE]
    
    [TABLE]
    [TR]
    [TD="class: number"]07[/TD]
    [TD="class: content"]FLAG1=42  [/TD]
    [/TR]
    [/TABLE]
    
    [TABLE]
    [TR]
    [TD="class: number"]08[/TD]
    [TD="class: content"]ELSE   [/TD]
    [/TR]
    [/TABLE]
    
    [TABLE]
    [TR]
    [TD="class: number"]09[/TD]
    [TD="class: content"]LOW RELE1   [/TD]
    [/TR]
    [/TABLE]
    
    [TABLE]
    [TR]
    [TD="class: number"]10[/TD]
    [TD="class: content"]FLAG1=32 [/TD]
    [/TR]
    [/TABLE]
    
    [TABLE]
    [TR]
    [TD="class: number"]11[/TD]
    [TD="class: content"]ENDIF  [/TD]
    [/TR]
    [/TABLE]
    
    [TABLE]
    [TR]
    [TD="class: number"]12[/TD]
    [TD="class: content"]   [/TD]
    [/TR]
    [/TABLE]
    
    [TABLE]
    [TR]
    [TD="class: number"]13[/TD]
    [TD="class: content"]ELSE   [/TD]
    [/TR]
    [/TABLE]
    
    [TABLE]
    [TR]
    [TD="class: number"]14[/TD]
    [TD="class: content"]IF DRO => START1 OR DRO =< END1 THEN   [/TD]
    [/TR]
    [/TABLE]
    
    [TABLE]
    [TR]
    [TD="class: number"]15[/TD]
    [TD="class: content"]HIGH RELE1 [/TD]
    [/TR]
    [/TABLE]
    
    [TABLE]
    [TR]
    [TD="class: number"]16[/TD]
    [TD="class: content"]FLAG1=42   [/TD]
    [/TR]
    [/TABLE]
    
    [TABLE]
    [TR]
    [TD="class: number"]17[/TD]
    [TD="class: content"]ELSE   [/TD]
    [/TR]
    [/TABLE]
    
    [TABLE]
    [TR]
    [TD="class: number"]18[/TD]
    [TD="class: content"]LOW RELE1 [/TD]
    [/TR]
    [/TABLE]
    
    [TABLE]
    [TR]
    [TD="class: number"]19[/TD]
    [TD="class: content"]FLAG1=32   [/TD]
    [/TR]
    [/TABLE]
    
    [TABLE]
    [TR]
    [TD="class: number"]20[/TD]
    [TD="class: content"]ENDIF   [/TD]
    [/TR]
    [/TABLE]
    
    [TABLE]
    [TR]
    [TD="class: number"]21[/TD]
    [TD="class: content"]endif[/TD]
    [/TR]
    [/TABLE]
    
    

    (Disregard line numberings, they were added when copy-pasting from scratchpad)
  • SapphireSapphire Posts: 496
    edited 2014-09-18 22:06
    How are you calculating DR0?

    Are DR0, START1 and END1 all Word variables?
  • CuriousOneCuriousOne Posts: 931
    edited 2014-09-18 23:48
    DRO (means "time") is calculated here:
    saatebi =( hh >> 4 * 10 ) + ( hh // 16 )
    cutebi =( mm >> 4 * 10 ) + ( mm // 16 )
    DRO=SAATEBI*60+CUTEBI
    

    DRO is word, START1 and END1 are byte type variables.
  • CuriousOneCuriousOne Posts: 931
    edited 2014-09-19 01:04
    Fixed!

    I've inserted the modified code, but forgot to remove old one, so it was interfering, thank you very much!
  • SapphireSapphire Posts: 496
    edited 2014-09-19 08:44
    START1 and END1 need to be Words.

    If the end time is 23:30, then END1 = 23*60 + 30 = 1410 which won't fit in a Byte. The result will be 130 which is 02:10.
Sign In or Register to comment.