Help with BUTTON code
joshlfisher
Posts: 3
I have this snippet of code:
It seems to work almost as intended.
The Start_Engine_Button function Loops, and the Button command DOES call the StartEngine function when the button is pressed,
HOWEVER,
If I wait and do not push the button right away, the StartEngine function is called ANYWAY, as if the button was pressed. It takes about 2 seconds.
The switch is a combo LED/momentary pushbutton.
Wiring is red/LED+, Black/LED-, and White.
Pushing the switch shorts Red and White
I have Red connected to VDD, and Black connected to a pin. Pull the pin low to turn on the LED.
I have the White connected to a pin. I assume the pressing the switch will pull the pin High.
What am I missing? Why does the pin go HIGH eventually, even if I don't press the switch?
I can eliminate the problem by adding
I don't really like this, as it feels like a band-aid, and I didn't think this behavior was typical.
Tag_Found: LOW VstartButtonLED LOW VstartButton GOTO Start_Engine_Button Start_Engine_Button: DEBUG "BEFORE BUTTON" BUTTON VstartButton,1,255,255,BtnWk,1,StartEngine DEBUG "AFTER BUTTON" GOTO Start_Engine_Button StartEngine: HIGH VstartRelay PAUSE 2000 LOW VstartRelay HIGH VstartButtonLED
It seems to work almost as intended.
The Start_Engine_Button function Loops, and the Button command DOES call the StartEngine function when the button is pressed,
HOWEVER,
If I wait and do not push the button right away, the StartEngine function is called ANYWAY, as if the button was pressed. It takes about 2 seconds.
The switch is a combo LED/momentary pushbutton.
Wiring is red/LED+, Black/LED-, and White.
Pushing the switch shorts Red and White
I have Red connected to VDD, and Black connected to a pin. Pull the pin low to turn on the LED.
I have the White connected to a pin. I assume the pressing the switch will pull the pin High.
What am I missing? Why does the pin go HIGH eventually, even if I don't press the switch?
I can eliminate the problem by adding
LOW VstartButtonRight before the BUTTON command, so it sets the pin low every time it loops, waiting for the button.
I don't really like this, as it feels like a band-aid, and I didn't think this behavior was typical.
Comments
Someone could "google that", pulldown resistor, but still.
I always forget about floating pins.
as per PJ Allen's request for an explanation, I knew exactly what you were talking about with a pulldown resistor, but for those that don't, Just connect a 10K ohm resistor between the PIN, and Vss( or ground), to pull the pin LOW (will report 0 if polled with a DEBUG statement)
LOW VstartButton
is making that pin a low output. Then when the program hits the BUTTON command, that pin is turned into an input and at first it is low. But it gradually drifts away from zero and after about two seconds as you say it drifts up past the Stamp switching threshold and jumps to StartEngine. You can cut short that interval by pressing the button. The circuit needs that pulldown resistor as suggested by Sapphire. And in that case it won't need the LOW command.
You didn't mention a resistor in series with the LED. Is there a resistor included inside the pushbutton?
The button command may be overkill for your purpose. A simple
IF VstartButton = 0 GOTO StartEngine
may suffice.
The pull down resistor did the trick, and I nixed the LOW VstartButton. I had also mis-wired my breadboard, and that contributed to one of my previous attempts (WITH a pulldown resistor) not working, hence my plea for help. All good now.
Not sure if there is a resistor in the switch for the LED. The original application the switch was intended for (Universal Push-button start kit for automobile) Just fed the LED 5v. I assume that is what the Stamp is doing.
I used the button command simply to take advantage of the debounce, I'm sure you are right, and I don't really need it, but it works.
What it does not mean (often confused) is a different thing. When it is waiting for the buttonDown, it does NOT require the button to be down for a length of time, stable, before it counts as buttonDown. In other words, even a millisecond pulse of noise coming from outside will be recognized as buttonDown. If you want that kind of debouncing, you'll have to read the state a few times in a row to be sure it is for real.