Hardware Reference
In-Depth Information
Completing the Program
The last step is to actually add playback functionality to the sketch. This can be
accomplished with a simple for loop that goes through each index in the array,
and plays the given note for the given duration. Since you presumably don't
want to listen to this over and over again, you can put the playback functionality
in the setup() function so that it only happens once. You can restart playback
by hitting the Reset button. Listing 5-1 shows the complete playback program.
Listing 5-1: Arduino Music Player—music.ino
//Plays a song on a speaker
#include "pitches.h" //Header file with pitch definitions
const int SPEAKER=9; //Speaker Pin
//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
};
void setup()
{
//Play each note for the right duration
for (int i = 0; i < 20; i++)
{
tone(SPEAKER, notes[i], times[i]);
delay(times[i]);
}
}
void loop()
{
//Press the Reset button to play again.
}
Search WWH ::




Custom Search