Recently, we were taught about breadboards, how they worked, coding, how to set up an Arduino board, and how to get a robot to work. We were given the task of creating a robot with what we had picked up from the lessons throughout the past weeks to demonstrate our knowledge on the topic. Me and my partner for this project had little no experience prior to this project on coding and building robots, so we had to create a robot that showed that we understood what we were taught, but was also simple enough for us to build properly and thoroughly. We settled with a robot that played hot cross buns and had a light that blinked before the song played to show when the song started. we were able to set up the robots circuit easily, however, we had a lot of problems when setting up the code. There were several problems when we tried to get the robot to do several things at once. Our original idea was to play a song with buttons and when you pressed the button the light would flash, but we couldn't get the code to work and tried getting a light to blink while the song played and somehow in the code it made it so that the light flashed just before the song played and after the song played.
Code
This is the coding we used for our robot
int speakerPin = 10; // Set pin 10 as speaker pin
void setup() {
pinMode(13, OUTPUT); // Set pin 13 to output
pinMode(speakerPin, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH); // Turn on the LED
delay(2000); // Wait for two seconds
digitalWrite(13, LOW); // Turn off the LED
delay(2000); // Wait for two seconds
play('b', 4); //Hot
play('a', 4); //Cross
play('g', 4); //buns
play(' ', 4); //
play('b', 4); //hot
play('a', 4); //cross
play('g', 4); //buns
play(' ', 2); //
play('g', 2); //one
play('g', 2); //a
play('g', 2); //penny
play('a', 2); //two
play('a', 2); //a
play('a', 2); //penny
play(' ', 1); //
play('b', 4); //hot
play('a', 4); //cross
play('g', 4); //buns
}
void play( char note, int beats)
{
int numNotes = 14; // number of notes in our note and frequency array (there are 15 values, but arrays start at 0)
//Note: these notes are C major (there are no sharps or flats)
//this array is used to look up the notes
char notes[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', 'D', 'E', 'F', 'G', 'A', 'B', ' '};
//this array matches frequencies with each letter (e.g. the 4th note is 'f', the 4th frequency is 175)
int frequencies[] = {131, 147, 165, 175, 196, 220, 247, 262, 294, 330, 349, 392, 440, 494, 0};
int currentFrequency = 0; //the frequency that we find when we look up a frequency in the arrays
int beatLength = 150; //the length of one beat (changing this will speed up or slow down the tempo of the song)
//look up the frequency that corresponds to the note
for (int i = 0; i < numNotes; i++) // check each value in notes from 0 to 14
{
if (notes[i] == note) // does the letter passed to the play function match the letter in the array?
{
currentFrequency = frequencies[i]; // Yes! Set the current frequency to match that note
}
}
//play the frequency that matched our letter for the number of beats passed to the play function
tone(speakerPin, currentFrequency, beats * beatLength);
delay(beats * beatLength); //wait for the length of the tone so that it has time to play
delay(50);//a little delay between the notes makes the song sound more natural
}