The goal of this project was to learn about electricity and coding. We had to complete multiple packets full of experiments to learn about these things. The first packet's content consisted of basic electrical properties such as voltage, resistance, current, and more. The second packet was more intricate and complicated, we had to use a breadboard to do the experiments. For the third and final packet, we had to use an Arduino programming packet to learn how to code. Once all the packets were completed, we had to create our own code and present it to the class.
Our Project:
For our project, my partner and I decided to program a song for our presentation. To do this, we had to find the notes, their frequencies, and the tempo of the song. The hardest part of programming was getting the song to sound exactly how we wanted it to. We noticed that we needed to change the frequencies of each note drastically because the difference of a couple numbers didn't change the sound that much. Once we figured this out, it was relatively easy. We just had to do a lot of guess and check with the frequencies until we got it right.
The 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.
// 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.
Pictures of our Project:
Reflection:
This was a relatively difficult project, considering my partner and I went into it having no prior knowledge of how to code or program. However, we came out of it now having a decent amount of knowledge on it. It took a lot of work to get there, three packets worth of work. The biggest thing I learned was that precision and concentration is a huge and vital part of work, in this case, if we weren't precise with every little detail, our project wouldn't have worked. I learned that my partner and I are both very good at getting a lot of work done and done well if we really concentrate and put our minds to something.