Time comparison


Closed Thread
Results 1 to 21 of 21

Thread: Time comparison

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default

    That looks pretty good Malc.
    And nope, you don't need arrays for the flag bits.

    I don't think you should have anything in the AlwaysOFF section, since the CheckTimes routine just determines if it's inside the program period or not.
    With multiple time periods, whatever is in AlwaysOFF would happen to both periods if only triggered in one of them.
    Any actions can be done after returning from the subroutine.

    Something like this in the main loop maybe.
    Code:
        fn = 0                              ; select the first Lights
        GOSUB CheckTimes                    ; compare the programed period
        IF ProgON THEN                      ; If in the program period
            IF Lights1 = 0 THEN Lights1 = 1 ; Turn light on (if off)
        ELSE
            IF Lights1 = 1 THEN Lights1 = 0
        ENDIF
    ;   -----------------------------------
        fn = 1                              ; select the second Lights
        GOSUB CheckTimes                    ; compare the programed period
        IF ProgON THEN
            IF Lights2 = 0 THEN Lights2 = 1
        ELSE
            IF Lights2 = 1 THEN Lights2 = 0
        ENDIF
    DT

  2. #2
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Thanks Darrel,

    Hopefully the LCD will arrive soon and I'll be able to test this on the development board

  3. #3
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Darrel Taylor View Post
    That looks pretty good Malc.
    And nope, you don't need arrays for the flag bits.

    I don't think you should have anything in the AlwaysOFF section, since the CheckTimes routine just determines if it's inside the program period or not.
    With multiple time periods, whatever is in AlwaysOFF would happen to both periods if only triggered in one of them.
    Any actions can be done after returning from the subroutine.

    Something like this in the main loop maybe.
    Code:
        fn = 0                              ; select the first Lights
        GOSUB CheckTimes                    ; compare the programed period
        IF ProgON THEN                      ; If in the program period
            IF Lights1 = 0 THEN Lights1 = 1 ; Turn light on (if off)
        ELSE
            IF Lights1 = 1 THEN Lights1 = 0
        ENDIF
    ;   -----------------------------------
        fn = 1                              ; select the second Lights
        GOSUB CheckTimes                    ; compare the programed period
        IF ProgON THEN
            IF Lights2 = 0 THEN Lights2 = 1
        ELSE
            IF Lights2 = 1 THEN Lights2 = 0
        ENDIF

    Darrel,

    Just ripped the LCD out of the unit so I could test this. So far it seems to work apart from one strange thing, the timings are a minute out... If I set the lighting period to come on at 15:00 it comes on at 15:01, and set to off at 15:05 it goes of 15:06, and I don't get anything displayed on the LCD

    Here's the code that turns the lights on or off:

    Code:
        fn = 1                              ; select the second Lights
        GOSUB CheckTimes                    ; compare the programed period
        IF ProgON THEN
            IF Lights2 = 0 THEN Lights2 = 1
        LCDOut $FE, $94+9,"Light 2 ON"
        ELSE
            IF Lights2 = 1 THEN Lights2 = 0
         LCDOut $FE, $94+9,"          "
         ENDIF
        if Lights1 = 1 and Lights2 = 1 then 
        LCDOut $FE, $94+9,"Lights ON"
        ELSE
         LCDOut $FE, $94+9,"          "
         Endif
    then there's the checking routine:

    Code:
    CheckTimes:
    TimeCmpFlags = 0  ; clear flags first
    
         ; if the Start and Stop times are the same, then Always OFF 
      if (lightsetHR[fn]=timeH) AND _       
         (lightsetMN[fn]=timeM) then AlwaysOFF                      
    
         ; is it past the Start time?
      if (timeH>lightsetHR[fn]) OR _              
         (timeH=lightsetHR[fn] AND timeM>lightsetMN[fn])then PastStart=1
    
         ; is it past the Stop time?
      if (timeH>lightoffHR[fn]) OR _                
         (timeH=lightoffHR[fn] AND timeM>lightoffMN[fn])then PastStop=1
    
         ; does the period end the following day?
      if (lightoffHR[fn]< lightsetHR[fn]) OR _           
         (lightoffHR[fn]=lightsetHR[fn] AND lightoffMN[fn] < lightsetMN[fn]) then NextDay[fn]=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
    And finally the section where the time is set:

    Code:
    Lighting:
    
    Lighton:
    Lcdout $FE,2
    LCDOUT $FE,$80,"Set On Time For "
        IF H_butt = 0 THEN GOSUB IncHours
        IF M_butt = 0 THEN GOSUB IncMinutes
        lightsetHR[fn]=Hours
        lightsetMN[fn]=Minutes
        if viv >=2 then viv =2
        LCDOUT $FE,$C0,"Light Circuit ",#viv
        lcdout $FE,$94,#lightsetHR[fn] DIG 1,#lightsetHR[fn] DIG 0,":",#lightsetMN[fn] DIG 1,#lightsetMN[fn] DIG 0
    pause 200
    If S_butt = 0 then
    pause 250 
    fn = fn +1
    viv = viv+1
    hours = 0
    minutes = 0
    
    endif
    If fn > 1 then 
    LCDOUT $FE,1
    viv=1
    fn=0
    goto lightoff
    endif
    goto lighton
    
    lightoff:
    Lcdout $FE,2
    LCDOUT $FE,$80,"Set Off Time For "
        IF H_butt = 0 THEN GOSUB IncHours
        IF M_butt = 0 THEN GOSUB IncMinutes
        lightoffHR[fn] = Hours
        lightoffMN[fn] = Minutes
        if viv >=2 then viv =2
        LCDOUT $FE,$C0,"Light Circuit ",#viv
        lcdout $FE,$94,#lightoffHR[fn] DIG 1,#lightoffHR[fn] DIG 0,":",#lightoffMN[fn] DIG 1,#lightoffMN[fn] DIG 0
    pause 200
    If !S_butt then 
    pause 250
    fn = fn +1
    viv = viv +1
    hours = 0
    minutes = 0
    endif
    If fn >=2 then 
    LCDOUT $FE,1
    goto mainmenu
    endif
    goto lightoff

    The subs for inc hrs or mins:
    Code:
    IncHours:
        Hours = Hours + 1
        IF Hours = 24 THEN Hours = 0
        
        pause 250
    RETURN
    
    IncMinutes:
        Minutes = Minutes + 1
        IF Minutes = 60 THEN Minutes = 0
        
        pause 250
    RETURN
    Any ideas....??

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


    Did you find this post helpful? Yes | No

    Default

    Malcolm,

    In the original CheckTimes: routine, the Seconds test had a >=.
    That way it's TRUE when the exact second rolls around.

    Modified for Minutes as the smallest period, I think the Minutes test now needs the >=

    Code:
    CheckTimes:
    TimeCmpFlags = 0  ; clear flags first
    
         ; if the Start and Stop times are the same, then Always OFF 
      if (lightsetHR[fn]=timeH) AND _       
         (lightsetMN[fn]=timeM) then AlwaysOFF                      
    
         ; is it past the Start time?
      if (timeH>lightsetHR[fn]) OR _              
         (timeH=lightsetHR[fn] AND timeM >= lightsetMN[fn])then PastStart=1
    
         ; is it past the Stop time?
      if (timeH>lightoffHR[fn]) OR _                
         (timeH=lightoffHR[fn] AND timeM >= lightoffMN[fn])then PastStop=1
    
         ; does the period end the following day?
      if (lightoffHR[fn]< lightsetHR[fn]) OR _           
         (lightoffHR[fn]=lightsetHR[fn] AND lightoffMN[fn] < lightsetMN[fn]) then NextDay[fn]=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
    P.S. There's still an Array index in your NextDay flag.
    Last edited by Darrel Taylor; - 26th July 2010 at 22:34. Reason: P.S.
    DT

  5. #5
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Thanks Darrel,

    I'll give it a try in the morning

  6. #6
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Darrel,

    Changed the code and it's still not functioning right.

    If I set the on time as 15:00 and off time as 15:01 the led comes on at 15:01 and stays on... well past the programmed stop time... any ideas ?

    Code:
    CheckTimes:
    TimeCmpFlags = 0  ; clear flags first
    
         ; if the Start and Stop times are the same, then Always OFF 
      if (lightsetHR[fn]=timeH) AND _       
         (lightsetMN[fn]=timeM) then AlwaysOFF                      
    
         ; is it past the Start time?
      if (timeH>lightsetHR[fn]) OR _              
         (timeH=lightsetHR[fn] AND timeM >= lightsetMN[fn])then PastStart=1
    
         ; is it past the Stop time?
      if (timeH>lightoffHR[fn]) OR _                
         (timeH=lightoffHR[fn] AND timeM >= lightoffMN[fn])then PastStop=1
    
         ; does the period end the following day?
      if (lightoffHR[fn]< lightsetHR[fn]) OR _           
         (lightoffHR[fn]=lightsetHR[fn] AND lightoffMN[fn] < lightsetMN[fn]) 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
    Last edited by malc-c; - 27th July 2010 at 12:18. Reason: code added

  7. #7
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default back to the drawing board

    OK something has really got screwed up....

    Here's the menu routine which is used to set the times for the lighting

    Code:
    Lighting:
    
    Lighton:
    Lcdout $FE,2
    LCDOUT $FE,$80,"Set On Time For "
        IF H_butt = 0 THEN GOSUB IncHours
        IF M_butt = 0 THEN GOSUB IncMinutes
        lightsetHR[fn]=Hours
        lightsetMN[fn]=Minutes
        if viv >=2 then viv =2
        LCDOUT $FE,$C0,"Light Circuit ",#viv
        lcdout $FE,$94,#lightsetHR[fn] DIG 1,#lightsetHR[fn] DIG 0,":",#lightsetMN[fn] DIG 1,#lightsetMN[fn] DIG 0
    pause 200
    If S_butt = 0 then
    pause 250 
    fn = fn +1
    viv = viv+1
    hours = 0
    minutes = 0
    
    endif
    If fn > 1 then 
    LCDOUT $FE,1
    viv=1
    fn=0
    goto lightoff
    endif
    goto lighton
    
    lightoff:
    Lcdout $FE,2
    LCDOUT $FE,$80,"Set Off Time For "
        IF H_butt = 0 THEN GOSUB IncHours
        IF M_butt = 0 THEN GOSUB IncMinutes
        lightoffHR[fn] = Hours
        lightoffMN[fn] = Minutes
        if viv >=2 then viv =2
        LCDOUT $FE,$C0,"Light Circuit ",#viv
        lcdout $FE,$94,#lightoffHR[fn] DIG 1,#lightoffHR[fn] DIG 0,":",#lightoffMN[fn] DIG 1,#lightoffMN[fn] DIG 0
    pause 200
    If !S_butt then 
    pause 250
    fn = fn +1
    viv = viv +1
    hours = 0
    minutes = 0
    endif
    If fn >=2 then 
    LCDOUT $FE,1
    goto mainmenu
    endif
    goto lightoff
    so I added the following lines to display the times on the LCD when the program is running
    Code:
    lcdout $FE,$D4+8,#lightsetHR[fn] DIG 1,#lightsetHR[fn] DIG 0,":",#lightsetMN[fn] DIG 1,#lightsetMN[fn] DIG 0
    lcdout $FE,$D4+14,#lightoffHR[fn] DIG 1,#lightoffHR[fn] DIG 0,":",#lightoffMN[fn] DIG 1,#lightoffMN[fn] DIG 0
    setting the time on to 00:00 and the off time to 00:01 the LCD displays 00:50 and 01:00 respectively. So obviously DT's routine will never match the times set..... I don't know why this has happened because in the original code it matched perfectly....

  8. #8
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default pulling my hair out

    OK as I couldn't figure the above out I started from scratch (again) and tried this:

    Code:
    For fn = 0 to 1
    
    If lightsetHR[fn]<timeH or lightsetHR[fn]>timeH and lightsetMN[fn]<timeM or lightsetMN[fn]>timeM and lightoffHR[fn]<timeH OR lightoffHR[fn]>timeH and lightoffMN[fn]<timeM OR lightoffMN[fn]>timeM Then
            LCDOut $FE, $94+9,"          "
            if fn=0 then Lights1 = 0
            if fn=1 then Lights2 = 0 
    endif
      
    If lightsetHR[fn]=timeH and lightsetMN[fn]=timeM then         'compare value of each set time to current time and if matches
        if fn=0 then Lights1 = 1
        if fn=1 then Lights2 = 1                              'turn on those light
        LCDOut $FE, $94+9,"Lights ON"
    endif
    
    next
    
    fOR fn=0 to 3 
    
    If StartHour[fn]<timeH or StartHour[fn]>timeH and StartMin[fn]<timeM or StartMin[fn]>timeM and StopHour[fn]<timeH OR StopHour[fn]>timeH and StopMin[fn]<timeM OR StopMin[fn]>timeM then          'compare value of each start time to current time and if matches
       LCDOut $FE, $94,"       "                     
       SetPoints[fn]=normtemp[fn] 
    endif
       
    If StartHour[fn]=timeH and StartMin[fn]=timeM then         'compare value of each start time to current time and if matches
       SetPoints[fn]=Droptemp[fn]                              'change the corresponding set point to the drop temperature
       LCDOut $FE, $94,"Night"
    endif
    
    if StopHour[fn]=timeH and StopMin[fn]=timeM then           'compare value of each stop time to current time and if matches
       LCDOut $FE, $94,"       "                     
       SetPoints[fn]=normtemp[fn]                             'change value back to normal temperature
    Endif
    next fn
    Which works, apart from the original problem, in that at midnight the lights come on and stay on, and the temperatures drop to the default drop temperature. My guess is that as both start times and stop times = 00:00 this is what's causing the issue.

    Anyone able to help come up with a suitable working snippet of code, or tell me why it doesn't work correctly when using DTs code ?

Members who have read this thread : 0

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