This is
an introduction to programming using the BasicX micro controller, if
you have never programmed before this will help you on your way. More
experienced readers should still skim through this to get the basics
of the language.
Programming is a common language between you and your Robocore, it
lets you tell it what to do. In robotics, programming is necessary to
make machines that can work by themselves, without human intervention.
These are called autonomous robots.
1) Lets
start by connecting your Robocore to the power supply and the serial
port of your computer, and loading up the BasicX software.
2) When
this is loaded, click on the Monitor Port menu and select the COM port
that the Robocore is connected to (probably COM1). The Download Port
menu should be set to the same COM port.
3) Click
the editor button. A window should open asking for a filename. Just
type a name for your first program, lets say Demoprog. It will tell
you that the file does not exist, so do you want to create it. Click
yes.
Now lets
write our first program!
To test
that the BasicX is working, we will make it send a message back to
your computer. This is done using the debug.print command.
Type the
following program into the editor window (or copy and paste it). The
first and last lines should already be present, if they are just copy
the middle line.
Sub
main()
debug.print "Robocore test: Everything is working"
End Sub
Before we can download
to the BasicX we need to set the chip preferences. This basically
tells the chip what we want to be doing with each pin (input, output
etc). To do this click on the project menu, followed by chip (or the
F7 shortcut). We can leave everything as it is for now so just click
on OK.
Now
click on the Compile menu and select Compile and Run (keyboard
shortcut F5) This will send the code to the Robocore. Going back to
the main BasicX window, you should see the text ' Robocore test:
Everything is working ' appear on the screen.
The Sub
main() and End Sub commands tell the Robocore where the program begins
and ends.
The
debug.print command is very useful for telling us what the Robocore is
doing, in the next case we will see more of its capabilities.
Now we
will make the Robocore solve an equation for us. To do this we will
need a variable. This is a value stored under a name in the memory of
the Robocore. We will give this variable the name ' answer '.
Sub
main()
Dim
answerI As integer ' declaring
Dim
answer As string ' variables
answerI
= 5*5 ' doing the maths
answer=Cstr(answerI)
'convert to printable format
debug.print "5 times 5 is "; answer 'print answer
End Sub
Run the
program as before and you should get the computer telling you the
answer.
This
program shows that you can print values from your program to the
screen using debug.print just by adding a semi-colon and the variable
name after the "text", this is very handy for example when testing
sensors.
The
program also uses comments, these have no effect on the program but
allow us to add information about each line of the program. The
computer ignores everything written on the line after an apostrophe
(').
This
program has also introduced us to variables, the topic for the next
tutorial in the programming series |