Calculating Time for clock's preset


Closed Thread
Results 1 to 34 of 34
  1. #1
    Join Date
    Jun 2008
    Posts
    84

    Default 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. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by menta View Post
    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. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by menta View Post
    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>
    DT

  4. #4
    Join Date
    Jun 2008
    Posts
    84


    Did you find this post helpful? Yes | No

    Default

    Taylor, Do you mean calculate time from Mid or Noon ?

  5. #5
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by menta View Post
    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. #6
    Join Date
    Jun 2008
    Posts
    84


    Did you find this post helpful? Yes | No

    Default

    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. #7
    Join Date
    Jun 2008
    Posts
    84


    Did you find this post helpful? Yes | No

    Default

    If I didn't make it clear, a program can be:
    Start 13:10:02
    End 13:10:05
    HH:MM:SS

  8. #8
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    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
    DT

  9. #9
    Join Date
    Jun 2008
    Posts
    84


    Did you find this post helpful? Yes | No

    Default

    Thanks,
    The main risk is if for some reason you loose a check, then you have missed the program start time

  10. #10
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by menta View Post
    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. #11


    Did you find this post helpful? Yes | No

    Default

    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. #12
    Join Date
    Jun 2008
    Posts
    84


    Did you find this post helpful? Yes | No

    Default

    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. #13
    Join Date
    Jun 2008
    Posts
    84


    Did you find this post helpful? Yes | No

    Default

    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. #14
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by menta View Post
    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. #15
    Join Date
    Jun 2008
    Posts
    84


    Did you find this post helpful? Yes | No

    Default

    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. #16
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Cool

    Quote Originally Posted by menta View Post
    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. #17
    Join Date
    Jun 2008
    Posts
    84


    Did you find this post helpful? Yes | No

    Default

    From the HELP "Size is BIT, BYTE or WORD."
    Does the 16F support 32bit ?

  18. #18
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by menta View Post
    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. #19
    Join Date
    Jun 2008
    Posts
    84


    Did you find this post helpful? Yes | No

    Default

    So can I use the 16F with long variables or not ?

  20. #20
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by menta View Post
    So can I use the 16F with long variables or not ?
    ...........................
    Quote Originally Posted by skimask View Post
    Well, you never specified that you were using a 16Fxxx, so I guess you don't get to use a LONG variable.

  21. #21
    Join Date
    Jun 2008
    Posts
    84


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post
    ...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. #22
    Join Date
    Jun 2008
    Posts
    84


    Did you find this post helpful? Yes | No

    Default

    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. #23
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post
    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.
    Quote Originally Posted by menta View Post
    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. #24


    Did you find this post helpful? Yes | No

    Default

    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. #25
    Join Date
    Jun 2008
    Posts
    84


    Did you find this post helpful? Yes | No

    Default

    So where is it possible to use Long variables?

  26. #26


    Did you find this post helpful? Yes | No

    Default

    not needed, set up 1307 for 24 hr mode

  27. #27
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by menta View Post
    So where is it possible to use Long variables?
    PIC18Fxxxx and PBP...just like I said....just not so many words...

  28. #28
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Lightbulb

    Quote Originally Posted by menta View Post
    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:
    DT

  29. #29
    Join Date
    Jun 2008
    Posts
    84


    Did you find this post helpful? Yes | No

    Default

    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. #30
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    2 programs? What?
    DT

  31. #31
    Join Date
    Jun 2008
    Posts
    84


    Did you find this post helpful? Yes | No

    Default

    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. #32
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    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.
    .
    DT

  33. #33
    Join Date
    Jun 2008
    Posts
    84


    Did you find this post helpful? Yes | No

    Default

    Can you detail more abit about the array ?
    Thanks.

  34. #34
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    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>
    DT

Similar Threads

  1. I don't understand this code!
    By Russ Kincaid in forum mel PIC BASIC Pro
    Replies: 46
    Last Post: - 13th February 2008, 02:55
  2. Measuring time
    By AugustoPedrone in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 30th July 2007, 23:46
  3. Serout2/serin2 Pbp Problem
    By SOMRU in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 11th December 2006, 19:55
  4. Calculating elapsed time, how?
    By Eng4444 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 1st June 2006, 09:00
  5. Timer in real time
    By martarse in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 29th July 2005, 14:24

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts