DFPlayer stop button

Hello all, sorry sorry for posting something about DFPlayer again, there are so many out there, I spent the whole day on this idiot question, but I did not manage to solve it.
I need to Play/Stop a song with a button via arduino nano. It works via serial, but when a song is played by pressing a button, the loop stucks and does not read any input until the end of the song :frowning: Please help..

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    player.volume(10);
    player.play(1);
    // ok, this works, and the song starts
  } else {
//    whatever.  this code does not exists until the song is finished.
    player.stop();
  }
}

While buttonState is HIGH, your code snippet is sending out a continuous stream of volume and play commands.

While buttonState is LOW, it is sending out a continuous stream of stop commands.

Is that what you want to happen? A non stop stream of commands to the DFPlayer? Because that doesn't seem all that useful.

Or do you want a single volume and play command sequence to be sent when buttonState changes from LOW to HIGH, and a single stop command to be sent when buttonState changes from HIGH to LOW?

Hello @van_der_decken thank you for your help.

Yes. Exactly That!

I see your point about the stream of commands! I can fix that with a boolean, but that did not seem to solve the freeze thing...

[edit] yes it does! :see_no_evil:

A

Look at the State Change Detection example (File->examples->02.digital->State Change Detection) to learn how to detect when a button gets pressed, not if a button is pressed.

what an idIOT I am!
now it is working:

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH && !isPlaying) {
    player.volume(5);
    // Play the first MP3 file on the SD card
    player.play(1);
    isPlaying = true;
  } else if (buttonState == LOW && isPlaying) {
    player.stop();
    isPlaying = false;
  }
}

I made the mistake when I put only else rather than the complete else if (buttonState == LOW && isPlaying)

Thank you all, I would really like to offer you guys a couple of beers..