Code
nt speakerPin = 10; //the pin that buzzer is connected to
void setup()
{pinMode(speakerPin, OUTPUT); //set the output pin for the speaker
pinMode(13, OUTPUT);
}
void loop()
{digitalWrite(13, HIGH); // Turn on the LED
delay(1000); //wait for one second
digitalWrite(13, LOW); // Turn off the LED
delay(1000); //wait one second
digitalWrite(12, HIGH); // Turn on the LED
delay(1000); // Wait for one seconds
digitalWrite(12, LOW); // Turn off the LED
delay(1000); // Wait for one seconds
digitalWrite(11, HIGH); //Turn on the LED
delay(1000); // Wait for one seconds
digitalWrite(11, LOW); // Turn off the LED
play('g', 2); //ha
digitalWrite(13, HIGH); // Turn on the LED
play('g', 1); //ppy
play('a', 4); //birth
play('g', 4); //day
digitalWrite(13, LOW); // Turn off the LED
play('C', 4); //to
play('b', 4); //you
play(' ', 2); //pause for 2 beats
play('g', 2); //ha
play('g', 1); //ppy
digitalWrite(12, HIGH); // Turn on the LED
play('a', 4); //birth
play('g', 4); //day
play('D', 4); //to
play('C', 4); //you
digitalWrite(12, LOW); // Turn off the LED
play(' ', 2); //pause for 2 beats
play('g', 2); //ha
play('g', 1); //ppy
play('G', 4); //birth
digitalWrite(11, HIGH); //Turn on the LED
play('E', 4); //day
play('C', 4); //dear
play('b', 4); //your
play('a', 6); //name
digitalWrite(11, LOW); // Turn off the LED
play(' ', 2); //pause for 2 beats
play('F', 2); //ha
play('F', 1); //ppy
play('E', 4); //birth
play('C', 4); //day
play('D', 4); //to
play('C', 6); //you
while (true) {} //get stuck in this loop forever so that the song only plays once
}
void play( char note, int beats)
{
int numNotes = 14; // number of notes in our note and frequency array (there are 15 values, but arrays start at 0)
//Note: these notes are C major (there are no sharps or flats)
//this array is used to look up the notes
char notes[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', 'D', 'E', 'F', 'G', 'A', 'B', ' '};
//this array matches frequencies with each letter (e.g. the 4th note is 'f', the 4th frequency is 175)
int frequencies[] = {131, 147, 165, 175, 196, 220, 247, 262, 294, 330, 349, 392, 440, 494, 0};
int currentFrequency = 0; //the frequency that we find when we look up a frequency in the arrays
int beatLength = 150; //the length of one beat (changing this will speed up or slow down the tempo of the song)
//look up the frequency that corresponds to the note
for (int i = 0; i < numNotes; i++) // check each value in notes from 0 to 14
{
if (notes[i] == note) // does the letter passed to the play function match the letter in the array?
{
currentFrequency = frequencies[i]; // Yes! Set the current frequency to match that note
}
}
//play the frequency that matched our letter for the number of beats passed to the play function
tone(speakerPin, currentFrequency, beats * beatLength);
delay(beats * beatLength); //wait for the length of the tone so that it has time to play
delay(50); //a little delay between the notes makes the song sound more natural
}
/* CHART OF FREQUENCIES FOR NOTES IN C MAJOR
Note Frequency (Hz)
c 131
d 147
e 165
f 175
g 196
a 220
b 247
C 262
D 294
E 330
F 349
G 392
A 440
B 494
Description
We did many things with the wires such as connecting them for an electric current for stuff to work and adding lights for looks as well as making the speaker work then started programming the code once we put in all the wires and circuits.
Resistance-330
Voltage -250
Coding -the experience of coding was weird and confusing as would get harder the farther you go into it copying a lot of stuff and having to remember the codes is a struggle but as time went on it got easier and we learned through asking questions on the way and learning from the teachers we were able to have success.