Hardware Reference
In-Depth Information
Note that because numbering starts at zero, the index of 2 represents the third
value in the array. If you want to change one of the values of the array, you can
do so in a similar fashion:
numbers[2] = 10;
Next, you will use arrays (as shown in these examples) to create a structure
that can hold the sequence of notes that you want to play on your speaker.
Making Note and Duration Arrays
To store the info about the song you want to play, you can use two arrays of the
same length. The first contains the list of pitches, and the second contains the
list of durations for which each note should play in milliseconds. You can then
iterate through the indices of these arrays and play back your tune.
Using the meager musical skills that I've maintained from my music classes
back in high school, I've assembled a short and catchy tune:
//Note Array
int notes[] = {
NOTE_A4, NOTE_E3, NOTE_A4, 0,
NOTE_A4, NOTE_E3, NOTE_A4, 0,
NOTE_E4, NOTE_D4, NOTE_C4, NOTE_B4, NOTE_A4, NOTE_B4, NOTE_C4, NOTE_D4,
NOTE_E4, NOTE_E3, NOTE_A4, 0
};
//The Duration of each note (in ms)
int times[] = {
250, 250, 250, 250,
250, 250, 250, 250,
125, 125, 125, 125, 125, 125, 125, 125,
250, 250, 250, 250
};
Note that both arrays are the same length: 20 items. Notice that some of the
notes are specified as 0 . These are musical rests (unplayed beats). Each note pairs
with a duration from the second array. For those familiar with music theory,
note that I've made quarter notes 250ms and eighth notes 125ms. The song is
in “four-four” time, in musical terms.
Try out this given note sequence, first; then try to create your own!
NOTE Listentoarecordingofthistune,playedbyanArduino:
www.exploringarduino.com/content/ch5 .Youcanalsofindthis
recordingontheWileywebsiteshownatthebeginningofthischapter.
Search WWH ::




Custom Search