Calculating Time for clock's preset

# Thread: Calculating Time for clock's preset

1. ## Calculating Time for clock's preset

What is the best way to verify that Current Time of a clock is within the program(preset) Start-End time? e.g if a program should be ON from 13:00:00 and off at 14:00:00
I thought about calculating time in seconds from midnight, but represanting 24 Hours in seconds require more than 16Bit.

2. Did you find this post helpful? |
Originally Posted by menta
I thought about calculating time in seconds from midnight, but represanting 24 Hours in seconds require more than 16Bit.
Then don't use every second...use every 2 seconds. 60*60*24=86,400 / 2 = 43,200.
Or you could use every 1.5 seconds, 60*60*24=86,400 / 1.5 = 57.600.

Or use 'binary time'...if there is such a thing...
Instead of dividing a day into 86,400 chunks, it's broken up into 65,536 chunks, each chunk being 1.318359375 seconds long.

3. Did you find this post helpful? |
Originally Posted by menta
What is the best way to verify that Current Time of a clock is within the program(preset) Start-End time? e.g if a program should be ON from 13:00:00 and off at 14:00:00
At first glance I'd say ...
Code:
```IF Hours = 13 THEN
ProgON = 1
ELSE
ProgON = 0
ENDIF```
But I'm guessing you want more than the example showed.
<br>

4. Did you find this post helpful? |
Taylor, Do you mean calculate time from Mid or Noon ?

5. Did you find this post helpful? |
Originally Posted by menta
Taylor, Do you mean calculate time from Mid or Noon ?
Well, if 13 is 13 hours, and you want to check from 13:00 to 14:00, then I suppose it's from midnight...

6. Did you find this post helpful? |
I think that calculating time from midnigh or noon will be a problem if end time of a program is after 00:00, e.g Start time - 13:00 , End time - 1:00

7. Did you find this post helpful? |
If I didn't make it clear, a program can be:
Start 13:10:02
End 13:10:05
HH:MM:SS

8. Did you find this post helpful? |
All those little details help.

Here's another possibility.
Run it once per second. Probably a gosub from the clock's code.
Code:
```;--Start Time--
Start_Hours   CON 13
Start_Minutes CON 10
Start_Seconds CON 2
;--Stop Time--
Stop_Hours    CON 13
Stop_Minutes  CON 10
Stop_Seconds  CON 5

ProgON        VAR BIT
ProgON = 0

;--sub--
IF Hours = Start_Hours THEN
IF Minutes = Start_Minutes THEN
IF Seconds = Start_Seconds THEN
ProgON = 1
ENDIF
ENDIF
ENDIF

IF Hours = Stop_Hours THEN
IF Minutes = Stop_Minutes THEN
IF Seconds = Stop_Seconds THEN
ProgON = 0
ENDIF
ENDIF
ENDIF```

9. Did you find this post helpful? |
Thanks,
The main risk is if for some reason you loose a check, then you have missed the program start time

10. Did you find this post helpful? |
Originally Posted by menta
Thanks,
The main risk is if for some reason you loose a check, then you have missed the program start time
That's why you use LONG variables, calculate out the # of seconds since midnight then do a comparison, i.e.
If current_time_in_seconds > _on_time_in_seconds then
do-whatever

Or do it the hard way with WORDs and multiple IF/Then And/Or statements...

11. Did you find this post helpful? |
how about using 2 loops depending on operating output,

if progon=1 then do turnoff checking ' "otherwise"
do turn on checking
'''
'''
'''
turnoff checking stuff
''''
turnon checking stuff

don

12. Did you find this post helpful? |
Calculating time from Midnight is not possible since it more than 16Bit.
Moreover, If start time is 13:00:00 and end time is 01:00:00 will be also a problem.

13. Did you find this post helpful? |
Maybe, checking the Seconds can be with > instead of =, since there is no chance that it will miss for more than few seconds.
Next time if the Program is already On the check will be passed.

14. Did you find this post helpful? |
Originally Posted by menta
Calculating time from Midnight is not possible since it more than 16Bit.
Moreover, If start time is 13:00:00 and end time is 01:00:00 will be also a problem.
Which is why I suggested only counting every other second above....
Or like DT said, you have another bit that'll signify AM or PM...
Code:
```time_of_day var bit
am con 0
pm con 1```
if time_of_day = 0 ' then it must be between 0000 and 1159
if time_of_day = 1 ' then it must be between 1200 and 2359...

Or again, you can use a LONG variable type and use a 32 bit (ok, 31 plug a sign) variable type.

15. Did you find this post helpful? |
How do I define LONG variable?
What do I do with a situation where start is 10:00 and end is 09:00?
If using AM/PM sign, what do I do if start is at AM and stop is PM ?

16. Did you find this post helpful? |
Originally Posted by menta
How do I define LONG variable?
It's in your PBP manual...just like you define any other variable.

What do I do with a situation where start is 10:00 and end is 09:00?
If using AM/PM sign, what do I do if start is at AM and stop is PM ?
Time to start thinking a bit...
If it starts at AM, then the flag would equal AM AND the time would match.
If it starts at PM, then the flag would equal PM AND the time would match.
Doesn't matter if it starts or stops, it's all in how you set up your If/Then statement to match what YOU want to do.

17. Did you find this post helpful? |
From the HELP "Size is BIT, BYTE or WORD."
Does the 16F support 32bit ?

18. Did you find this post helpful? |
Originally Posted by menta
From the HELP "Size is BIT, BYTE or WORD."
Does the 16F support 32bit ?
Well, you never specified that you were using a 16Fxxx, so I guess you don't get to use a LONG variable.
And it's not so much if the 16F supports 32 bits, 'cause none of the 10F/12F/16F/18F support 32 bit variables. It's the compiler that either supports them or doesn't.

19. Did you find this post helpful? |
So can I use the 16F with long variables or not ?

20. Did you find this post helpful? |
Originally Posted by menta
So can I use the 16F with long variables or not ?
...........................
Well, you never specified that you were using a 16Fxxx, so I guess you don't get to use a LONG variable.

21. Did you find this post helpful? |
...it's not so much if the 16F supports 32 bits, 'cause none of the 10F/12F/16F/18F support 32 bit variables. It's the compiler that either supports them or doesn't.
If its not related to the PIC then why can't I ?
According to this http://www.microchip.com/stellent/id...cName=en532453
what does the 32 means if not supporting 32-bit ?

22. Did you find this post helpful? |
I got a solution from a friend:
This will cover all cases for start: (Same will be for end but with End values)

HH>StartHH ||
HH=StartHH & MM>StartMM ||
HH=StartHH & MM=StartMM & SS>=StartSS

Should Start programs.
The only problem is wrap around at 23:59:00 -> 00:00:00
But this will be handled with a special flag to identify change from PM->AM
Last edited by menta; - 3rd July 2008 at 17:20.

23. Did you find this post helpful? |
Well, you never specified that you were using a 16Fxxx, so I guess you don't get to use a LONG variable.
And it's not so much if the 16F supports 32 bits, 'cause none of the 10F/12F/16F/18F support 32 bit variables. It's the compiler that either supports them or doesn't.
Originally Posted by menta
If its not related to the PIC then why can't I ?
According to this http://www.microchip.com/stellent/id...cName=en532453
what does the 32 means if not supporting 32-bit ?
With any luck, this should clear it up a bit...
PBP is a COMPILER....MPASM is an ASSEMBLER.
PBP supports...PICxxxxx (see their website...way too many to list)
MPASM supports...well, basically, anything in the Microchip inventory.
The PIC32 is supported by MPASM, the assembler.
The PIC32 isn't supported by PicBasicPro, the compiler.
A program is written (source code), is compiled (by PBP) into assembly code (machine code), and assembled (by MPASM) into hex code (binary to be programmed) by the programmer (PICKIT2 for example).

24. Did you find this post helpful? |
once started or stopped, start looking for the next event starting with hour = then min = then second > as DT suggested earlier if then.....
same logic a human would use looking for an event

amgen

25. Did you find this post helpful? |
So where is it possible to use Long variables?

26. Did you find this post helpful? |
not needed, set up 1307 for 24 hr mode

27. Did you find this post helpful? |
Originally Posted by menta
So where is it possible to use Long variables?
PIC18Fxxxx and PBP...just like I said....just not so many words...

28. Did you find this post helpful? |
Originally Posted by menta
I got a solution from a friend:
HH>StartHH ||
HH=StartHH & MM>StartMM ||
HH=StartHH & MM=StartMM & SS>=StartSS
Looks like that will work (with some parenthesis added).

This should work for the rest of it ...
Code:
```TimeCmpFlags  VAR BYTE
PastStart   VAR TimeCmpFlags.0
PastStop    VAR TimeCmpFlags.1
NextDay     VAR TimeCmpFlags.2
ProgON      VAR TimeCmpFlags.3

CheckTimes:
TimeCmpFlags = 0  ; clear flags first

; if the Start and Stop times are the same, then Always OFF
if (Stop_H=Start_H) AND _
(Stop_M=Start_M) AND _
(Stop_S=Start_S) then AlwaysOFF

; is it past the Start time?
if (Hours>Start_H) OR _
(Hours=Start_H AND Minutes>Start_M) OR _
(Hours=Start_H AND Minutes=Start_M AND Seconds>=Start_S) then PastStart=1

; is it past the Stop time?
if (Hours>Stop_H) OR _
(Hours=Stop_H AND Minutes>Stop_M) OR _
(Hours=Stop_H AND Minutes=Stop_M AND Seconds>=Stop_S) then PastStop=1

; does the period end the following day?
if (Stop_H< Start_H) OR _
(Stop_H=Start_H AND Stop_M < Start_M) OR _
(Stop_H=Start_H AND Stop_M=Start_M AND Stop_S < Start_S) then NextDay=1

;---------------
if !NextDay then                               ; same day, use AND
if PastStart AND !PastStop then ProgON = 1
else                                           ; next day, use OR
IF PastStart OR !PastStop then ProgON = 1
endif

AlwaysOFF:
return```
Added: You should keep in touch with your "Friend". It was a good "Tip".
Last edited by Darrel Taylor; - 4th July 2008 at 01:57. Reason: Added:

29. Did you find this post helpful? |
Thanks,
But, there is still a problem when there are 2 programs with a gap between each other.
Second one will always try to make it off as the current time is not in its range, while the first one is on

30. Did you find this post helpful? |
2 programs? What?

31. Did you find this post helpful? |
yeah,
The device can have several programs
e.g
Start 13:00:00 End 13:30:00
Start 14:00:00 End 14:30:00
Start 17:00:00 End 17:30:00

32. Did you find this post helpful? |
You need to start telling us those things at the beginning.

If you change all the Start/Stop/Flag variables to arrays, it should work with multiple programs.
.

33. Did you find this post helpful? |
Can you detail more abit about the array ?
Thanks.

34. Did you find this post helpful? |
Sure,

If Stop_H was an array of bytes ...

Stop_H VAR BYTE[3]

Then you could access each different programmed period like this

Stop_H(Period)

Same thing for the other variables too.
<br>