PDA

View Full Version : Calculating Time for clock's preset



menta
- 1st July 2008, 18:03
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.

skimask
- 1st July 2008, 18:16
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.

Darrel Taylor
- 1st July 2008, 20:13
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 ...
IF Hours = 13 THEN
ProgON = 1
ELSE
ProgON = 0
ENDIF

But I'm guessing you want more than the example showed.
<br>

menta
- 1st July 2008, 21:26
Taylor, Do you mean calculate time from Mid or Noon ?

skimask
- 1st July 2008, 21:31
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...

menta
- 1st July 2008, 21:36
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

menta
- 1st July 2008, 21:38
If I didn't make it clear, a program can be:
Start 13:10:02
End 13:10:05
HH:MM:SS

Darrel Taylor
- 1st July 2008, 22:17
All those little details help.

Here's another possibility.
Run it once per second. Probably a gosub from the clock's 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

menta
- 1st July 2008, 22:53
Thanks,
The main risk is if for some reason you loose a check, then you have missed the program start time

skimask
- 2nd July 2008, 00:49
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...

amgen
- 2nd July 2008, 12:23
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

menta
- 2nd July 2008, 19:02
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.

menta
- 2nd July 2008, 19:18
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.

skimask
- 2nd July 2008, 19:19
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...


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.

menta
- 2nd July 2008, 21:44
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 ?

skimask
- 2nd July 2008, 21:48
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.

menta
- 2nd July 2008, 22:12
From the HELP "Size is BIT, BYTE or WORD."
Does the 16F support 32bit ?

skimask
- 3rd July 2008, 03:34
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.

menta
- 3rd July 2008, 07:46
So can I use the 16F with long variables or not ?

skimask
- 3rd July 2008, 13:38
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.

menta
- 3rd July 2008, 15:37
...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/idcplg?IdcService=SS_GET_PAGE&nodeId=2615&dDocName=en532453
what does the 32 means if not supporting 32-bit ?

menta
- 3rd July 2008, 16:33
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

skimask
- 3rd July 2008, 17:26
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.


If its not related to the PIC then why can't I ?
According to this http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=2615&dDocName=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).

amgen
- 3rd July 2008, 17:44
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

menta
- 3rd July 2008, 18:54
So where is it possible to use Long variables?

amgen
- 3rd July 2008, 19:03
not needed, set up 1307 for 24 hr mode

skimask
- 3rd July 2008, 19:04
So where is it possible to use Long variables?
PIC18Fxxxx and PBP...just like I said....just not so many words...

Darrel Taylor
- 3rd July 2008, 20:53
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 ...
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".

menta
- 4th July 2008, 14:10
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

Darrel Taylor
- 4th July 2008, 19:54
2 programs? What?

menta
- 4th July 2008, 20:46
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

Darrel Taylor
- 4th July 2008, 21:12
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.
.

menta
- 4th July 2008, 21:29
Can you detail more abit about the array ?
Thanks.

Darrel Taylor
- 5th July 2008, 06:09
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>