Hey all!
One of the downsides to the DFPlayer Pro was the exclusion of a busy pin and until recently there was no isPlaying function in the Pro's library.
Here's a link to where I found out the news - ISPLAYING FUNCTION? · Issue #3 · DFRobot/DFRobot_DF1201S · GitHub
Here's a link to where you can download the new library - GitHub - DFRobot/DFRobot_DF1201S: Here comes the DFPlayer Pro-a mini simple but powerful MP3 Player! This MP3 player module supports four controlling modes: Arduino, AT command, on-board buttons, and ADKEY. You can directly press the on-board button to play or switch music without using a controller. By using a USB cable, you can easily copy your favorite songs into this module to play them any where you want, or use it as a sound card for your PC or Raspberry Pi after connecting them together.
As of writing this post, updating or downloading the latest library through the Arduino IDE software doesn't install the version linked above. I had to download it from there to get the right one installed. It's confusing because the latest version is listed as being version 1.0.2 but inside the updated .cpp file it is named "V1.0"
If you're not sure you have the right one installed, just scroll down the .cpp file and look for the isPlaying function - bool DFRobot_DF1201S::isPlaying() (it should be on line 208)
Now to the problem.
I have no idea how to implement this function. The DFPlayer Mini had a similar function in some libraries, but I'm not sure if it worked exactly the same way.
I want to use a momentary push button to turn an LED on and off with each press of the button. At the same time, it should also play a song and stop and currently playing song.
When the light comes on the track plays. When the light goes out the track stops.
Before finding out about the new library, I was trying to use the pause function to stop the track. After some research, I think the pause function has always been broken in the old DFPlayer Pro library, which explains why I could never get it to work.
However, this was apparently addressed in the same update that added the isPlaying function to the library.
It does work, but there's a new issue. I'm not sure if it's my code at fault or if the "fix" to the library didn't fully address the problem.
I'll post the code and then I'll explain what's happening. I'm using an Arduino UNO and the Arduino IDE
#include <DFRobot_DF1201S.h>
#include <SoftwareSerial.h>
int transistorPin = 4; // transistor turns the speaker on
int transistorState=0;
int LEDState=0;
int LEDPin=8;
int buttonPin=12;
int buttonNew;
int buttonOld=1;
int dt=50; // 50 instead of 100 allows for more rapid button presses
int ddt=1000;
SoftwareSerial DF1201SSerial(2, 3); //RX TX (might be the other way around, i swear it keeps changing)
DFRobot_DF1201S DF1201S;
void setup() {
Serial.begin(115200);
DF1201SSerial.begin(115200);
while(!DF1201S.begin(DF1201SSerial)){
Serial.println("Init failed, please check the wire connection!");
delay(ddt);
}
pinMode(transistorPin, OUTPUT);
pinMode(LEDPin, OUTPUT);
pinMode(buttonPin, INPUT);
DF1201S.setPrompt(false); // Silence the voice prompt on Start-up
DF1201S.switchFunction(DF1201S.MUSIC);
DF1201S.setPlayMode(DF1201S.SINGLE);
Serial.print("PlayMode:");
Serial.println(DF1201S.getPlayMode());
DF1201S.setVol(12); // Range 0 - 30
Serial.print("VOL:");
Serial.println(DF1201S.getVol());
Serial.println(" ");
Serial.println("Welcome!");
Serial.println(" ");
}
void loop() {
digitalRead(transistorPin);
if (transistorState==0){
digitalWrite(transistorPin, HIGH);
transistorState=1;
Serial.println("Waiting for user input.");
}
buttonNew=digitalRead(buttonPin);
if(buttonOld==0 && buttonNew==1){
if(LEDState==0){
digitalWrite(LEDPin,HIGH);
DF1201S.playFileNum(4);
LEDState=1;
Serial.println("LED On");
Serial.println("Playing Track");
}
else{
digitalWrite(LEDPin, LOW);
DF1201S.pause();
LEDState=0;
Serial.println("LED Off");
Serial.println("Nothing Playing");
}
}
buttonOld=buttonNew;
delay(dt);
}
I'm using the transistor to delay the speaker. This means the speaker activates after the player has booted up and avoids the small pop you get when powering on.
The rest of the code almost works.
The light comes on and turns off as expected with each button press. No problem.
However.
The track plays on the first button press (this is expected) . The track restarts on the second button press (this is wrong, it should turn off). The track restarts again on the third button press (this is fine) and pauses on the fourth button press (again this is fine). From here, the code works as intended. It will turn the light on and start the track on the next press and turn the light off and stop the track on the following press. It will continue this way until the Arduino is reset.
In summary, it's just the second button press that is messed up. Again, not sure if it's my code that's wrong, or if the pause function of the player is not implemented correctly or setup correctly in the library.
There's going to be a flaw with my plan anyway. Even when I reach the stage in the code when everything is working as I want it to, when the track finishes playing on it's own, I'm left with a light on and no sound playing. This means when I press the button again the light turns off and the track starts. I don't want this to happen.
I think the isPlaying function might help with this if I knew how to implement it in my code. If anyone has any ideas, I'd really appreciate it. I hope all of that made sense and I hope the new library is useful to someone, provided the changes made actually work!
Thank you.
3 posts - 2 participants