Shop OBEX P1 Docs P2 Docs Learn Events
Push button problems — Parallax Forums

Push button problems

Alexander DumasAlexander Dumas Posts: 9
edited 2006-10-12 19:40 in BASIC Stamp
I'm including my code - I hope someone can modify/explain why it is not behaving.

the goal:···· push down on a push button with what ever force and duration, and activate the flashonce routine 1 time

the problem: if I hold down on the momentary push button for 1/2 second or more, only the red button (the first in the chain) while light up and the call to the flashonce subroutine is momentarily messed up. I have to wait about 1 or 2 seconds more before the flashonce routine will work - if I click on the button very quickly, then it works.


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

DEBUG "Starting From Top", CR
counter1 VAR Word
PAUSE 1000
PushBtn PIN· 3 ' pushbutton on P0
DO
· DEBUG ? PushBtn, CR
· IF (PushBtn=1) THEN
··· GOSUB flashonce
· ENDIF
LOOP
flashonce:
······ counter1 = counter1 + 1
······ DEBUG "PushBtn=1", ? PushBtn, counter1, CR
······ 'flash red
······ HIGH 15
······ PAUSE 1000
······ LOW 15
······ 'now yellow
······ HIGH 12
······ PAUSE 1000
······ LOW 12
······ 'now green
······ HIGH 8
······ PAUSE 1000
······ LOW 8
RETURN

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2006-10-12 16:54
    I think this should be working the way you wrote it. I assume that, when you push the button, the pin gets set to HIGH. When the IF statement sees the HIGH, your "flashonce" routine gets called. Your routine flashes the red LED for one second, then the yellow one for one second, then the green one for one second, then returns. If the button is still HIGH, it repeats the whole process. If the button is LOW, it keeps checking it.

    Now, one question is: How did you hook up the button? How are the LEDs connected? Maybe there's a short circuit between the button circuitry and the LEDs. You didn't post a circuit diagram. What's the power source? Look at the "What's a microcontroller?" book (downloadable from the Parallax website) for instructions on hooking up a pushbutton to a Stamp.
  • Alexander DumasAlexander Dumas Posts: 9
    edited 2006-10-12 18:32
    thanks for answering...

    I essentially used the circuit from "What's a microcontroller?" page 81.. THe only modification is adding 2 other leds...

    It works fine as I mentionned - I can get all of the leds to false in succession - but only if I don't hold down on the push button....

    Otherwise it gags...

    From the debugging window it looks like it is operating as a program reset...

    Hold down for too long and it displays the initial message "Starting From Top" and then does a series of displays indicating pushbtn = 0
  • Mike GreenMike Green Posts: 23,101
    edited 2006-10-12 18:41
    Again, something must be wrong with how you hooked up your circuit. Maybe you've got an incorrect resistor somewhere. If the resistor in your pushbutton circuit is too small, you may be causing excessive current drain on the Stamp's regulator (if that's the source of +5V for the circuit) and the regulator will shut down turning off the power to the Stamp. That'll shut off the LEDs, cause a reset, then the power comes on again once the excessive drain stops.
  • bennettdanbennettdan Posts: 614
    edited 2006-10-12 19:29
    hey try this and see if it works more to your liking and if it works like you want then you can uncomment the debug lines of code to make the debug terminal come back up. Also make sure your button is on P3

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

    'DEBUG "Starting From Top", CR
    'counter1 VAR Word
    'PAUSE 1000
    'PushBtn PIN 3 ' pushbutton on P0
    DO
    'DEBUG ? PushBtn, CR
    IF in3=1 THEN flashonce
    ENDIF
    LOOP
    flashonce:
    'counter1 = counter1 + 1
    'DEBUG "PushBtn=1", ? PushBtn, counter1, CR
    'flash red
    HIGH 15
    PAUSE 1000
    LOW 15
    'now yellow
    HIGH 12
    PAUSE 1000
    LOW 12
    'now green
    HIGH 8
    PAUSE 1000
    LOW 8
    END
  • Alexander DumasAlexander Dumas Posts: 9
    edited 2006-10-12 19:40
    You're great!!! I put in a 15 omh instead of 15k omh resistor. Urgh... Works great now... THanks much.
Sign In or Register to comment.