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.
Momentary Push and Release (MP3 plays until the end of the track) - WORKS
Push and Hold (MP3 plays while the button is held and stop when released) - NOT WORKING
Hold and Release (One MP3 plays on button hold and another plays on release) - NOT WORKING
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
}
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.
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.