EasyManua.ls Logo

Parallax BASIC Stamp 2e - Defining Array Variables

Default Icon
353 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
4: BASIC Stamp Architecture Defining Arrays
BASIC Stamp Programming Manual 2.0c www.parallaxinc.com Page 51
The above example will create a bit-sized variable called Mouse, and
nibble-sized variable called Cat, a byte-size 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 nibble 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 the BS2, BS2e, BS2sx and BS2p, 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 n1. 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:
1
2
e
2
sx
2
p
2
2
e
2
sx
2
p
2
DEFINING ARRAYS.

Table of Contents

Related product manuals