Page 148 ·
Robotics with the Boe-Bot
familiar code blocks in subroutines for each basic maneuver. Each maneuver is given a
one-letter code as a reference. Long lists of these code letters can be stored in EEPROM
and then read and decoded during program execution. This avoids the tedium of
repeating long lists of subroutines, or having to change the variables before each
GOSUB
command.
This programming approach requires some new PBASIC instructions: the
DATA
directive,
and
READ
and
SELECT
...
CASE
...
ENDSELECT
commands. Let’s take a look at each before
trying out an example program.
Each of the basic maneuvers is given a single letter code that will correspond to its
subroutine: F for
Forward
, B for
Backward
, L for
Left_Turn,
and R for
Right_Turn.
Complex Boe-Bot movements can be quickly choreographed by making a string of these
code letters. The last letter in the string is a Q, which will mean “quit” when the
movements are over. The list is saved in EEPROM during program download with the
DATA
directive, which looks like this:
DATA "FLFFRBLBBQ"
Each letter is stored in a byte of EEPROM, beginning at address 0 (unless we tell it to
start somewhere else). The
READ
command can then be used to get this list back out of
EEPROM while the program is running. These values can be read from within a
DO…LOOP
like this:
DO UNTIL (instruction = "Q")
READ address, instruction
address = a 1
' PBASIC code block omitted here.
LOOP
The
address
variable is the location of each byte in EEPROM that is holding a code
letter. The
instruction
variable will hold the actual value of that byte, our code letter.
Notice that each time through the loop, the value of the
address
variable is increased by
one. This will allow each letter to be read from consecutive bytes in the EEPROM,
starting at address 0.
The
DO…LOOP
command has optional conditions that are handy for different
circumstances. The
DO UNTIL (condition)
...
LOOP
allows the loop to repeat until a
certain condition occurs.
DO WHILE (condition)
...
LOOP
allows the loop to repeat only