const int speakerPin=5;
const int button1Pin=11;
const int button2Pin=10;
const int led1Pin=8;
const int led2Pin=7;
const int led3Pin=6;
int songChoice=0;
int numOfSongs=2; // STUDENT -- if you add a song don't forget to change this number
int twinkleLength=48;
char twinkleNotes[]="ccggaag ffeeddc ggffeed ggffeed ccggaag ffeeddc "; // a space represents a rest
int twinkleBeats[]={ 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 3, 1,
1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 3, 1,
1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 3, 1,
1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 3, 1,
1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 3, 1,
1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 3, 1};
int twinkleTempo=300;
int maryLength=31;
char maryNotes[]="edcdeee ddd egg edcdeeeeddedcgc";
int maryBeats[]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
int maryTempo=275;
// STUDENT -- Using the same design as in twinkle and mary, you may create your own song variables here
// int length, char notes array, int beats array, and int tempo
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 261, 293, 329, 349, 392, 435, 460, 492 };
void setup() {
pinMode(speakerPin, OUTPUT);
pinMode(button1Pin, INPUT_PULLUP);
pinMode(button2Pin, INPUT_PULLUP);
}
void loop() {
int button1State=digitalRead(button1Pin); // Start Button
int button2State=digitalRead(button2Pin); // Select Button
char *notes; // These are special variables called pointers. This is an advanced topic.
int *beats;
int length;
int tempo;
if(button2State==LOW) {
songChoice++;
delay(200); // prevent doubletap
if(songChoice>=numOfSongs)
songChoice=0;
}
if(button1State==LOW) { // if the button is pushed, initiate the song
if(songChoice==0) {
notes=twinkleNotes;
beats=twinkleBeats;
length=twinkleLength;
tempo=twinkleTempo;
} else if(songChoice==1) {
notes=maryNotes;
beats=maryBeats;
length=maryLength;
tempo=maryTempo;
} // STUDENT - if you add another song, you must reassign variables with an else if HERE
for (int i = 0; i < length; i++) {
if (notes[i] == ' ') {
delay((beats[i] * tempo)-30); // rest
} else {
for(int j=0; j<length; j++) {
if(names[j]==notes[i]) {
tone(speakerPin,tones[j],beats[i]*tempo);
//--These next three lines make the lights dance--
digitalWrite(led1Pin,j%3);
digitalWrite(led2Pin,(j+1)%3);
digitalWrite(led3Pin,(j+2)%3);
delay(tempo/2+10);
break;
}
}
}
// pause between notes
delay(tempo / 2);
}
}
if(songChoice==0) {
// -- STUDENT --
// This code will happen when the first song is selected
// Put code here to make LED 1 go on
// and make all other LEDs go off
} else if(songChoice==1) {
// -- STUDENT --
// This code will happen when the second song is selected
// Put code here to make LED 2 go on
// and make all other LEDs go off
} // STUDENT - if you add anoter song, you should activate the third LED here
}