Shop OBEX P1 Docs P2 Docs Learn Events
beginner help - Snippet of code for button press — Parallax Forums

beginner help - Snippet of code for button press

plingbootplingboot Posts: 7
edited 2008-10-18 23:38 in BASIC Stamp
Hi all,
I just wondered whether anyone had a snippet of code they could post to help me get started with the SUI buttons on my BotBoard2/BS2p please. The button/led's are on pins 12, 13 and 14.

At this moment, i just want to get to grips with making it work - ie playing a sound on a button push or showing a message in the debug window.

I guess i'd like to be able to have a version which detected which button was is being pushed at start-up too?

Thanks in advance.

Comments

  • plingbootplingboot Posts: 7
    edited 2008-10-17 19:02
    Thanks for the link,
    Just tried the simple programme on p78 (with my botboard 2) but instead of the debug showing 0 while the button is up and 1 when it's down, i'm getting the opposite. rolleyes.gif ie: 1 and when i press i get 0

    Cold anyone point out the error of my ways?

    ' {$STAMP BS2p}
    ' {$PBASIC 2.5}
    
    DO 
    DEBUG ? IN12
    PAUSE 250 
    LOOP
    
    
  • NR1XNR1X Posts: 111
    edited 2008-10-17 19:32
    the differance between active high and active low is why you have differant results.. if you have 0 without pushing button then you have active high. (high signal when button is pressed)
    if you have 1 without pressing button then you have active low.(low signal when button is pressed).. if you haven't gone through the whats a microcontroller text, it is highly recommended.. you will learn alot about what the stamp can do and how to make it do what you want..
  • GICU812GICU812 Posts: 289
    edited 2008-10-18 17:51
    Yea, it sounds like you have a high pullup on the pin, and the switch is connected to ground. If you pull the pin low, and connect the switch to +5v then you will get 0 when waiting and 1 when pressing.
    Some other useful button code

    Wait for button press, then run code.

    do while in12 = 0 ' if in12 is low, then loop, if in 12 is high, exit loop
    loop

    your code here


    Polling buttons

    do
    if in12=1 then gosub button1
    if in13=1 then gosub button2
    if in14=1 then gosub button3
    loop

    button1:
    debug "Button 1 pressed"
    return

    button2:
    debug ... you get the idea

    Also, you can refrence the pins with names

    Button1 PIN 12

    do

    if Button1 = 1 then debug "Button 1 is Pressed"
    if in12 = 1 then debug "This also shows button one is pressed

    loop

    All of the above was assuming you were working with a high signal when the button was pressed. If you continue working with a low signal, just invert all the 0's to 1' and 1's to 0's


    That should get you playing.
  • GICU812GICU812 Posts: 289
    edited 2008-10-18 17:53
    oh and is may be useful to wait for the button to be released before moving on with code, otherwise if its fast enough, it will run the code several times per button push

    do
    if in12=1 then gosub button1
    loop


    button1:
    do while in12=1 'this will hold the program until you release button 1
    loop
    now put your code
  • plingbootplingboot Posts: 7
    edited 2008-10-18 18:33
    thanks for the reply.

    i'm using a lynxmotion botboard2. the buttons are built-in to the board - 3 of them on pins 12, 13, 14. They are enabled with 3 jumpers. I'm therefore not sure what needs to be done to change to a high signal - or whether it's necessary.

    the brief info on using the buttons (in the instruction pdf) says:

    The 3 LEDs and Pushbuttons use only 3 I/O lines to make a simple user interface for your program.
    By making the I/O line a low output the LED can be turned on. By briefly making the I/O line an input the I/O line cane be read to see if a button is being pressed. However, do not make the I/O line a high output and press a button, as amage to the I/O pin can occur.
  • NR1XNR1X Posts: 111
    edited 2008-10-18 23:38
    sounds like the buttons are hardwired as active high..
Sign In or Register to comment.