For this project, we were given a Sparkfun Inventor's Kit and a DELL laptop and tasked with writing some code to create art. In my group, my partner and I wrote our code to make lights flash at the same time that a song was being played. This actually proved to be more difficult than we thought, and at one point our art show was fully functional. We were trying to improve the code and accidentally messed it up to the point where we didn't have enough time to fix all of our mistakes. Needless to say it was a good experience to have and I am proud of the work we did.
Here is our code (it starts on the line directly below this one):
// put your setup code here, to run once:
const int buzzerPin = 9;
const int songLength = 28;
char notes[] = "ggabgbadggabgfggabCbagfdefgg"; // a space represents a rest
int beats[] = {1,1,1,1,1,1,1,1,1,1,1,1,2,2,1,1,1,1,1,1,1,1,1,1,1,1,2,2};
int tempo = 200;
void setup()
{
pinMode(buzzerPin, OUTPUT);
pinMode(2, OUTPUT);
pinMode(4, OUTPUT);
pinMode(6, OUTPUT);
pinMode(8, OUTPUT);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(7, OUTPUT);
pinMode(10, 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 if (notes[i] == 'g') // otherwise, play the note
{
digitalWrite (2, HIGH);
digitalWrite (4, HIGH);
digitalWrite (6, HIGH);
digitalWrite (8, HIGH);
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration); // wait for tone to finish
digitalWrite (2, LOW);
digitalWrite (4, LOW);
digitalWrite (6, LOW);
digitalWrite (8, LOW);
// brief pause between notes
}
else if (notes[i] == 'a') // otherwise, play the note
{
digitalWrite (3, HIGH);
digitalWrite (5, HIGH);
digitalWrite (7, HIGH);
digitalWrite (10, HIGH);
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration);
digitalWrite (3, LOW);
digitalWrite (5, LOW);
digitalWrite (7, LOW);
digitalWrite (10, LOW); // wait for tone to finish
// brief pause between notes
}
else if (notes[i] == 'b') // otherwise, play the note
{
digitalWrite (2, HIGH);
digitalWrite (4, HIGH);
digitalWrite (6, HIGH);
digitalWrite (8, HIGH);
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration);
digitalWrite (2, LOW);
digitalWrite (2, LOW);
digitalWrite (4, LOW);
digitalWrite (8, LOW);
tone(buzzerPin, frequency(notes[i]), duration);
// wait for tone to finish
// brief pause between notes
}
else if (notes[i] == 'd') // otherwise, play the note
{
digitalWrite (3, HIGH);
digitalWrite (5, HIGH);
digitalWrite (7, HIGH);
digitalWrite (10, HIGH);
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration);
digitalWrite (3, LOW);
digitalWrite (5, LOW);
digitalWrite (7, LOW);
digitalWrite (10, LOW);
tone(buzzerPin, frequency(notes[i]), duration);
// wait for tone to finish
} // otherwise, play the note
else if (notes[i] == 'f') // otherwise, play
{
digitalWrite (2, HIGH);
digitalWrite (4, HIGH);
digitalWrite (6, HIGH);
digitalWrite (8, HIGH);
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration);
digitalWrite (2, LOW);
digitalWrite (4, LOW);
digitalWrite (6, LOW);
digitalWrite (8, LOW);
tone(buzzerPin, frequency(notes[i]), duration);
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration); // wait for tone to finish
}
delay(tempo/10); // brief pause between notes
}
//while(true){}
}
int frequency(char note)
{
int i;
const int numNotes = 10; // number of notes we're storing
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', 'v', 'h'};
int frequencies[] = {262, 294, 330, 369, 392, 440, 494, 523, 554, 415};
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.
}
}
// put your main code here, to run repeatedly:
The code ends on the line directly above this one.
Reflection:
This project was a little bit rough. It was fun, but it was hard. It was a really good experience to have because you got to see what coding is like. Through this project, you were able to see how meticulous it is, but also what you can do with coding. It amazes me what you can make a circuit board do with a certain combination of characters. I liked our final product which turned out pretty good, but when we tried to make it better things went awry and so I'd have to say that was the one thing I might have changed.