The objective of this project was to design and code a light/sound system using various electrical connections transmitted from a computer to a bread board. My group and I designed a robot that played Hot Cross buns as well as lit up various colored lights in sequences. Before we began building, we learned different functions for finding the total Ohms values as well as different types of connections and currents.
Our Code
const int buzzerPin = 3; const int songLength = 19;
// Notes is an array of text characters corresponding to the notes // in your song. A space represents a rest (no tone)
char notes[] = "bag bag ggggaaaabag"; // a space represents 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[] = {2,2,3,1,2,2,3,1,1,1,1,1,1,1,1,1,2,2,3};
// The tempo is how fast to play the song. // To make the song play faster, decrease this value.
int tempo = 150;
void setup() { // put your setup code here, to run once: pinMode(13, OUTPUT); pinMode(12, OUTPUT); pinMode(11, OUTPUT); pinMode(10, OUTPUT); pinMode(9, OUTPUT); pinMode(8, OUTPUT); pinMode(7, OUTPUT); pinMode(6, OUTPUT); pinMode(5, OUTPUT); pinMode(4, OUTPUT); pinMode(buzzerPin, OUTPUT);
}
void loop() { // put your main code here, to run repeatedly: digitalWrite(13, HIGH); // Turn on the LED digitalWrite(4, HIGH); delay(250); // Wait for 0.25 seconds digitalWrite(12, HIGH); // Turn on the LED digitalWrite(5, HIGH); delay(250); // Wait for 0.25 seconds digitalWrite(11, HIGH); // Turn on the LED digitalWrite(6, HIGH); delay(250); // Wait for 0.25 seconds digitalWrite(10, HIGH); // Turn on the LED digitalWrite(7, HIGH); delay(250); // Wait for 0.25 seconds digitalWrite(9, HIGH); // Turn on the LED digitalWrite(8, HIGH); delay(250); // Wait for 0.25 seconds digitalWrite(9, LOW); // Turn off the LED digitalWrite(8, LOW); delay(250); // Wait for 0.25 seconds digitalWrite(10, LOW); // Turn off the LED digitalWrite(7, LOW); delay(250); // Wait for 0.25 seconds digitalWrite(11, LOW); // Turn off the LED digitalWrite(6, LOW); delay(250); // Wait for 0.25 seconds digitalWrite(12, LOW); // Turn on the LED digitalWrite(5, LOW); delay(250); // Wait for 0.25 seconds digitalWrite(13, LOW); // Turn off the LED digitalWrite(4, LOW); delay(250); // Wait for 0.25 seconds
digitalWrite(13, HIGH); // Turn on the LED delay(200); // Wait for 0.2 seconds digitalWrite(12, HIGH); // Turn on the LED delay(200); // Wait for 0.2 seconds digitalWrite(11, HIGH); // Turn on the LED delay(200); // Wait for 0.2 seconds digitalWrite(10, HIGH); // Turn on the LED delay(200); // Wait for 0.2 seconds digitalWrite(9, HIGH); // Turn on the LED delay (200); // Wait for 0.2 seconds digitalWrite(8, HIGH); // Turn on the LED delay(200); // Wait for 0.2 seconds digitalWrite(7, HIGH); // Turn on the LED delay(200); // Wait for 0.2 seconds digitalWrite(6, HIGH); // Turn on the LED delay(200); // Wait for 0.2 seconds digitalWrite(5, HIGH); // Turn on the LED delay (200); // Wait for 0.2 seconds digitalWrite(4, HIGH); // Turn on the LED delay(250); // Wait for 0.2 seconds digitalWrite(4, LOW); // Turn off the LED delay(200); // Wait for 0.2 seconds digitalWrite(5, LOW); // Turn off the LED delay(200); // Wait for 0.2 seconds digitalWrite(6, LOW); // Turn off the LED delay(200); // Wait for 0.2 seconds digitalWrite(7, LOW); // Turn off the LED delay(200); // Wait for 0.2 seconds digitalWrite(8, LOW); // Turn off the LED delay(200); // Wait for 0.2 seconds digitalWrite(9, LOW); // Turn off the LED delay(200); // Wait for 0.2 seconds digitalWrite(10, LOW); // Turn off the LED delay (200); // Wait for 0.2 seconds digitalWrite(11, LOW); // Turn off the LED delay(200); // Wait for 0.2 seconds digitalWrite(12, LOW); // Turn off the LED delay(200); // Wait for 0.2 seconds digitalWrite(13, LOW); // Turn off the LED delay(200); // Wait for 0.2 seconds
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 }
// Repeat infintely unless instructed otherwise.
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 = 8; // 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 }
Concepts
Resistance (R): measure of how much current is slowed/how many obstacles; it is measured in ohms Series:all parts of the circuit are connected on one path for the electricity (total resistance of resistors in a series) Parallel:all parts of the circuit are connected across from each other forming two paths for the electricity (sum of resistors in a series=total resistance) Electrical Circuit: a complete loop of conductive material from positive to negative sides of a power source Ohm's Law: voltage = current x resistance (V=IR)
Kirchhoff's Laws: Current Law: the sum of the currents going into a connection must equal the sum of the currents leaving the connection. Voltage Law: the sum of the voltages around a circuit must equal zero. Current (I) : "flow" of electricity through a circuit.
Voltage (V): potential energy drop across a component of a circuit.
Power (P): rate at which electrical energy is transferred by a circuit.
Reflection
Overall, this was easily the most intricate project we did all year. You have to pay attention to the slightest details or none of it would work. We had to try many different designs before finding one that worked and did what we wanted it to do. A skill I gained was a new attention to detail, because that was necessary for all the little connectors and wires of the breadboard. I also learned how to code on Arduino, which I'd never done before. I could still work on my focus more, as well as be more involved in the process than I was at the time. All in all we were able to finish with a working product on a completely new and difficult subject.