Jenna’s Updated Circuit 6

For my revision to circuit 6, I edited the music and added a little LED flair.

Special shout out to Disney/MiceChat for the sweet it’s a small world character graphics.

 

The full edited code can be found below:

//Edited Arduino Code 6 – it’s a small world version

int speakerPin = 9;
int LED1 = 10;
int LED2 = 11;
int LED3 = 12;

int length = 36; // the number of notes
char notes[] = “ffafggg ggbgaaa aaCabbbagcef “; // a space represents a rest
int beats[] = { 4, 1, 2, 2, 4, 1, 3, 0, 0, 0, 4, 1, 2, 2, 4, 1, 3, 0, 0, 0, 4, 1, 2, 2, 4, 1, 3, 1, 1, 6, 6, 6, 0, 0 , 0, 0 };
int tempo = 100;

void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}

void playNote(char note, int duration) {
char names[] = { ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘a’, ‘b’, ‘C’ };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1070, 956 };

// play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}

void setup() {
pinMode(speakerPin, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);

}

void loop() {
for (int i = 0; i < length; i++) {
// Turn on LEDs
if (notes[i] == ‘c’ || notes[i] == ‘f’ || notes[i] == ‘b’){
digitalWrite(LED1, HIGH);}
else if (notes[i] ==’d’ || notes[i]==’g’ || notes[i]==’C’) {
digitalWrite(LED2, HIGH);
}
else
digitalWrite(LED3, HIGH);

//Play notes
if (notes[i] == ‘ ‘) {
delay(beats[i] * tempo); // rest
} else {
playNote(notes[i], beats[i] * tempo);
}

digitalWrite(LED1, LOW); //turn LEDS off
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);

// pause between notes
delay(tempo / 2);

}
}

 

 

 

** REVISION:

I realized after posting that the part of the code to include rests in between notes is incorrect. In the notes vector, it should only include one space per rest and the length of the rest should be modified by the corresponding number in the beats vector. I tried to edit and reupload, but my piezo speaker lost a leg in the process! đŸ˜¦