Okay, so I used some of the code from the original sketch for the "shave and a haircut" audio that came packaged in the Arduino software. I think I have a grasp on what is going here. I have made very detailed comments in helping me to learn about what is going here. Will you guys please read over my comments and tell me if I have anything even a little bit off?
Thank you.
//First tell the program what library(ies) to use. The file is called from the libraries folder.
#include "pitches.h"
//Then start out by telling the program what notes out of the pitches.h file we will be using. Since there are numbers used we must state that we
//are using an integer by writing int at the beginning.
int melody[] = {
NOTE_B0, NOTE_C1, NOTE_CS1, NOTE_D1, NOTE_D2, NOTE_DS1, NOTE_DS2, NOTE_E1, NOTE_E2, NOTE_E3, NOTE_F1, NOTE_F2,
NOTE_FS1, NOTE_FS2, NOTE_FS3, NOTE_G1, NOTE_G2, NOTE_G3, NOTE_GS4, NOTE_GS5, NOTE_GS1, NOTE_A1, NOTE_AS1, NOTE_AS4,
NOTE_AS5, NOTE_AS6, NOTE_B1, NOTE_B4, NOTE_B5, NOTE_C2, NOTE_C4, NOTE_C5, NOTE_C6, NOTE_CS2, NOTE_CS4, NOTE_CS5,
NOTE_CS6, NOTE_D2, NOTE_D4, NOTE_D5, NOTE_D6, NOTE_D7, NOTE_DS2, NOTE_DS4, NOTE_DS5, NOTE_DS6, NOTE_E2, NOTE_E4,
NOTE_E5, NOTE_E6, NOTE_E7, NOTE_F2, NOTE_F4, NOTE_F5, NOTE_F6, NOTE_F7, NOTE_FS2, NOTE_FS4, NOTE_FS5, NOTE_FS6,
NOTE_FS7, NOTE_G2, NOTE_G4, NOTE_GS2, NOTE_A2, NOTE_AS2, NOTE_B2, NOTE_C3, NOTE_DS3, NOTE_E3, NOTE_F3, NOTE_FS3,
NOTE_G3, NOTE_GS3, NOTE_A3, NOTE_AS3, NOTE_B3, NOTE_C4, NOTE_D4, NOTE_DS4, NOTE_E4, NOTE_F4, NOTE_FS4, NOTE_G4,
NOTE_GS4, NOTE_A4, NOTE_AS4, NOTE_B4, NOTE_CS5, NOTE_D5, NOTE_DS5, NOTE_F5, NOTE_FS5, NOTE_G5, NOTE_GS5, NOTE_A5,
NOTE_AS5, NOTE_B5, NOTE_C6, NOTE_CS6, NOTE_D6, NOTE_DS6, NOTE_E6, NOTE_F6, NOTE_FS6, NOTE_G6, NOTE_GS6, NOTE_A6,
NOTE_AS6, NOTE_B6, NOTE_C7, NOTE_CS7, NOTE_D7, NOTE_DS7, NOTE_E7, NOTE_F7, NOTE_FS7, NOTE_G7, NOTE_GS7, NOTE_A7,
NOTE_AS7, NOTE_B7, NOTE_C8, NOTE_CS8, NOTE_D8, NOTE_DS8};
//Next we need to state how many times we want each tone to play for. So we say it is
//an integer of course first because we are going to use a number in the string. Then
//we arbitrarily call it the noteLengths and say that we are going to play each note once.
//Each one of the 1's tells the program to play each note once. If we only had one 1, it would play
//all the tones one time, for the amount of time specified (here it's one full tone length which is set
//in thisNote<100). Since we have 132 tones, we've put the 1 132 separate times.
int noteLengths[] = {
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
//Now we start the setup so we can play these neat tones we have called up above.
void setup(){
//the "for" statement is causing the integer "thisNote" when it
//has the = sign to execute whatever is on the other side of it.
//It is set at zero because that is the lowest number we can use
//before using a negative number. 0 is always the beginning. The 100
//means that it is a full note, so it's on a scale of 0-100 in this
//type of equation. The next "thisNote" has a < sign, which means to
//test the equation by executing it on pin 5 (pin 5 is set below in the
//"tone" statement). Then the final "thisNote" with the ++ simply means
//to repeat the process. Then we open the process that we're going to write up
//with the open bracket.
for (int thisNote = 0; thisNote<100; thisNote++){
//Next we're going to need to say just how long we want each "thisNote" to play.
//So we say that's an integer and call the length "noteDuration". We say that we
//need the noteDuration to be one full note, that's why we're dividing the noteDurations
//by 1 so it will remain a full note. Then we
//state that the 1 full length for the noteDuration is done with thisNote. That means
//that thisNote is what is playing the tone through your speaker.
int noteLength = 1000/noteLengths[thisNote];
//Next we're calling the "tone" function as we next need to state how long each tone is played, so we
//tell it to play on pin 5 first, then we want the full note to be played in the melody
//by using the notes from thisNote. For each thisNote in the melody, we need to have it check the noteDuration
//so it can call the right integer, which is 1 up above (1/noteDurations) so it will play a full note.
tone (5, melody[thisNote], noteLength);
//Now we want a small delay between the notes so it doesn't sound like one fluid sound going up and down. Unless
//of course you want to have no space between the tones and have one smooth up and down sound. Since there is
//a number in there we have to firstly tell the line of code that there an integer it's going to have to deal with/
//as well as when the program reaches a number in the string of characters that it will be a whole number (integer).
//Then we call this the pauseBetweenNotes and say that the pauseBetweenNotes is equal to the noteDuration x 1, basically
//stating that the pause is the same length as the note itself.
int pauseBetweenNotes = noteLength * .05;
//Now since we have stated just how long we want the pause to be between the notes, we use the "delay" function to implement
//the actual pause between the notes. The reason we put the parenthesis here and not above is because we were just declaring
//that we intended to use the pauseBetweenNotes below.
delay (pauseBetweenNotes);
//if you uncomment the next line it will cause the program to stop after running through one time as long as you place
//this code in void loop(). If this code remains here in void setup() it will only run one time regardless if you have
//the line below in there or not.
//noTone(5);
}
}
void loop(){
}