Robot Art Show
This is the code the we used: /*
SparkFun Inventor’s Kit
Circuit 2A - Buzzer
Play notes using a buzzer connected to pin 10
This sketch was written by SparkFun Electronics, with lots of help from the Arduino community.
This code is completely free for any use.
View circuit diagram and instructions at: https://learn.sparkfun.com/tutorials/sparkfun-inventors-kit-experiment-guide---v40
Download drawings and code at: https://github.com/sparkfun/SIK-Guide-Code
*/
int speakerPin = 10; //the pin that buzzer is connected to
void setup()
{
pinMode(13, OUTPUT); // Set pin 13 to output
pinMode(12, OUTPUT); // Set pin 12 to output
pinMode(11, OUTPUT); // Set pin 11 to output
pinMode(9, OUTPUT); // Set pin 9 to output
pinMode(speakerPin, OUTPUT); //set the output pin for the speaker
}
void loop()
{
play('b', 3); //ha
digitalWrite(13, HIGH); // Turn on the LED
play('a', 3); //ppy
digitalWrite(13, LOW); // Turn on the LED
play('h', 3); //birth
play('a', 3); //day
play('C', 3); //to
play(' ', 3); //you
play('D', 3); //pause for 2 beats
digitalWrite(12, HIGH); // Turn on the LED
play('C', 3); //ha
digitalWrite(12, LOW); // Turn on the LED
play('b', 3); //ppy
play('C', 3); //birth
play('E', 3); //day
play(' ', 3); //to
play('F', 3); //pause for 2 beats
digitalWrite(11, HIGH); // Turn on the LED
play('E', 3); //ha
digitalWrite(11, LOW); // Turn on the LED
play('I', 3); //ppy
digitalWrite(11, HIGH); // Turn on the LED
play('E', 3); //birth
digitalWrite(11, LOW); // Turn on the LED
play('B', 3); //day
digitalWrite(11, HIGH); // Turn on the LED
play('A', 3); //dear
digitalWrite(11, LOW); // Turn on the LED
play('J', 3); //your
play('A', 3); //your
play('B', 3); //your
play('A', 3); //your
play('J', 3); //your
play('A', 3); //your
play('B', 3); //your
play(' ', 9); //your
digitalWrite(9, HIGH); // Turn on the LED
play('A', 3); //name
digitalWrite(9, LOW); // Turn on the LED
play('B', 3); //pause for 2 beats
digitalWrite(13, HIGH); // Turn on the LED
play('B', 3); //birth
digitalWrite(13, LOW); // Turn on the LED
play('A', 3); //day
digitalWrite(12, HIGH); // Turn on the LED
play('G', 3); //to
digitalWrite(12, LOW); // Turn on the LED
play('A', 3); //you
digitalWrite(12, HIGH); // Turn on the LED
play('B', 3); //birth
digitalWrite(12, LOW); // Turn on the LED
play('A', 3); //day
digitalWrite(12, HIGH); // Turn on the LED
play('G', 3); //to
digitalWrite(12, LOW); // Turn on the LED
play('A', 3); //you
play('B', 3); //birth
play('A', 3); //day
play('G', 3); //to
play('A', 3); //you
digitalWrite(13, HIGH); // Turn on the LED
digitalWrite(12, HIGH); // Turn on the LED
digitalWrite(11, HIGH); // Turn on the LED
digitalWrite(9, HIGH); // Turn on the LED
play('E', 3); //you
digitalWrite(13, LOW); // Turn on the LED
digitalWrite(12, LOW); // Turn on the LED
digitalWrite(11, LOW); // Turn on the LED
digitalWrite(9, LOW); // Turn on the LED
play(' ', 20);
//while (true) {} //get stuck in this loop forever so that the song only plays once
}
void play( char note, int beats)
{
int numNotes = 18; // 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 g# D# G# 5G# 5G 6C
char notes[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'h', 'C', 'D', 'I', 'E', 'F', 'G', 'A', 'B', ' ', 'J', 'K', 'L', 'M', 'N'};
//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, 208, 262, 294, 311, 330, 349, 392, 440, 494, 0, 415, 500, 831, 784, 1047};
int currentFrequency = 0; //the frequency that we find when we look up a frequency in the arrays
int beatLength = 40; //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
}
/* CHART OF FREQUENCIES FOR NOTES IN C MAJOR
Note Frequency (Hz)
c 131
d 147
e 165
f 175
g 196
a 220
b 247
g# 415
C 262
D 294
E 330
F 349
G 392
A 440
B 494
*/