3.6 The Push Button
This section will give you an example of the use of a push button. Push buttons are extremely useful
for a range of things and so the
Button
programming code has quite a number of parameters (parts to
the code command that can vary).
1. Attach the Push Button module to P3 on the edge of the motherboard.
2.
Start the
Programming Editor
or
AXEpad
, or start a
new program by using the
File/New/Basic Program menu option.
3. Now, type the following into the text window:
symbol
P0 = B.0
' Define Output P0
symbol
IN3 = pinB.3
' Define Input P3
symbol
PushButton = IN3
' Define the PushButton input
do
' Do the following ...
low
P0
' Initialise LED off
if
PushButton = 1
then
' When button pushed ..
gosub
LightLED
' Call the LightLED subroutine
end if
loop
' Repeat forever
LightLED:
high
P0
' Turn P0 LED on
pause
1000
' Leave on for 1000ms (1s)
return
' And return from subroutine
Some things to note about the program above:
•
This time we have defined a variable called
PushButton
at the very beginning of the program.
It is called a ‘variable’ because the value can
vary
according to input from the user. Note that
the value for PushButton will be input at Pin 3 (IN3).
•
We have added a subroutine to the program and given it the label
LightLED
. The name of
the label should indicate what the function does. Subroutines are used to break up a long
program into smaller ‘sub-programs’.
•
Subroutines are called by using the
gosub
command and a subroutine returns control to the
line following the
gosub
command by using a
return
command. It is very important to add
the
return
command at the end of each subroutine.
4. Save your program as
PushButton.bas
.
5. Press the F5 key to download your program into the PICAXE on your Versabot motherboard.
6. Press the button at Pin 3 to turn the LED on and off.
Содержание Versabot
Страница 1: ...The Versabot Manual July 2009...
Страница 23: ...Step 03 Step 04 Step 05...
Страница 24: ...Step 06 Step 07 Step 08...
Страница 25: ...Step 09 Step 10 Step 11...
Страница 26: ...Step 12 Step 13...