Shop OBEX P1 Docs P2 Docs Learn Events
Repeat problem... — Parallax Forums

Repeat problem...

jvrproductionsjvrproductions Posts: 61
edited 2012-02-07 19:22 in Propeller 1
Hello and thank for any help. I have the following code. The problem is that for some reason went current its = to target i want outa 3 and 4 to go off wait 60 seconds and next outa 5 need to go off. but instead the program wait about 6 sec and put off all at the same time...
if ac ==  2    

         if current > target
             repeat 1
               outa[5]  := %1                                ' fan on
               outa[4]  := %0
               waitcnt (clkfreq * 20 + cnt)
           outa[3]  := %1                                    ' a/c on 
             
         elseif current == target
             repeat 1
               outa[3]  := %0                                 
               outa[4]  := %0
               waitcnt (clkfreq * 60 + cnt) 
           outa[5]  := %0    
            
          
         elseif current < target
          outa[5]  := %0                                
          outa[3]  := %0                                
          outa[4]  := %0

Thanks

Comments

  • ElectricAyeElectricAye Posts: 4,561
    edited 2012-02-04 09:19
    waitcnt wraps around about every 53 seconds or so. Read about it in the Propeller manual and you'll see what I mean. I'm guessing that's what your problem is, but we would probably need to see your entire code to see what's happening.

    From the Prop manual, page 219 version 1.1:
    IMPORTANT: Since WAITCNT pauses the cog until the System Counter matches the given value, care must be taken to ensure that the given value was not already surpassed by the System Counter. If the System Counter already passed the given value before the wait hardware activated then the cog will appear to have halted permanently when, in fact, it is waiting for the counter to exceed 32 bits and wrap around to the given value. Even at 80 MHz, it takes over 53 seconds for the 32-bit System Counter to wrap around!

    In a sense, I think you're encountering something like the opposite of what that warning pertains to. You're hitting that system clock value before the 53 seconds wrap around has happened.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-02-04 09:34
    I agree with ElectricAye,

    You're using too large a wait period. You could break it into two 30 second waits or you could use one of the clock objects to keep track of time. (I personally think two waits would be easier in this case.)
  • MagIO2MagIO2 Posts: 2,243
    edited 2012-02-04 09:40
    But your really have to explain to me what's the deeper meaning of a

    repeat 1

    ;o)

    If you have more than one time where you need to wait > 53 seconds you could create a function which waits for seconds:
    PUB waits( seconds ) : counter
      counter := cnt
      repeat seconds
        waitcnt( counter+=clkfreq )
    
  • jvrproductionsjvrproductions Posts: 61
    edited 2012-02-04 09:44
    Thank for the help. Here is the complete code (It's no finish and its only one PUB) I changed to waitcnt (clkfreq + cnt) and i still having the same problem. Also the 1st if "if current > target" work fine.. outa 5 go on wait 1 sec and outa 3 go on. The problem is only on current == target
    PUB controles
    
    
        dira[3..5]  := %111
    
    
    
    
       
        repeat
          if fan == 1                                       'fam on
             outa[5]  := %1
    
    
          else                                              'fam auto of
            outa[5]  := %0
    
    
     
     
          if ac ==  2                                       
    
    
             if current > target
                 repeat 1
                   outa[5]  := %1                           ' fan on  wait 10 sec 
                   outa[4]  := %0
                   waitcnt (clkfreq  + cnt)
               outa[3]  := %1                               ' a/c on 
                 
             elseif current == target
                 repeat 1
                   outa[3]  := %0                           ' check heat off, a/c compressor go off      
                   outa[4]  := %0
                   waitcnt (clkfreq  + cnt)                 ' wait 10 sec (HERE IS THE PROBLEM)
               outa[5]  := %0    
                                                            'fan go off
              
             elseif current < target
              outa[5]  := %0                                
              outa[3]  := %0                                
              outa[4]  := %0 
              
          
          if ac ==  3         ' heat on
               outa[4]  := %1
               outa[5]  := %1   
               outa[3]  := %0
    
    
    
    
          if ac == 4           ' heat emerg
               outa[4]  := %1
               outa[5]  := %1   
               outa[3]  := %0
    
    
          
          if ac ==  1         ' heat on
               outa[4]  := %0
               outa[5]  := %0   
               outa[3]  := %0
        
         
    
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-02-04 09:55
    Are you sure the program moves to the "current == target" section?

    Can current jump past the target value? I'm just noticing that 3 through 5 all get turned off at the same time if "current < target".

    Could the problem be in a different area of code? (i.e. where the value of current is set)
  • ElectricAyeElectricAye Posts: 4,561
    edited 2012-02-04 10:05
    Duane Degn wrote: »
    ...

    Could the problem be in a different area of code? (i.e. where the value of current is set)

    Are the values of current and target in integer form? Sometimes people forget that the > and < operators only work for integers and they try to do comparisons with real numbers, etc.
  • jvrproductionsjvrproductions Posts: 61
    edited 2012-02-04 10:17
    i will double check (It's possible because i and pretty new programing...... :-) ). But i just change the wait time to one second and the repeat to 10 (To get the 10 Seconds...) And now it's working but with a delay..... Can be a stack problem?

    [video=youtube_share;toYoGafc-lA]
  • jvrproductionsjvrproductions Posts: 61
    edited 2012-02-04 13:42
    ..... and the repeat to 10 (To get the 10 Seconds...) And now it's working but with a delay..... Can be a stack problem?
    ..... No JV It's not an stack problem..... You have a repeat. If you have a repeat you need to wait for the repeat to finish..... What st... question you did.... :-)
  • lardomlardom Posts: 1,659
    edited 2012-02-04 17:17
    It looks like he wants a temperature control. His three tests are "IF current>target, elseif current == target, elseif current<target" His pin assignments are pin[3]=heat. pin[4]=ac and pin[5]=fan. His repeat loop has three tests. I'm not clear on the 60 second waitcnt. His repeat loop only needs to execute one of those three tests. The fan has to come on first and stop last.
  • JonnyMacJonnyMac Posts: 9,197
    edited 2012-02-07 09:37
    This is a case where the case structure will make the code easier to follow and troubleshoot. I also suggest using named pin constants as an aid to readability. Why type this:
    outa[5] := 1    ' fan is on
    



    ...when
    outa[FAN_CTRL] := IS_ON
    


    ... is so much more obvious -- and doesn't require comments. CONstants are your friends! ;)


    Here's a stab at reformatting your method to make it more readable -- without constant names as I don't know what all pins do.
    PUB controles  
    
      dira[3..5]  := %111
    
      repeat
        if (fan)
          outa[5]  := 1
        else
          outa[5]  := 0
    
          
        case ac
          2:
            if (current > target)
              outa[5] := 1 
              outa[4] := 0 
              waitcnt(cnt + (10 * clkfreq))
              outa[3] := 1
       
            elseif (current == target)
              outa[5] := 0 
              outa[4] := 0 
              waitcnt(cnt + (10 * clkfreq))
              outa[3] := 0
    
            else
              outa[5]:= %0                                
              outa[3]:= %0                                
              outa[4]:= %0 
       
          3:                                                        ' heat on
            outa[4] := %1
            outa[5] := %1   
            outa[3] := %0
    
          4:                                                        ' heat emerg
            outa[4] := %1
            outa[5] := %1   
            outa[3] := %0
    
          1:                                                        ' heat on
            outa[4] := %0
            outa[5] := %0   
            outa[3] := %0
    
  • jvrproductionsjvrproductions Posts: 61
    edited 2012-02-07 19:22
    JonnyMac wrote: »
    This is a case where the case structure will make the code easier to follow and troubleshoot. I also suggest using named pin constants as an aid to readability. Why type this:
    outa[5] := 1    ' fan is on
    
    thanks jonny i will start updating my code....


    ...when
    outa[FAN_CTRL] := IS_ON
    


    ... is so much more obvious -- and doesn't require comments. CONstants are your friends! ;)


    Here's a stab at reformatting your method to make it more readable -- without constant names as I don't know what all pins do.
    PUB controles  
    
      dira[3..5]  := %111
    
      repeat
        if (fan)
          outa[5]  := 1
        else
          outa[5]  := 0
    
          
        case ac
          2:
            if (current > target)
              outa[5] := 1 
              outa[4] := 0 
              waitcnt(cnt + (10 * clkfreq))
              outa[3] := 1
       
            elseif (current == target)
              outa[5] := 0 
              outa[4] := 0 
              waitcnt(cnt + (10 * clkfreq))
              outa[3] := 0
    
            else
              outa[5]:= %0                                
              outa[3]:= %0                                
              outa[4]:= %0 
       
          3:                                                        ' heat on
            outa[4] := %1
            outa[5] := %1   
            outa[3] := %0
    
          4:                                                        ' heat emerg
            outa[4] := %1
            outa[5] := %1   
            outa[3] := %0
    
          1:                                                        ' heat on
            outa[4] := %0
            outa[5] := %0   
            outa[3] := %0
    
Sign In or Register to comment.