Our Assingment
During this assingment we learned all about electricity and coding. We were first assinged to complete two packets that explained all of the basic principals of electric circuits such as voltage, amps, resistance, etc. After we understood the basics, we moved on to the more complicated arduino programming packet. During this time we experiment with LED lights and creating sound through the programming. Once we understood how to connect the breadboard and computer to create complex codes, it was time to make our own.
Our project
My group decided that we were going to make a song. In order to do this we had to fin the tempo pf the song, the frequencies of each note, and we needed to have a good ear so that we could adjust the notes and tempo accordingly. The hardest part about this project was making the song sound as good as possible. When entering the frequencies for each note and listening to the song we could tell that something was not right. We th changed frequencies and tempo until our song sounded the way that we wanted it to.
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.
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.
// 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.
Below are pictures of our cicuit diagram and our actually circuit
Reflection
This project was definately a difficult one but it taught me a lot. Going through three full packets of information was a lot to take in but the material was presented very well. I learned how to overcome a lot of challenges and that when dealing with code, you really need to double check everything becuase the smallest flaw can stop the entire project from working. Overall this was a challenging but educational project.