Chapter 2: Your Boe-Bot’s Servo Motors · Page 71
ACTIVITY #5: HOW TO STORE VALUES AND COUNT
This activity introduces variables, which are used in PBASIC programs to store values.
Boe-Bot programs later in this book will rely heavily on variables. The most important
thing about being able to store values is that the program can use them to count. As soon
as your program can count, it can both control and keep track of the number of times
something happens.
Your servos do not need to be connected to power for this activity.
√ If you have a Board of Education Rev C, set the 3-position switch to position-1.
This disconnects power from the servo ports only. The BASIC Stamp, Vdd, Vin,
and Vss will all still be connected to power.
√ If you have a BASIC Stamp HomeWork Board, remove one battery from the
battery pack, but leave the 9 V battery connected to the battery clip. This
disconnects power from the servo ports, but power remains connected to the
embedded BASIC Stamp, Vdd, Vin, and Vss.
Using Variables for Storing Values, Math Operations, and Counting
Variables can be used to store values. Before you can use a variable in PBASIC, you
have to give it a name and specify its size. This is called declaring a variable.
variableName VAR Size
You can declare four different sizes of variables in PBASIC:
Size – Stores
Bit – 0 to 1
Nib – 0 to 15
Byte – 0 to 255
Word – 0 to 65535
or -32768 to + 32767
The next example program just involves a couple of word variables:
value VAR Word
anotherValue VAR Word
After you have declared a variable, you can also initialize it, which means giving it a
starting, or initial, value.
value = 500
anotherValue = 2000