Shop OBEX P1 Docs P2 Docs Learn Events
Bug - help request — Parallax Forums

Bug - help request

g3cwig3cwi Posts: 262
edited 2012-02-25 12:24 in Propeller 1
The following code is a lap counter that looks for a reedswitch being triggered. It mostly works but never get to the very end line which indicates the race is over. Any ideas?
PUB Count | FLAG_C, Lap


    dira [LED]~~    'Set the LED port as output
    dira [SWITCH]~  'Set SWITCH port as input

    
  
    Lap:=0    'Initialise Lap Counter
   
      
      Repeat until Lap == 6
            
        FLAG_C:=0 'Initialise Flag_C  
        Repeat Until Flag_C > 20   'Debounce transition
          If ina[SWITCH] == 0
            FLAG_C += 1 'Increment Flag Counter
           
          Else
            FLAG_C:=0 'Reset while bouncing
           
            
        Lap += 1

        
            
        FLAG_C:=0 'Initialise Flag_C     
        Repeat Until Flag_C > 20   'Debounce transition
          If ina[SWITCH] == 1
            FLAG_C += 1 'Increment Flag Counter
          Else
            FLAG_C:=0  'Reset while bouncing

             

outa[LED]~~ 'Race end


Thanks.

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-02-25 05:57
    One problem is the code resets the count if an off state is encounted. I personally think it would be easier to make sure the switch has been off long enough before counting an on as "on".

    I don't want to spoil you fun so I want to warn you.

    *** Spoiler Alert ***

    Here's my attempt.
    PUB Count | Lap, previousState, offCount
    
      dira [LED]~~    'Set the LED port as output
      dira [SWITCH]~  'Set SWITCH port as input
       
      
      Lap := 0    'Initialise Lap Counter
      previousState := _OffState
      offCount := 0  
      Repeat until Lap == 6
        if ina[SWITCH] == 0
          if previousState == _OffState and offCount => _OffThreshold
            Lap++        
          previousState := _OnState
          offCount := 0  
        else
          offCount++
          previousState := _OffState
      outa[LED]~~ 'Race end
      
    CON
      _OffThreshold = 200
      #0, _OffState, _OnState 
    

    Edit: You'd want to adjust the value of "_OffThreshold" where it's sort enough to still register counts at fast rotations but not so short it will count a bouncing switch.
  • MagIO2MagIO2 Posts: 2,243
    edited 2012-02-25 06:37
    I think the biggest problem in the original code is, that the program completely ends, which stops the COG an makes all pins an input again.

    So, to keep your LED switched on you need an endless repeat loop at the very end.

    Sorry Duane, but I think your code is not much better in this sense ;o)
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-02-25 06:51
    MagIO2 wrote: »
    Sorry Duane, but I think your code is not much better in this sense ;o)

    You're always welcome to critic my code (I consider it a favor) but in this case, I assumed this wasn't the whole program. I figured flow would return to the calling method.

    If this is the only method, then there would need to be a repeat added after the outa statement.

    I didn't compile the above method since it used constants (and I assumed methods) not declared in the posted code.

    I personally like to add a serial debug to programs I'm having trouble with. You can have the debug running in a separate cog so it doesn't interfere with the timing. Even if the debug is in the same cog with debug statements inline with the code, you could rotate the table slowly to compensate for the debug statements to see if the branching of the code is working as you expect it to. The debug statements can be removed as you feel more confident with the code.
  • Mike GMike G Posts: 2,702
    edited 2012-02-25 07:02
    g3cwi, as MagIO2 stated an infinite repeat loop is needed. I'd create a lap counter that runs in its own COG. It's whole purpose is life is to count laps and place the lap count in memory. The main program also loops forever and its job is to interpret the laps. For example, if the lap count is 15 then 15/6 = 2; 15 // 6 = 3; 2 races occurred and we're 3 laps into the 3rd race. Light the LEDs accordingly.
  • g3cwig3cwi Posts: 262
    edited 2012-02-25 07:56
    Hi

    Thanks for the responses. The answer was that addition of a repeat worked perfectly. This cog can then be shut down once its state has been reported. I transfer the end of race flag to the main program simply by looking at the output pin of the Lap counter cog.

    I am very happy with the program. It makes use of several cogs to do different tasks and really does show the flexibility of the Propellor. It's rather cool.

    Thanks again.

    Richard
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-02-25 08:40
    Richard,

    I'm concerned about a single off bouce causing a lap to be missed.
    Repeat Until Flag_C > 20   'Debounce transition
              If ina[SWITCH] == 0
                FLAG_C += 1 'Increment Flag Counter
               
              [B]Else
                FLAG_C:=0 'Reset while bouncing
    [/B]
    

    So if the reed switch bouces it will cause "FLAG_C" to be reset. I'd think this could cause a lap to be missed if the switch bouces.

    I don't see a check to make sure the switch has turned off prior to counting the next lap.

    What if you turn the turntable really slow? Doesn't it count additional laps?

    As it is now, it looks like the switch has to be on between 20 and 39 loops in order for the lap to be counted correctly.
  • g3cwig3cwi Posts: 262
    edited 2012-02-25 11:36
    Hi Duane

    I estimate the on-time to be around 100ms and the loop runs much faster so there are no problems. It really does seem to work just fine as it is!

    Regards

    Richard
  • CircuitsoftCircuitsoft Posts: 1,166
    edited 2012-02-25 12:24
    A lot of reed switches don't bounce. How? There's a small amount of mercury on the contacts of the switch. When it closes, the mercury contacts. When it bounces, the surface tension of the mercury holds it in contact through the bounce.
Sign In or Register to comment.