Overview:
In this project we were tasked with making a show out of lights and buzzers. We had to figure out where the lights and buzzers went on the breadboard and also write all of the code for the lights and buzzers. We made our buzzer play Mary had a Little Lamb and our lights turn on every beat and then turn off once all of the turned on.
Our Code
int speakerPin = 7; //the pin that buzzer is connected to
int RedPin = 13; //the pin that the light is connected to
int GreenPin = 12; //the pin that the light is connected to
int BluePin = 11; //the pin that the light is connected to
int YellowPin = 10; //the pin that the light is connected to
void setup()
{
pinMode(speakerPin, OUTPUT); //set the buzzer as output
pinMode(RedPin, OUTPUT); //set the light as output
pinMode(GreenPin, OUTPUT); //set the light as output
pinMode(BluePin, OUTPUT); //set the light as output
pinMode(YellowPin, OUTPUT); //set the light as output
}
void loop()
{
play('E', 1); //mar
digitalWrite(13, HIGH); //lights the light connected to pin 13
play('D', 1); //ry
digitalWrite(12, HIGH); //lights the light connected to pin 12
play('C', 1); //had
digitalWrite(11, HIGH); //lights the light connected to pin 11
play('D', 1); //a
digitalWrite(10, HIGH); //lights the light connected to pin 10
play('E', 1); //lit
digitalWrite(13, LOW); //turns off light connected to pin 13
play('E', 1); //tle
digitalWrite(12, LOW); //turns off light connected to pin 12
play('E', 1); //lamb
digitalWrite(11, LOW); //turns off light connected to pin 11
play(' ', .5); //pause for 2 beats
play('D', 1); //lit
digitalWrite(10, LOW); //turns off light connected to pin 10
play('D', 1); //tle
digitalWrite(13, HIGH); //lights the light connected to pin 13
play('D', 1); //lamb
digitalWrite(12, HIGH); //lights the light connected to pin 12
play('E', 1); //lit
digitalWrite(11, HIGH); //lights the light connected to pin 11
play('G', 1); //tle
digitalWrite(10, HIGH); //lights the light connected to pin 10
play('G', 1); //lamb
digitalWrite(13, LOW); //turns off light connected to pin 13
play(' ', .5); //pause for 2 beats
play('E', 1); //ma
digitalWrite(12, LOW); //turns off light connected to pin 12
play('D', 1); //ry
digitalWrite(11, LOW); //turns off light connected to pin 11
play('C', 1); //had
digitalWrite(10, LOW); //turns off light connected to pin 10
play('D', 1); //a
digitalWrite(13, HIGH); //lights the light connected to pin 13
play('E', 1); //lit
digitalWrite(12, HIGH); //lights the light connected to pin 12
play('E', 1); //tle
digitalWrite(11, HIGH); //lights the light connected to pin 11
play('E', 1); //lamb
digitalWrite(10, HIGH); //lights the light connected to pin 10
play(' ', .5); //pause for 2 beats
play('C', 1); //its
digitalWrite(13, LOW); //turns off light connected to pin 13
play('D', 1); //fleece
digitalWrite(12, LOW); //turns off light connected to pin 12
play('D', 1); //was
digitalWrite(11, LOW); //turns off light connected to pin 11
play('E', 1); //white
digitalWrite(10, LOW); //turns off light connected to pin 10
play('D', 1); //as
digitalWrite(13, HIGH); //lights the light connected to pin 13
play('C', 4); //snow
digitalWrite(12, HIGH); //lights the light connected to pin 12
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 = 500; //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
*/
Reflection:
One thing that I did well on in this project was leading. I think that both me and Caetano did well in leadership because we made sure we did not stay off task for too long and made sure we got our project done. Another thing that I did well on was listening to Caetano's ideas and making sure that both of our ideas were in our project. One thing that I can improve on in the next project is thinking more critically. If I thought more critically during this project then we could have had more components to our light show instead of what we had. One more thing that I could have done better in my project was being more empathetic. Next project I will focus on being more empathetic and thinking critically.