Push button problems
Alexander Dumas
Posts: 9
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
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
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.
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
' {$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