Our Assignment During this unit we learned a great deal about coding and electricity. Our first task was to complete two packets involving coding. The fist packet introduced the concept of coding and some terms such as voltage, amps, resistance, etc. The next packet went into more depth and actually had us set up arduino circuits and further understand coding. We used breadboards to experiment with making sound, using LEDs, and just simple connections. Throughout both packets we were checked up on to ensure that we understood all of the material. After we had a understanding of coding and electricity we were assigned to make our own project using the same concepts.
Our Project My group decided on the idea of making a song (Humble, Kendrick Lamar) play using the speaker. In order to make the song sound the same and play the same notes we had to figure out what notes to use and what the correct tempo would be. We looked up the notes for the song that we wanted to play and copied them down onto the code. It showed us the frequencies of each note and the tempo of the song. It was very tough trying to figure out the right tempo and frequencies to put into the code. Although it was tough we changed the frequencies often until the song sounded just like the original.
Our Code
const int buzzerPin = 9;
// We'll set up an array with the notes we want to play // change these values to make different songs!
// Length must equal the total number of notes and spaces
const int songLength = 47;
// Notes is an array of text characters corresponding to the notes // in your song. A space represents a rest (no tone)
char notes[] = "e e E e a a a E e e E e a a a E e e E e a a a"; // a space a rest
// Beats is an array of values for each note and rest. // A "1" represents a quarter-note, 2 a half-note, etc. // Don't forget that the rests (spaces) need a length as well.
int beats[] = {1,1,1,3,1,1,1,1,1,.8,1,1,1,.8,1,1,1,1,1,3,1,1,1,1,1,.8,1,1,1,.8,1,1,1,1,1,3,1,1,1,1,1,.8,1,1,1,.8,1};
// The tempo is how fast to play the song. // To make the song play faster, decrease this value.
int tempo = 180;
void setup() { pinMode(buzzerPin, OUTPUT); }
void loop() { int i, duration; for (i = 0; i < songLength; i++) // step through the song arrays { duration = beats[i] * tempo; // length of note/rest in ms if (notes[i] == ' ') // is this a rest? { delay(duration); // then pause for a moment } else // otherwise, play the note { tone(buzzerPin, frequency(notes[i]), duration); delay(duration); // wait for tone to finish } delay(tempo/10); // brief pause between notes } // We only want to play the song once, so we'll pause forever:
// If you'd like your song to play over and over, // remove the above statement }
int frequency(char note) { // This function takes a note character (a-g), and returns the // corresponding frequency in Hz for the tone() function. int i; const int numNotes = 11; // number of notes we're storing // The following arrays hold the note characters and their // corresponding frequencies. The last "C" note is uppercase // to separate it from the first lowercase "c". If you want to // add more notes, you'll need to use unique characters.
// For the "char" (character) type, we put single characters // in single quotes.
char names[] = {'e', 'e', 'E', 'e', 'a', 'a', 'a', 'E'}; int frequencies[] = {300, 300, 320, 300, 100, 100, 100, 380}; // Now we'll search through the letters in the array, and if // we find it, we'll return the frequency for that note. for (i = 0; i < numNotes; i++) // Step through the notes { if (names[i] == note) // Is this the one? { return(frequencies[i]); // Yes! Return the frequency } } return(0); // We looked through everything and didn't find it, // but we still need to return a value, so return 0.
the pictures below represent a diagram of the circuit and the actual circuit.
This project was a fun but tough project. The main reason it was hard was because the coding and different codes were all very confusing. A peak of mine was coming up with different ideas for my team mates. A pit was my productivity. I didn't work very fast and diligently on the packets and so it made it harder to know certain terms and concepts. Another peak was my team work. I would support others ideas and suggest new ones while having a good attitude toward our group. The second and final pit was the fact that I was out of school for a few days due to a concussion. it put me behind in the packets and behind in my group.