Electricity and Robots Project
For this project, we used an arduino board to create a program or a robot. We learned how to code which was very new and interesting. We started out with basic circuits and eventually made our way up to making our own program. Quinton Graybeal and I created a game of Red Light Green Light that also had music playing during the game.
Our Code
This is the code we used to create our program
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 = 30;
// Notes is an array of text characters corresponding to the notes
// in your song. A space represents a rest (no tone)
char notes[] = "agfgaaa ggg aaa agfgaaaaggagf ";// 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.
int beats[] = {1,1,1,1,1,1,2,1,1,1,2,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,3,1,1};
// The tempo is how fast to play the song.
// To make the song play faster, decrease this value.
int tempo = 300;
void setup() {
// put your setup code here, to run once:
pinMode(2, OUTPUT); //pin 1 is an output
pinMode(3, OUTPUT); //pin 3 is an output
pinMode(buzzerPin, OUTPUT); //the buzzer is an output
}
void loop() {
digitalWrite(2, HIGH); //this lights the LED that comes out of pin 1
digitalWrite(3, HIGH); //this lights the LED that comes out of pin 3
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:
//while(true){}
// 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 = 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.
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
// 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.
}
https://docs.google.com/a/students.nusd.org/document/d/1JY5wsKNxPGKMumL7dB7hVk-p1daBKH0E7kZE--m-bZo/mobilebasic
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 = 30;
// Notes is an array of text characters corresponding to the notes
// in your song. A space represents a rest (no tone)
char notes[] = "agfgaaa ggg aaa agfgaaaaggagf ";// 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.
int beats[] = {1,1,1,1,1,1,2,1,1,1,2,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,3,1,1};
// The tempo is how fast to play the song.
// To make the song play faster, decrease this value.
int tempo = 300;
void setup() {
// put your setup code here, to run once:
pinMode(2, OUTPUT); //pin 1 is an output
pinMode(3, OUTPUT); //pin 3 is an output
pinMode(buzzerPin, OUTPUT); //the buzzer is an output
}
void loop() {
digitalWrite(2, HIGH); //this lights the LED that comes out of pin 1
digitalWrite(3, HIGH); //this lights the LED that comes out of pin 3
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:
//while(true){}
// 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 = 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.
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
// 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.
}
https://docs.google.com/a/students.nusd.org/document/d/1JY5wsKNxPGKMumL7dB7hVk-p1daBKH0E7kZE--m-bZo/mobilebasic
Concepts
Circuit- A path of conductive material that electricity flows through and connects positive and negative sides of a power source
Resistance- An obstacle that weakens or slows the movement of electricity measured in ohms.
Power- Rate at which energy is transferred by a circuit
Voltage- Potential energy drop across a component of a circuit. Measured in volts
Resistance- An obstacle that weakens or slows the movement of electricity measured in ohms.
Power- Rate at which energy is transferred by a circuit
Voltage- Potential energy drop across a component of a circuit. Measured in volts
Reflection
On this project I worked very well. It was not my favorite project this year, but I learned a lot. At the beginning of this project it was hard to figure out how everything was working. Eventually I caught on and building our program became much easier. Many things with coding were frustrating. At times the code would have no errors, but the program wouldn't run how we needed it to. We figured it out and created our game. Learning to code and create this was very new and interesting and Quinton and I both learned a lot from it.