Datasheet
Chapter 4 • BOE Shield-Bot Navigation
132 • Robotics with the BOE Shield-Bot
int elementCount = sizeof(note) / sizeof(int);
Later, your for loop can use the elementCount variable to play all the notes in the array,
even if you add or delete elements:
for(int index = 0; index < elementCount; index++)
Create, save, and run PlayAllNotesInArray.
Open the Serial Monitor as soon as the sketch is done loading.
Verify again that the Serial Monitor displays each note in the array as the speaker
plays it.
// Robotics with the BOE Shield – PlayAllNotesInArray
// Uses sizeof to determine number of elements in the array
// and then displays and prints each note value in the sequence.
int note[] = {1047, 1147, 1319, 1397, 1568, 1760, 1976, 2093};
void setup()
{
Serial.begin(9600);
int elementCount = sizeof(note) / sizeof(int);
Serial.print("Number of elements in array = ");
Serial.println(elementCount);
for(int index = 0; index < elementCount; index++)
{
Serial.print("index = ");
Serial.println(index);
Serial.print("note[index] = ");
Serial.println(note[index]);
tone(4, note[index], 500);
delay(750);
}
}
void loop()
{
}
Your Turn – Add Notes
Try adding the next two notes, D7 and E7, using the frequencies (rounded down)
from the keyboard diagram. Your array can use more than one line, like this: