24 hour timer code anyone?
CuriousOne
Posts: 931
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:
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?
'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
(Disregard line numberings, they were added when copy-pasting from scratchpad)
Are DR0, START1 and END1 all Word variables?
DRO is word, START1 and END1 are byte type variables.
I've inserted the modified code, but forgot to remove old one, so it was interfering, thank you very much!
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.