Shop OBEX P1 Docs P2 Docs Learn Events
Daily timer — Parallax Forums

Daily timer

GrantmcFGrantmcF Posts: 30
edited 2012-10-23 21:15 in Propeller 1
I'm trying to schedule a cooler to come on and turn off at a set time of day. My code keeps blowing by my if statement and going to the else and turning it off. Can anyone give me a hint or point me at a good sample of code? The week day or weekend selection works ok. This is the section of code where I'm having the problem.


Pub Timer
 Time_total := ((hour*60)+minute)
 Wkdystrt_total := ((Wkdy_hrstart*60)+Wkdy_minstart)
 Wkdystp_total  := ((Wkdy_hrstop*60)+Wkdy_minstop)
 Wkndstrt_total := ((Wknd_hrstart*60)+Wknd_minstart)
 Wkndstp_total  := ((Wknd_hrstop*60)+Wknd_minstop)

 case dow
   1,7: Wknd_Timer
   2,3,4,5,6: Wkdy_Timer
    

Pub Wkdy_Timer
  WkdyT_status:= 1
  If (Time_total => Wkdystrt_total) 'and (Time_total <= Wkdystp_total)
    Control_logic         '(go to control logic)

  Else
    cogstop(Relay_cog~ -1)
    Relay_cog := cognew( Cooler_off,@ Relay_Stack)+1
    Loop 


Pub Wknd_Timer
  WkndT_status:= 1
  If (Time_total => Wkndstrt_total) and (Time_total <= Wkndstp_total) 
    Control_logic         '(go to control logic)

  Else
    cogstop(Relay_cog~ -1)
    Relay_cog := cognew( Cooler_off,@ Relay_Stack)+1
    Loop 


Comments

  • doggiedocdoggiedoc Posts: 2,243
    edited 2012-10-19 15:37
    I think you are using the assignment form of 'less than/equal to' I never can remember and have to look it up.
    WkndT_status:= 1
      If (Time_total => Wkndstrt_total) and (Time_total <= Wkndstp_total) 
        Control_logic         '(go to control logic)
    
    Should be:
    WkndT_status:= 1
      If (Time_total => Wkndstrt_total) and (Time_total [COLOR="#FF0000"]=<[/COLOR] Wkndstp_total) 
        Control_logic         '(go to control logic)
    
  • Ron CzapalaRon Czapala Posts: 2,418
    edited 2012-10-19 15:38
    You might to use => and =< for comparison, >= and <= are assignment operators - see page 43 of manual.
  • GrantmcFGrantmcF Posts: 30
    edited 2012-10-21 17:39
    Thanks, that was driving me nuts. I hadn't even considered that it would mean something different being on the other side of the =.
  • SRLMSRLM Posts: 5,045
    edited 2012-10-21 18:51
    GrantmcF wrote: »
    Thanks, that was driving me nuts. I hadn't even considered that it would mean something different being on the other side of the =.

    That's why nowdays I never use the "less/greater than or equal to" operator, I just use the "less/greater than" operator. It makes it easier on my head, and I don't have to worry about it.

    If you must think about it, then the easiest way (for me) is to remember things like "+=" and "-=", where the operator is on the left side, and the assignment is on the right.
  • GrantmcFGrantmcF Posts: 30
    edited 2012-10-22 18:00
    So, I have corrected the operators and tested the code and found another issue. How do you account for a stop time after midnight? The code works fine as long as the start and stop times are in the same day. If you set a start time of 10pm and a stop time of 7am, it will not start because the time is not less than the start time. How can you roll it over into a new day?
    
    Pub Timer
        Lcd.init(Lcd_Pin, 19200,4)
    '   Lcd.backlight(true)                                   'turn backlight on
        Lcd.cursor(0)                                         'turn cursor off
        Lcd.cls                                               'Clear screen, move cursor to 0,0                
        Lcd.str(string("Timer test"))                      'output a startup message
        waitcnt(clkfreq * 5 + cnt)
        
        WkdyT_status:= 0                                    
        WkndT_status:= 0                   
        dow := 1                          'day of week 1-7
        Time_total := 631 '1031am                 '0-1440 ((hour*60)+minute)
        Wkdystrt_total := 630             '((Wkdy_hrstart*60)+Wkdy_minstart)
        Wkdystp_total  := 635             '((Wkdy_hrstop*60)+Wkdy_minstop)
        Wkndstrt_total := 630             '((Wknd_hrstart*60)+Wknd_minstart)
        Wkndstp_total  := 635             '((Wknd_hrstop*60)+Wknd_minstop)
    
      repeat
        Lcd.cls
        Lcd.gotoxy(0,0)
        Lcd.str(string("Wkdy Timer "))
        Lcd.decx(WkdyT_status,2)
        Lcd.gotoxy(0,1)
        Lcd.str(string("Wknd Timer "))
        Lcd.decx(WkndT_status,2)
    
        Lcd.gotoxy(0,2)
        Lcd.str(string("Timer Works "))
        Lcd.decx(Timer_works,2)
        Lcd.gotoxy(0,3)
        Lcd.str(string("Timer Sucks "))
        Lcd.decx(Timer_sucks,2)
        waitcnt(clkfreq*3 + cnt)
    
        Lcd.cls    
        Lcd.gotoxy (0,0)
        Lcd.str(string("Time Total "))
        Lcd.decx(Time_total,4)
        Lcd.gotoxy(0,1)
        Lcd.str(string("WkdyStart T "))
        Lcd.decx(Wkdystrt_total,4)
        Lcd.gotoxy(0,2)
        Lcd.str(string("WkdyStop T "))
        Lcd.decx(Wkdystp_total,4)    
        waitcnt(clkfreq*3 + cnt)
    
        Lcd.cls    
        Lcd.gotoxy (0,0)
        Lcd.str(string("Time Total "))
        Lcd.decx(Time_total,4)
        Lcd.gotoxy(0,1)
        Lcd.str(string("WkndStart T "))
        Lcd.decx(Wkndstrt_total,4)
        Lcd.gotoxy(0,2)
        Lcd.str(string("WkndStop T "))
        Lcd.decx(Wkndstp_total,4)    
        waitcnt(clkfreq*3 + cnt)
            
     
        case dow
          1,7: Wknd_Timer
          2,3,4,5,6: Wkdy_Timer
    
        
    
    Pub Wkdy_Timer
      WkdyT_status:= 1
      If (Time_total => Wkdystrt_total)and (Time_total =< Wkdystp_total)
        Timer_works :=1
        cogstop(Relay_cog~ -1)
        Relay_cog := cognew( Wet_high,@ Relay_Stack)+1        
    
      Else
        Timer_sucks :=1
        cogstop(Relay_cog~ -1)
        Relay_cog := cognew( Dry_high,@ Relay_Stack)+1
        
    
    
    Pub Wknd_Timer
      WkndT_status:= 1
      If (Time_total => Wkndstrt_total) and (Time_total =< Wkndstp_total)
        Timer_works :=1 
        cogstop(Relay_cog~ -1)
        Relay_cog := cognew( Wet_low,@ Relay_Stack)+1 
    
      Else
        Timer_sucks :=1
        cogstop(Relay_cog~ -1)
        Relay_cog := cognew( Dry_low,@ Relay_Stack)+1
    
    
  • SRLMSRLM Posts: 5,045
    edited 2012-10-22 18:28
    You should comment your code, or choose more descriptive variable names...

    Anyway, it looks like you are stopping and starting the Wet/Dry_high/low cog every 9 seconds (every time through the loop)? Why the constant restarting and starting? There is also a bug: the first time through the loop, when Relay_cog = 0, it tries to stop cog -1.

    For your stop time before start time issue, you would need to add another case (or condition) to your if statements. Something like:
    if (wkdystrt_total > wkdystp_total AND (time_total >wkdystrt_total OR time_total<wkdystop_total) )
        Timer_works = 1
        ...
    
  • GrantmcFGrantmcF Posts: 30
    edited 2012-10-23 21:15
    Thanks, SLRM. That was just what I needed. The reason it cycles so much, is that this method was just written to trouble shoot the timer. The cog stop logic makes more sense when seen in the full context
Sign In or Register to comment.