Shop OBEX P1 Docs P2 Docs Learn Events
Help with BUTTON code — Parallax Forums

Help with BUTTON code

joshlfisherjoshlfisher Posts: 3
edited 2014-10-11 15:32 in BASIC Stamp
I have this snippet of code:
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 VstartButton
Right 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

  • SapphireSapphire Posts: 496
    edited 2014-10-11 12:31
    Add a pull-down resistor to the white wire connected to your button pin. It's floating and will report 1 or 0 randomly. 10k ohm will work.
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2014-10-11 12:37
    It would probably help to explain what you mean, perhaps add a pic.
    Someone could "google that", pulldown resistor, but still.
  • joshlfisherjoshlfisher Posts: 3
    edited 2014-10-11 13:25
    Thanks Sapphire! that did it.
    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)
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2014-10-11 13:32
    What is happening is that the command
    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.
  • joshlfisherjoshlfisher Posts: 3
    edited 2014-10-11 14:25
    Tracy, thanks for the explanation.
    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.
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2014-10-11 15:32
    What debounce means for the BUTTON command is simply this: Once it has detected buttonDown and jumped to motorStart, it has to detect buttonUp before it can again detect buttonDown.

    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.
Sign In or Register to comment.