Shop OBEX P1 Docs P2 Docs Learn Events
BUTTON Command — Parallax Forums

BUTTON Command

byocbyoc Posts: 17
edited 2013-05-22 13:43 in BASIC Stamp
I'm trying to make a simple pushbutton tactile switch that turns an LED on with one press and turns it off with another press. It's sort of working. The "*" in the debug window comes up everytime the button is pressed, but the LED does not turn on/off reliably. Sometimes it takes two or three pushes for it to work. Here's my code:

' {$STAMP BS2}
' {$PBASIC 2.5}

Btn PIN 3
btnWrk VAR Byte
TurnOn:
BUTTON Btn, 0, 255, 255, btnWrk , 0, PressOn
DEBUG "*"
HIGH 14
PressOn:
GOTO TurnOff
TurnOff:
BUTTON Btn, 0, 255, 255, btnWrk, 0, PressOff
DEBUG "*"
LOW 14
PressOff:
GOTO TurnOn

Any ideas as to what I'm doing wrong?

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2013-05-22 13:22
    You've got the GOTOs wrong. You want the 1st BUTTON statement to wait (hang) until the button is pressed, then fall through to the 2nd BUTTON statement to wait (hang) until the button is pressed again, then repeat. It should look like:
    Beginning:
    Wait1stPressed:
    BUTTON Btn, 0, 255, 255, btnWrk, 0, Wait1stPressed
    ' Here you can turn the LED on and use DEBUG to notify you
    Wait1stRelease:
    BUTTON Btn, 0, 255, 255, btnWrk, 1, Wait1stRelease
    
    Wait2ndPressed:
    BUTTON Btn, 0, 255, 255, btnWrk, 0, Wait2ndPressed
    ' Here you can turn the LED off and use DEBUG to notify you
    Wait2ndRelease:
    BUTTON Btn, 0, 255, 255, btnWrk, 1, Wait2ndRelease
    
    GOTO Beginning
    
    You also have to wait for the button to be released before moving on to the 2nd press, otherwise, the Stamp thinks you've pressed it again.
  • byocbyoc Posts: 17
    edited 2013-05-22 13:43
    Thanks so much, Mike. That makes perfect sense now. I tried that out and it works perfectly. However, I just discovered the TOGGLE command!

    Btn PIN 3
    btnWrk VAR Byte
    Main:
    BUTTON Btn, 0, 255, 255, btnWrk , 0, No_Press
    TOGGLE 14
    No_Press:
    GOTO Main

    Although, your way will make more sense when I use it in a practical application because ultimately I want it to do more than just turn the LED on and off.
Sign In or Register to comment.