HELP! Changing the melody using buttons while it's playing

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;
    }
}

your playMelody() function does not return until all the notes have been played (see the for() loop?)

so you need to break that into chunks and non blocking code using millis() for example

For extra information and examples look at

you might benefit from studying state machines. Here is a small introduction to the topic: Yet another Finite State Machine introduction

Make your playMelody() function non-blocking. This means that you have to get rid of the for-loop for starters, use tone() without the duration (if I'm not mistaken) in favour of some logic (possibly a finite state machine) and that you have to call that function as often as possible from loop().

I wanted to give it a quick try

thank you

You need to study the code now!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.