Shop OBEX P1 Docs P2 Docs Learn Events
Code for a one Button RUN / STOP for the BS1 — Parallax Forums

Code for a one Button RUN / STOP for the BS1

Hello,
I can't seem to figure this one out...
The push button switch is one active low normally open contact which closes when pushed. (P0=0)
My goal is to press the button once to RUN and the same button again to STOP. A flag bit (Bit 0) would change state with Run and STOP. Thanks, Jim

j.aaa8jr@gmail.com

Comments

  • Hi @Clovey

    I don't have the hardware setup to try, but would something like this work for you ?

    ' BUTTON.BS2
    ' Connect an active-low circuit to pin P0 of the BS2. When you press the
    ' button, the DEBUG screen will display an asterisk (*). The program, as
    ' shown below, will print an asterisk at the first button press, then
    ' delay approximately one second (200 x 5 ms PAUSE) before auto-repeating
    ' at a rate of approximately 100 ms (5 x 20 ms).  Feel free to modify the
    ' program to see the effects of your changes on the way BUTTON responds.
    
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    Btn             PIN     0
    
    btnWrk          VAR     Byte
    btnLastState    VAR     Byte
    
    btnLastState = 0
    
    Main:
      ' Try changing the Delay value (255) in BUTTON to see the effect of
      ' its modes: 0 = no delay; 1-254 = varying delays before auto-repeat;
      ' 255 = no auto-repeat (only one action per button press)
      '
      ' The BUTTON instruction will cause the program to branch to
      ' No_Press unless P0 = 0
    
      PAUSE 5
      BUTTON Btn, 0, 200, 20, btnWrk, 0, No_Press
    
      IF btnLastState = 1 THEN
        btnLastState = 0
        DEBUG "RUN"
      ELSE
        btnLastState = 1
        DEBUG "STOP"
      ENDIF
    
    No_Press:
      GOTO Main
    
  • I used to write a hundred or so BS1 programs every year around Halloween for people using the EFX-TEK Prop-1 controller (has nothing to do with the Propeller 1 which was released after the BS1-powered Prop-1). To do what you want reliably, you'll want a debounce and force the button to be released. You could do it in a subroutine like this:

    Check_Button:
      timer = 0
    
    CB_Now:
      IF Trigger = NotPressed THEN CB_Exit 
        PAUSE 5
        timer = timer + 5
        IF timer < 50 THEN CB_Now
    
    CB_Release:
          IF Trigger = Pressed THEN CB_Release      ' force release
          PAUSE 100                                 ' let button clear
          state = state ^ 1                         ' flip state
    
    CB_Exit:
      RETURN
    

    This ensures a good button press and then allows a clean release. In the main body of your code you could do something like this.

    Main:
      GOSUB Check_Button
      BRANCH state, (State_0, State_1)
    
    
    State_0:
      ' code for state 0
      GOTO Main
    
    
    State_1:
      ' code for state 1
      GOTO Main
    

    The attached listing compiles, but I did not connect to a BS1.

Sign In or Register to comment.