beginner help - Snippet of code for button press
plingboot
Posts: 7
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.
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
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. ie: 1 and when i press i get 0
Cold anyone point out the error of my ways?
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..
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.
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
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.