BASIC Stamp Architecture – Defining Arrays
Page 52
•
BASIC Stamp Programming Manual 2.0b
•
www.parallaxinc.com
MyBytes VAR BYTE(10) ' Define 10-byte array.
Index VAR NIB ' Define normal nibble variable.
FOR Index = 0 TO 9 ' Repeat with Index= 0,1,2...9
MyBytes(Index) = Index * 13 ' Write index*13 to each cell of array.
NEXT
FOR Index = 0 TO 9 ' Repeat with Index= 0,1,2...9
DEBUG ? MyBytes(Index) ' Show contents of each cell.
NEXT
STOP
If you run this program, DEBUG will display each of the 10 values stored
in the elements of the array: MyBytes(0) = 0*13 = 0, MyBytes(0) = 1*13 =
13, MyBytes(2) = 2*13 = 26 ... MyBytes(9) = 9*13 = 117.
A word of caution about arrays: If you’re familiar with other BASICs and
have used their arrays, you have probably run into the “subscript out of
range” error. Subscript is another term for the index value. It is
out-of-range when it exceeds the maximum value for the size of the array.
For instance, in the example above, MyBytes is a 10-cell array. Allowable
index numbers are 0 through 9. If your program exceeds this range,
PBASIC will not respond with an error message. Instead, it will access the
next RAM location past the end of the array. If you are not careful about
this, it can cause all sorts of bugs.
If accessing an out-of-range location is bad, why does PBASIC allow it?
Unlike a desktop computer, the BASIC Stamp doesn’t always have a
display device connected to it for displaying error messages. So it just
continues the best way it knows how. It’s up to the programmer (you!) to
prevent bugs.
Another unique property of PBASIC arrays is this: You can refer to the 0th
cell of the array by using just the array’s name without an index value. For
example:
MyBytes VAR BYTE(10) ' Define 10-byte array.
MyBytes(0) = 17 ' Store 17 to 0th cell.
DEBUG ? MyBytes(0) ' Display contents of 0th cell.
DEBUG ? MyBytes ' Also displays contents of 0th cell.
2
e
2
sx
2
p
2
2
e
2
sx
2
p
2
Summary of Contents for BASIC Stamp 2e
Page 1: ...BASIC Stamp Programming Manual Version 2 0c...
Page 34: ...Quick Start Guide Page 32 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Page 340: ...ASCII Chart Page 338 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Page 342: ...Reserved Words Page 340 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Page 346: ...Conversion Formatters Page 344 BASIC Stamp Programming Manual 2 0b www parallaxinc com...