BUTTON Command
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?
' {$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
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.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.