Datasheet
Chapter 4 • BOE Shield-Bot Navigation
128 • Robotics with the BOE Shield-Bot
maneuvers and maneuver times. If your sketch needs to store lists of maneuvers, the
variable array is the best tool for storing these lists.
This activity introduces arrays with some simple musical applications using the
piezospeaker. Then, it examines two different approaches for using arrays to store
sequences of maneuvers for playback while the sketch is running.
What’s an Array?
An array is a collection of variables with a common name. Each variable in the array is
referred to as an element. Here is an array declaration with eight elements:
int note[] = {1047, 1174, 1319, 1397, 1568, 1760, 1976, 2093};
The array’s name is note, and each element in the array can be accessed by its index
number. Arrays are zero indexed, so the elements are numbered 0 to 7. The diagram below
shows how
note[0] stores 1047, note[1] stores 1174, note[2] stores 1319, and so on,
up through
note[7], which stores 2093.
Let’s say your code needs to copy note[3], which stores the value 1397, to a variable
named
myVar. Your code could do it like this:
myVar = note[3];
Your sketch can change array element values too. For example, you could change the value
1976 to 1975 with an expression like this:
note[3] = 1975;
An array does not have to be pre-initialized with values like it is in the diagram above. For
example, you could just declare an array with 8 elements like this:
int myArray[8];
note[0]
int note[] = {1047, 1174, 1319, 1397, 1568, 1760, 1976, 2093};
note[1]
note[7]
note[2]
note[6]
note[3]
note[4]
note[5]