Go Down

Topic: New and Improved DFPlayer Mini Library!! (Read 13419 times) previous topic - next topic

Power_Broker

Yes, but with one small edit: in order to use multiple software serial ports, you must call SWSerialPort.listen() (SWSerialPort being either Serial1 or Serial2 in your case) before each call from the class DFPlayerMini_Fast.

Here's an example:

Code: [Select]


#include <DFPlayerMini_Fast.h>
#include <SoftwareSerial.h>

SoftwareSerial Serial1(2, 3); // RX, TX
SoftwareSerial Serial2(4, 5); // RX, TX

DFPlayerMini_Fast myMP3One;
DFPlayerMini_Fast myMP3Two;
// Or DFPlayerMini_Fast myMP3One, myMP3Two;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(9600);
  Serial2.begin(9600);

  myMP3One.begin(Serial1);
  myMP3Two.begin(Serial2);
 
  Serial.println("Setting volume to max");
  Serial1.listen();    //<---------------------------------- #
  myMP3One.volume(30);
  Serial2.listen();    //<---------------------------------- #
  myMP3Two.volume(30);
  delay(20);
 
  Serial.println("Playing track 1 player 1 for 5 sec");
  Serial1.listen();    //<---------------------------------- #
  myMP3One.play(1);
  Serial.println("Playing track 2 player 2 for 5 sec");
  Serial2.listen();    //<---------------------------------- #
  myMP3Two.play(2);
  delay(5000);

  Serial.println("Looping track 1");
  Serial1.listen();    //<---------------------------------- #
  myMP3One.loop(1);
}

void loop()
{
  //do nothing
}
"The desire that guides me in all I do is the desire to harness the forces of nature to the service of mankind."
   - Nikola Tesla

psycho774

I've been working with the player for a while and my latest project requires determining when a file has finished playing. I don't see a way to do that in this library, can you provide example code?

Thanks!

Power_Broker

#17
Jun 07, 2019, 09:35 pm Last Edit: Jun 07, 2019, 09:36 pm by Power_Broker
I've been working with the player for a while and my latest project requires determining when a file has finished playing. I don't see a way to do that in this library, can you provide example code?

Thanks!
I've never had to worry about it for my projects, but I know it is possible to do that - just not with this library in it's current state. One day I'll add query commands to the library, but I've been putting it off lately.

Can you describe your project?
"The desire that guides me in all I do is the desire to harness the forces of nature to the service of mankind."
   - Nikola Tesla

Power_Broker

I've done some preliminary testing and I've discovered how to determine if a song is playing or not. First, in order to send the query, you must send the following packet (in order):

- $S: 0x7E
- VER: 0xFF
- Len: 0x6
- CMD: 0x42
- Feedback: 0x01
- para1: 0x00
- para2: 00
- checksum: 0x-- <-- This is automatically calculated by my library (I don't have the exact value written down)
- $0: 0xEF

The response is always in two packets. Here are example response packets for both when a track is playing and when it isn't:

Track is playing response:
7E FF 64 20 21 FE B6 EF ----- Response packet #1
7E FF 64 10 00 FE BA EF ----- Response packet #2

Track is not playing response:
7E FF 64 20 20 FE B7 EF ----- Response packet #1
7E FF 64 10 00 FE BA EF ----- Response packet #2

From these examples, you can tell the track playing status by seeing if the 10th byte of the first response packet is a 1 or a 0.

I should be able to have logic around this query implemented into the library soon.
"The desire that guides me in all I do is the desire to harness the forces of nature to the service of mankind."
   - Nikola Tesla

xl97

In another thread about this module.. there was some discussion about how (the default) lib sometimes reported the file had stopped twice..

I dont recall the details.. just the topic..  might be worth a read when you go your path of updating..



Power_Broker

#20
Jun 08, 2019, 01:05 am Last Edit: Jun 08, 2019, 01:06 am by Power_Broker
I did some quick and dirty coding to implement the new member function trackIsPlaying() and it works!

Below is the example code I used to test the new function:
Code: [Select]

#include <DFPlayerMini_Fast.h>

DFPlayerMini_Fast myMP3;

void setup()
{
  Serial.begin(115200);
  while(!Serial);
  Serial4.begin(9600);

  //this program uses a hardware serial port (i.e. on a Mega), but a softwareserial port can be used
  myMP3.begin(Serial4);

  delay(1000);
 
  myMP3.play(1);
  delay(1000);

  Serial.println("hi");

  if(myMP3.trackIsPlaying())
    Serial.print("Track is playing");
  else
    Serial.print("Track is NOT playing");
}

void loop()
{
  // do nothing
}


Here is a link to the library repo.


"The desire that guides me in all I do is the desire to harness the forces of nature to the service of mankind."
   - Nikola Tesla

Go Up