BASIC stamp manual v2.2
4: BASIC Stamp Architecture – Defining Arrays
BASIC Stamp Syntax and Reference Manual 2.2 • www.parallax.com • Page 87
The above example will create a bit-sized variable called mouse, and
nibble-sized variable called cat, a byte-sized variable called dog and a
word-sized variable called rhino. Unlike in the BS1, these variable
declarations don't point to a specific location in RAM. Instead, we only
specified the desired size for each variable; the BASIC Stamp will arrange
them in RAM as it sees fit. Throughout the rest of the program, we can
use the names mouse, cat, dog and rhino to set or retrieve the contents of
these variables.
A variable should be given the smallest size that will hold the largest
value that will ever be stored in it. If you need a variable to hold the
on/off status (1 or 0) of switch, use a bit. If you need a counter for a
FOR…NEXT loop that will count from 1 to 100, use a byte. And so on.
If you assign a value to a variable that exceeds its size, the excess bits will
be lost. For example, suppose you use the byte variable dog, from the
example above, and write dog = 260 (%100000100 binary). What will dog
contain? It will hold only the lowest 8 bits of 260: %00000100 (4 decimal).
On all BS2 models, you can also define multipart variables called arrays.
An array is a group of variables of the same size, and sharing a single
name, but broken up into numbered cells, called elements. You can define
an array using the following syntax:
name VAR Size(n)
where name and Size are the same as described earlier. The new argument,
(n), tells PBASIC how many elements you want the array to have. For
example:
myList VAR Byte(10) ' Create a 10-byte array.
Once an array is defined, you can access its elements by number.
Numbering starts at 0 and ends at n-1. For example:
myList(3) = 57
DEBUG ? myList(3)
This code will display "myList(3) = 57" on the PC screen. The real power of
arrays is that the index value can be a variable itself. For example:
DEFINING ARRAYS.
All
2