Activity 7: Maneuver Sequences with Arrays

Some robotics applications require sequences of maneuvers.  You’ll actually see some simple sequence examples in the next chapter when the BOE Shield-Bot detects that it has bumped into an obstacle.  At that point, it has to back up, and then turn before trying to go forward again.  That is a simple sequence with two maneuvers. 

Another example is corridor navigation. The BOE Shield-Bot might have to find a corridor and then go through a sequence of maneuvers to enter it,  before searching for the corridor’s walls.

Other sequences can be more elaborate.  One example of a long and complex maneuver would be for a robotic dance contest.  (Robot dance contests have been gaining popularity in recent years.)  For dancing to an entire song, the robot might need a pretty long list of 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.

An integer array named note with eight elements, labels and arrows mark each element with its name and index number

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];

Then, your sketch could fill in the values of each element later, perhaps with sensor measurements, values entered from the Serial Monitor, or whatever numbers you need to store.

The diagram below shows the musical notes on the right side of a piano keyboard.  Each key press on a piano key makes a string (or a speaker if it’s electric) vibrate at a certain frequency. 

  • Compare the frequencies of the leftmost eight white keys to the values in the note array. 

Diagram of partial piano keyboard with keys C6 throu C8, and the frequency for each white and black key

Next, let’s use the array to make your BOE Shield-Bot play some musical notes…