Hi, I'm trying to make a christmas sweater that "sings" and has flashing LEDs. So far I've got it working with the test tone of cdef, but when I try to add multiple songs to the mix, my values of "notes[]" and "beats[]" don't update.
The code is supposed to run through once with the song "cdef" and then pick another song at random and play it instead. However, it just plays "cdef" over and over again. I checked, and serial monitor shows that "songsRand" and "notes[]" change at the right times, but after it loops, it just plays "cdef" again.
Hopefully I'm just missing something simple. I had to cut down my code to be within the 900 character limit, but I'm hoping to include 11 songs total that all work if I put them in one at a time as the first spot.
Thanks for looking!
int speakerPin = 13;
int led10 = 12;
int tempo = 150; //originally 300
int songs = 2; //number of songs available
int songsRand = 0; //random songs, starting with test #0
int length = 5; //variable for length of song
char notes[] = "cdef "; //variable for number of notes in a song
int beats[] = { 1,1,1,1,4 }; //variable for beats per note in a song
void setup() {
Serial.begin(9600);
pinMode(speakerPin, OUTPUT);
pinMode(led10, OUTPUT);
}
void loop() {
//****** 000 - Test Song ******
if(songsRand == 0){
int length = 5; // the number of notes
char notes[] = "cdef "; // a space represents a rest
int beats[] = { 2,2,2,4,4 };
Serial.println("Starting with: Test Song");
Serial.print("song notes contain: ");
for (int i = 0; i < length; i++){ Serial.print(notes[i]); } Serial.println("."); Serial.println();
}
//***********************************************
Serial.print("start of void loop, notes to play: ");for (int i = 0; i < length; i++){ Serial.print(notes[i]); } Serial.println("."); Serial.println();
for (int i = 0; i < length; i++) {
if (notes[i] == ' ') {
delay(beats[i] * tempo); // rest
} else {
playNote(notes[i], beats[i] * tempo);
}
// pause between notes
delay(tempo / 2);
}
Serial.println("end of void loop, time to pick a new song!");
int songsRand = random(1,songs);
Serial.print("Random song chosen is: ");Serial.println(songsRand);
//****** 001 - Twinkle Twinkle Little Star ******
if(songsRand == 1){
int length = 15; // the number of notes
char notes[] = "ccggaagffeeddc "; // a space represents a rest
int beats[] = { 1,1,1,1,1,1,2,1,1,1,1,1,1,2,4 };
Serial.println("Song Chosen is: (1) Twinkle Twinkle Little Star");
Serial.print("song notes contain: ");
for (int i = 0; i < length; i++){ Serial.print(notes[i]); } Serial.println("."); Serial.println();
}
//***********************************************
//****** 002 - God Rest Ye Merry Gentlemen ******
if(songsRand == 2){
int length = 69;
char notes[] = "ddaagfedcdefga ddaagfedcdefga avgavCDagfdefgfgavaagfedfedgfgavCDagfed";
int beats[] = { 2,2,2,2,2,2,2,2,2,2,2,2,2,4,2,2,2,2,2,2,2,2,2,2,2,2,2,2,4,2,2,2,2,2,2,2,2,2,2,2,2,2,2,4,2,2,4,2,2,2,2,2,2,4,1,1,2,4,2,2,2,2,2,2,2,2,2,2,8 };
Serial.println("Song Chosen is: (2) God Rest Ye Merry Gentlemen");
Serial.print("song notes contain: ");
for (int i = 0; i < length; i++){ Serial.print(notes[i]); } Serial.println("."); Serial.println();
}
//***********************************************
} // end of void loop
//***********************************************
//*************** Functions *********************
//***********************************************
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', 's', 'g', 'a', 'v', 'b', 'C', 'D', 'E' }; //note "s" is a f#, v is a b-flat.
int tones[] = { 1915, 1700, 1519, 1432, 1352, 1275, 1136, 1073, 1014, 956, 852, 758 };
// play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
digitalWrite(led10, HIGH);
playTone(tones[i], duration);
digitalWrite(led10, LOW);
}
}
}