I wrote a program to play bit music using my Nano and a small piezo buzzer. Everything works fine except it won't update the counter I set up to change the song when I press the rotary encoder. If I remove the function that plays the song, it will update the counter and move on to the next song but then gets stuck again. I'm using a state machine to update the counter. Each button press should increase the counter by 1. Can you guys help me please?
void loop() {
switch(State) { //Switches between states
case A: //A state
if(digitalRead(Push) == LOW) {
Timer = millis(); //Records time to Timer
State = B; //Sets state to B if button read is LOW
}
break;
case B: //B state
if(digitalRead(Push) == HIGH) {
State = A; //Sets state to A if button read is HIGH
}
else if ((unsigned long) (millis() - Timer >= 5)) { //Timer
Indicator = 1; //Sets indicator to 1
State = C; //Sets state to C
}
break;
case C: //C state
if(digitalRead(Push) == HIGH) {
State = A; //Sets state to A if button read is HIGH
}
break;
}
if (Indicator == 1) {
Indicator = 0; //Clears Indicator
Count += 1; //Adds 1 to Counter
}
//Plays different song upon button press
if(Count == 0 ) {
LcdDriver.setCursor(0,0); //Sets cursor
LcdDriver.print("Press Button"); //Prints to LCD
LcdDriver.setCursor(0,1); //Sets cursor
LcdDriver.print("To Start"); //Prints to LCD
}
if(Count == 1 ) {
LcdDriver.clear(); //Clears LCD
LcdDriver.setCursor(0,0); //Sets cursor
LcdDriver.print("Hello by Adele"); //Prints to LCD
hello(); //Plays song
delay(5000); //Pauses for 5 seconds and plays again
}
if(Count == 2 ) {
LcdDriver.clear(); //Clears LCD
LcdDriver.setCursor(0,0); //Sets cursor
LcdDriver.print("Star Spangled Banner"); //Prints to LCD
star_spangled_banner(); //Plays song
delay(5000); //Pauses for 5 seconds and plays again
}
if(Count == 3 ) {
Count = 0; //Sets count to 0
Indicator = 0; //Sets Indicator to 0
}
}