Shop OBEX P1 Docs P2 Docs Learn Events
Need help simplifying/debuging a simple program — Parallax Forums

Need help simplifying/debuging a simple program

Jpsousa4Jpsousa4 Posts: 2
edited 2005-08-20 20:53 in BASIC Stamp
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:
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?

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-08-20 14:15
    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 WilliamsJon Williams Posts: 6,491
    edited 2005-08-20 14:26
    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).

    attachment.php?attachmentid=38603


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    310 x 502 - 12K
  • Jpsousa4Jpsousa4 Posts: 2
    edited 2005-08-20 20:53
    I'll be sure to do that next time, Jon.
    Thank you so much for helping! =)
Sign In or Register to comment.