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
symbol
symbol
do
low
if
PushButton = 1
gosub
end if
loop
LightLED:
high
pause
return
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.
P0 = B.0
IN3 = pinB.3
PushButton = IN3
P0
then
LightLED
P0
1000
' Define Output P0
' Define Input P3
' Define the PushButton input
' Do the following ...
' Initialise LED off
' When button pushed ..
'
Call the LightLED subroutine
' Repeat forever
' Turn P0 LED on
' Leave on for 1000ms (1s)
' And return from subroutine
Need help?
Do you have a question about the Versabot and is the answer not in the manual?
Questions and answers