PDA

View Full Version : Need help simplifying/debuging a simple program



Jpsousa4
08-20-2005, 01:12 PM
My basic stamp and board came in the mail just a few hours ago. This is my first experience with a mircrocontroller. I've been fooling around with the button and other commands.
Right now I'm trying to get a pin to switch between high and low while the button is closed, and rest at low when open.
Problem is, it only works once. That is, the LED will blink when the button is closed, but after its opened, and closed again, the LED doesn't react.

I've checked my wiring, so its not that:
http://img398.imageshack.us/img398/8100/blinkled0hu.jpg

heres my code:


' {$STAMP BS2}
' {$PBASIC 2.5}
btna VAR Byte 'define variables
btnb VAR Byte

work: 'main
BUTTON 1,1,255,250,btna,0,noswitch 'If low, return to main.
BUTTON 1,1,255,250,btnb,1,switch 'if high, go to switch.

noswitch:
GOTO work 'return to main


switch: 'blink the LED for 250.
HIGH 4
PAUSE 250
LOW 4
PAUSE 250
IF (IN1 = 0) THEN work 'if switch is let go, return to main.
GOTO switch 'else, continue blinking the LED




Any ideas on simplifying/fixing the code?

Jon Williams
08-20-2005, 10:15 PM
Here's a program that works (just tested on my PDB) -- note that it takes advantage of PBASIC 2.5 capabilities and conforms to our "The Elements of PBASIC Style." What you'll find in actual practice is that the BUTTON instruction doesn't normally need to be used.



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

Btn PIN 1
Led PIN 4

Setup:
LOW Led

Main:
DO
IF (Btn = 1) THEN
TOGGLE Led
PAUSE 250
ELSE
LOW Led
ENDIF
LOOP

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax

Jon Williams
08-20-2005, 10:26 PM
Just a follow-up: Let me suggest that you need a resistor inline with your LED and it's a good idea to put one inline with the button input as well (to prevent accidents).

http://forums.parallax.com/attachment.php?attachmentid=38603


▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax

Jpsousa4
08-21-2005, 04:53 AM
I'll be sure to do that next time, Jon.
Thank you so much for helping! =)