Shop OBEX P1 Docs P2 Docs Learn Events
Need help improving program speed because of an undesired pause, Looking for an — Parallax Forums

Need help improving program speed because of an undesired pause, Looking for an

beazleybubbeazleybub Posts: 102
edited 2008-08-02 23:52 in BASIC Stamp
Currently I am using this code to count how many times a·switch has been repeatedly·activated·in one instance·and add the count to VAR temp then continue with the rest of my program.

Unfortunately this is slowing down the rest of my program by 100 ms and causing a undesired pause in the routine that follows it because my overall program loops back to Main: to monitor the switch for the next cycle.

I am trying to find a workaround to monitoring the switch when it is active and only continue to the rest of my program after the switch goes silent.

Because the switch pulses 0,1,0,1,0,1,0,1,0 (OR) "off,on,off,on,off,on,off,on,off" I used the routine No_Press: to give the program a way to see that the switch was currently active and not continue until it was definitely silent.

But this causes the over all speed of my program to slow because it has to loop back to Main:

Any suggestions? Thanks
Main:
 
BUTTON 2, 1, 255, 20, btnWrk, 0, No_Press
temp=temp+1
 
No_Press:
 
FOR check = 1 TO 5
IF ButtonLocation = 1 THEN GOTO Main
PAUSE 20
NEXT

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
How can there be·nothing? Nothing is something!

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-08-02 20:53
    Here's a program that may help. It should work without slowing down your main program.

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    MAXTIMES CON 200
    
    PB       PIN 2
    
    pState   VAR Bit
    PBCount  VAR Nib
    Ctr      VAR Nib
    Tmr      VAR Word
    btnWrk  VAR Byte
    
    DO
      GOSUB GetPBCount
      IF (PBCount) THEN DEBUG DEC PBCount, CR
      GOSUB CountPB
    LOOP
    
    GetPBCount:
    
      IF (Tmr >= MAXTIMES) THEN
        PBCount = Ctr
        Ctr = 0
        Tmr = 0
      ELSE
        PBCount = 0
      ENDIF
      RETURN
    
    CountPB:
    
      BUTTON 2, 1, 255, 0, btnWrk, 0, No_Press
    
      Press:
    
        IF (pState = 0) THEN
          DEBUG "*"
          Ctr = Ctr + 1
          pState = 1
          Tmr = 0
        ENDIF
        RETURN
    
      No_Press:
    
        IF (pState = 0) THEN
          Tmr = Tmr + 1
        ELSE
          Tmr = 0
          pState = 0
        ENDIF
        RETURN
    
    
    



    There are two main subroutines: CountPB and GetPBCount.

    CountPB should be called as often as possible. It checks the state of the button and counts transitions from 0 to 1. It also times null periods, so it can detect gaps between groups of button presses.

    GetPBCount checks to see if a sufficiently-long gap has transpired between presses. If so it returns (and resets) the count. If not, it returns zero. GetPBCount can be called at your leisure, whenever you want to check if a group of button presses has occurred.

    The constant MAXTIMES will depend on the length of the null period you require between groups and how often CountPB is called.

    -Phil

    Post Edited (Phil Pilgrim (PhiPi)) : 8/2/2008 8:58:38 PM GMT
  • beazleybubbeazleybub Posts: 102
    edited 2008-08-02 23:52
    Phil,

    Neat snipplet!

    I like the way it works very much!


    ············ smilewinkgrin.gif

    Thank you.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    How can there be·nothing? Nothing is something!
Sign In or Register to comment.