Please help! Here is a part of the code (I think I don't need the rest), basically I have 8 melodies and I want them to be changeable using two buttons, one for the next melody and the other for the previous melody while the current melody is still playing. I've been trying, but I just can't seem to get it to work.
int Next(){
if (digitalRead(T1) == HIGH && Counter < 9){
Counter ++;
if (Counter == 9){
Counter = 1;
}
}
return Counter;
}
int Previous(){
if (digitalRead(T2) == HIGH && Counter > 0){
Counter --;
if (Counter == 0){
Counter = 8;
}
}
return Counter;
}
void playMelody(int melody[], int notes) {
for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) {
divider = melody[thisNote + 1];
if (divider > 0) {
noteDuration = (wholenote) / divider;
} else if (divider < 0) {
noteDuration = (wholenote) / abs(divider);
noteDuration *= 1.5;
}
tone(buzzer, melody[thisNote], noteDuration * 0.9);
delay(noteDuration);
noTone(buzzer);
}
}
void setup() {
pinMode(T1,INPUT);
pinMode(T2,INPUT);
Serial.begin(9600);
}
void loop() {
Next();
Previous();
switch (Counter) {
case 1:
Serial.println("Harry Potter melody");
playMelody(harryPotterMelody, notesHarryPotter);
break;
case 2:
Serial.println("Super Mario melody");
playMelody(superMarioMelody, notesSuperMario);
break;
case 3:
Serial.println("Happy Birthday melody");
playMelody(happyBirthdayMelody, notesHappyBirthday);
break;
case 4:
Serial.println("Game od Thrones");
playMelody(GameofThronesmelody, notesGameofThrones);
break;
case 5:
Serial.println("Pink Panther");
playMelody(PinkPanthermelody, notesPinkPanther);
break;
case 6:
Serial.println("Star Wars");
playMelody(StarWarsmelody, notesStarWars);
break;
case 7:
Serial.println("Godfather");
playMelody(Godfathermelody, notesGodfather);
break;
case 8:
Serial.println("Lion sleep tonight");
playMelody(LionSleepTonightmelody, notesLionSleepTonight);
break;
}
}