Do action while button is held

Hi,

I am using DFPlayer Mini with this library: GitHub - PowerBroker2/DFPlayerMini_Fast: Fast and easy to understand Arduino library to use the DFPlayer Mini MP3 module from DFRobot.com. This is a huge improvement (both in terms of execution speed and simplicity) to the standard library provided by DFRobot.com.

I am trying to get a sound to play from the DFPlayer WHILE a button is held down and then stop playing when the button is released.

I have tried various ways with IF statements, While loops etc but I'm not getting anywhere fast.

Everything I try falls foul of the play() function blocking or holding up everything else until play is complete and I don't know how to get around it.

I am building a sound board for a friend and I need three different button actions.

  1. Momentary Push and Release (MP3 plays until the end of the track) - WORKS
  2. Push and Hold (MP3 plays while the button is held and stop when released) - NOT WORKING
  3. Hold and Release (One MP3 plays on button hold and another plays on release) - NOT WORKING

Any help greatly received.

I've been trying for days.

A better way to think about this problem is to start the sound when the button changes from not-pressed to pressed and stop the sound when the button changes from pressed to not-pressed. Something like this pseudo code

prevButtonState = buttonState;
newButtonState = digitalRead(buttonPin);
if (newButtonState == LOW and prevButtonState == HIGH) { // assumes LOW when pressed)
   // start sound
}
if (newButtonState == HIGH and prevButtonState == LOW) { 
   // stop sound
}

...R

Thank you Robin2.

Unfortunately, once myDFPlayer.play() has been called, it blocks the program from running any further until playback has ended so it never gets as far as checking to see if the button state has changed. The program just gets locked at that point until play() has finished it’s operation.

I’ve examined the library but I’m no expert so I can’t tell what’s causing the ‘blocking’ action.

For some reason, the library calls for the mp3 file to play until completion.

Everything I’ve tried has resulted in unwanted behavior from not playing at all, to trying to start play over and over while the button is held.

I have tried setting flags to keep track of states but that’s not the issue. It’s this blocking that the play() function does that stops the code from checking the state of the button once called.

It really has left me scratching my head lol

pi_and_chips:
Unfortunately, once myDFPlayer.play() has been called, it blocks the program from running any further until playback has ended so it never gets as far as checking to see if the button state has changed. The program just gets locked at that point until play() has finished it's operation.

I've examined the library but I'm no expert so I can't tell what's causing the 'blocking' action.

Alas, like so many other Arduino libraries there is no documentation.

I notice some functions such as isPlaying() and pause() which would only make sense for non-blocking playing

Perhaps you should edit your Original Post and add the name of the library to thee Title to catch the attention of the library's author who is a regular here.

...R

Could you post your code, or a small example that demonstrates this problem?

The API has a stop() and a pause() function. Seems like they'd be pretty useless if you can't break in during the play() function.

I don't know anything about this library, just looked at the available functions in the API

DFPlayer.play() doesn't seem to be blocking when I use that library. Are you sure you have the latest version of the library?

If you post the complete program of your best attempt we can perhaps check to see if the real problem lies elsewhere.

Steve

Thank you everyone for your replies!!

As it turns out, this was the answer I needed.

slipstick:
DFPlayer.play() doesn't seem to be blocking when I use that library.

If it wasn't blocking for you then it shouldn't be blocking for me and your reply confirmed it.

I went back and took another look at my button logic code and the problem was staring me in the face.

How does the switch even know it has changed state?

I hadn't put a call to read the state of the digital button pin again AFTER calling play(); DOH!!!

So this is what I have now and it's working.

//----BUTTON TWO   | PUSH AND HOLD | SINGLE TONE OR SOUND - SOUND WHILE BUTTON IS HELD----------------------------------

  // Read Arduino D4 Pin (Button 2) Value into a Variable
  int buttonTwo = digitalRead(4);

  while(buttonTwo == LOW){
    if(playingTrack == false){
      buttonTwoPressed = true;
      playingTrack = true;
      myDFPlayer.play(1);
        
    }
    buttonTwo = digitalRead(4);
  }

  if(buttonTwo == HIGH && buttonTwoPressed == true){
    buttonTwoPressed = false;
    playingTrack = false;
    myDFPlayer.stop();
  }
  

  //END BUTTON TWO--------------------------------------------------------------------------------------------------------

Thank you!!!!