Arduino Forum

Community => Exhibition / Gallery => Topic started by: Power_Broker on Nov 23, 2017, 07:27 am

Title: New and Improved DFPlayer Mini Library!!
Post by: Power_Broker on Nov 23, 2017, 07:27 am
Problem: The original DFPlayer Mini library is relatively slow and difficult to understand for newbies. After reading the datasheet/tutorials, I noticed that controlling the player could be a lot simpler.

Solution: Create my own library that is much simpler and easier to understand.


Here (https://github.com/PowerBroker2/DFPlayerMini_Fast) is a link to the final product!



If you think it can be improved, please let me know! (this is my first custom library)
Title: Re: New and Improved DFPlayer Mini Library!!
Post by: pert on Nov 23, 2017, 11:08 am
Thanks for your contribution!

This is a bit of a nitpick but you did ask for input:
The Arduino library specification (https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5:-Library-specification#library-examples) states:
Quote
Library examples must be placed in the examples folder.
...
Sketches contained inside the examples folder will be shown in the Examples menu of the IDE.
However, the Arduino IDE actually does show all sketches bundled with a library in the File > Examples menu regardless of their folder location so actually your example is currently fine where it is, even though it does not meet the specification. However, I always think it's a good idea to follow the specification unless there is a good reason not to. You have no guarantee that the IDE will retain this behavior in the future whereas I guarantee that the handling of the examples folder is a permanent feature.
Title: Re: New and Improved DFPlayer Mini Library!!
Post by: Power_Broker on Nov 23, 2017, 11:13 am
Thanks! I didn't notice the whole examples folder thing. I tested it and the example does show up in the IDE, but I'll change it to be in regulations anyway  ;)
Title: Re: New and Improved DFPlayer Mini Library!!
Post by: Power_Broker on Nov 25, 2017, 07:41 am
I recently added a few features such as changing EQ, pause, and resume.
Title: Re: New and Improved DFPlayer Mini Library!!
Post by: smeand on Dec 19, 2017, 07:51 pm
Did you delete the example file? Where can I get the reference doc for your lib?
Title: Re: New and Improved DFPlayer Mini Library!!
Post by: Power_Broker on Dec 19, 2017, 10:04 pm
Did you delete the example file? Where can I get the reference doc for your lib?
Yeah, I deleted it. I tested the sketch earlier and everything worked except for the resume(). I'll eventually try to fix that function (my guess is the resume command ID is wrong, but not sure yet), but I don't have the hardware right now to do that. Until then, I didn't want an example sketch posted that didn't work 100%.


Here is the example posted I had on github earlier:
Code: [Select]
#include <SoftwareSerial.h>
#include <DFPlayerMini_Fast.h>


SoftwareSerial mySerial(10, 11); // RX, TX
DFPlayerMini_Fast myDFPlayer;


void setup()
{
  Serial.begin(115200);
  mySerial.begin(9600);
 
  myDFPlayer.begin(mySerial);
}


void loop()
{
  myDFPlayer.play(1); //play mp3 file with leading identifier "0001"

  myDFPlayer.volume(1); //set volume low

  delay(100); //wait 100ms

  myDFPlayer.volume(30); //crank that stuff!

  delay(100); //wait another 100ms

  myDFPlayer.EQSelect(1); //set EQ as pop

  delay(100); //wait another 100ms

  myDFPlayer.EQSelect(5); //set EQ as bass

  delay(100); //wait another 100ms

  myDFPlayer.pause(); //pause player

  delay(100); //wait another 100ms

  myDFPlayer.resume(); //resume player

  delay(100); //wait another 100ms

  myDFPlayer.loop(2); //loop mp3 file with leading identifier "0002"

  while(1); //halt
}




If you have any questions, I'd be happy to answer them.
Title: Re: New and Improved DFPlayer Mini Library!!
Post by: xl97 on Mar 07, 2018, 04:32 pm
* Cant believe this post didnt turn up in my MANY previous searches!!

I'm going to give this lib a try...

The current DFRobot lib I am using is giving me trouble  (or it may be the module itself)..

Maybe your lib will behave better.  My current issue(s) are:

1.) When doing just a regular myDFPlayer.play(#) command...    then trying to check for when a file has finished/completed playback..  it always immediately says its completed.. (when it just started)

using a snippet of their printDetail() function..  that checks for:  DFPlayerPlayFinished

If I used disableLoop() and disableLoop().. then the track reliably was 'wacthed' and reported playback completed accurately..  (however that audio file is NOT supposed to be a loop!..but was only way I could get it to accurately detect when finished)...   so this file would sometimes get stuck in a loop...

Does your lib have some way to reliably detect when a file is finished playing?

I still dont see any examples folder?  Was this ever finished?  (is the lib ready for use?)


Title: Re: New and Improved DFPlayer Mini Library!!
Post by: jaredm2195 on Jun 13, 2018, 03:07 am
running this code:

#include <DFRobotDFPlayerMini.h>



/***************************************************
DFPlayer - A Mini MP3 Player For Arduino
<https://www.dfrobot.com/index.php?route=product/product&product_id=1121>

***************************************************
This example shows the basic function of library for DFPlayer.

Created 2016-12-07
By [Angelo qiao](Angelo.qiao@dfrobot.com)

GNU Lesser General Public License.
See <http://www.gnu.org/licenses/> for details.
All above must be included in any redistribution
****************************************************/

/***********Notice and Trouble shooting***************
1.Connection and Diagram can be found here
<https://www.dfrobot.com/wiki/index.php/DFPlayer_Mini_SKU:DFR0299#Connection_Diagram>
2.This code is tested on Arduino Uno, Leonardo, Mega boards.
****************************************************/

#include <Arduino.h>
#include <SoftwareSerial.h>


SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);

void setup()
{
 mySoftwareSerial.begin(9600);
 Serial.begin(9600);
 
 Serial.println();
 Serial.println(F("DFRobot DFPlayer Mini Demo"));
 Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
 
 if (!myDFPlayer.begin(mySoftwareSerial)) {  //Use softwareSerial to communicate with mp3.
   Serial.println(F("Unable to begin:"));
   Serial.println(F("1.Please recheck the connection!"));
   Serial.println(F("2.Please insert the SD card!"));
   while(true);
 }
 Serial.println(F("DFPlayer Mini online."));
 
 myDFPlayer.volume(10);  //Set volume value. From 0 to 30
 myDFPlayer.play(1);  //Play the first mp3
}

void loop()
{
 static unsigned long timer = millis();
 
 if (millis() - timer > 3000) {
   timer = millis();
   myDFPlayer.next();  //Play next mp3 every 3 second.
 }
 
 if (myDFPlayer.available()) {
   printDetail(myDFPlayer.readType(), myDFPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states.
 }
}

void printDetail(uint8_t type, int value){
 switch (type) {
   case TimeOut:
     Serial.println(F("Time Out!"));
     break;
   case WrongStack:
     Serial.println(F("Stack Wrong!"));
     break;
   case DFPlayerCardInserted:
     Serial.println(F("Card Inserted!"));
     break;
   case DFPlayerCardRemoved:
     Serial.println(F("Card Removed!"));
     break;
   case DFPlayerCardOnline:
     Serial.println(F("Card Online!"));
     break;
   case DFPlayerPlayFinished:
     Serial.print(F("Number:"));
     Serial.print(value);
     Serial.println(F(" Play Finished!"));
     break;
   case DFPlayerError:
     Serial.print(F("DFPlayerError:"));
     switch (value) {
       case Busy:
         Serial.println(F("Card not found"));
         break;
       case Sleeping:
         Serial.println(F("Sleeping"));
         break;
       case SerialWrongStack:
         Serial.println(F("Get Wrong Stack"));
         break;
       case CheckSumNotMatch:
         Serial.println(F("Check Sum Not Match"));
         break;
       case FileIndexOut:
         Serial.println(F("File Index Out of Bound"));
         break;
       case FileMismatch:
         Serial.println(F("Cannot Find File"));
         break;
       case Advertise:
         Serial.println(F("In Advertise"));
         break;
       default:
         break;
     }
     break;
   default:
     break;
 }

}



and getting this error in the serial monitor:

DFRobot DFPlayer Mini Demo
Initializing DFPlayer ... (May take 3~5 seconds)
Unable to begin:
1.Please recheck the connection!
2.Please insert the SD card!
it won't allow me to play the sound files. I am using mp3 files.
Title: Re: New and Improved DFPlayer Mini Library!!
Post by: Power_Broker on Sep 01, 2018, 03:47 am
This example code has nothing to do with my new library.
Title: Re: New and Improved DFPlayer Mini Library!!
Post by: revrickv on Sep 03, 2018, 04:16 am
I am also getting the errors

DFRobot DFPlayer Mini Demo
Initializing DFPlayer ... (May take 3~5 seconds)
Unable to begin:
1.Please recheck the connection!
2.Please insert the SD card!
it won't allow me to play the sound files. I am using mp3 files.

it was working 3 weeks ago finaly finshed other code for excape room whent to add
music now get error
Title: Re: New and Improved DFPlayer Mini Library!!
Post by: Power_Broker on Sep 26, 2018, 11:50 pm
Does your lib have some way to reliably detect when a file is finished playing?

I still dont see any examples folder?  Was this ever finished?  (is the lib ready for use?)
Sorry for not getting back to you sooner, I don't visit this forum nearly as much as I used to.

There aren't any example programs other than this one (similarly to the one I posted above, just without the pause()):

Code: [Select]

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


SoftwareSerial mySerial(10, 11); // RX, TX
DFPlayerMini_Fast myDFPlayer;


void setup()
{
  Serial.begin(115200);
  mySerial.begin(9600);
 
  myDFPlayer.begin(mySerial);
}


void loop()
{
  myDFPlayer.play(1); //play mp3 file with leading identifier "0001"

  delay(100); //wait 100ms

  myDFPlayer.volume(1); //set volume low

  delay(100); //wait another 100ms

  myDFPlayer.volume(30); //crank that stuff!

  delay(100); //wait another 100ms

  myDFPlayer.EQSelect(1); //set EQ as pop

  delay(100); //wait another 100ms

  myDFPlayer.EQSelect(5); //set EQ as bass

  delay(100); //wait another 100ms

  myDFPlayer.pause(); //pause player

  delay(100); //wait another 100ms

  myDFPlayer.loop(2); //loop mp3 file with leading identifier "0002"

  while(1); //halt
}


Also, in order to keep the code as streamlined as possible (and use as few pins as possible), the library doesn't use any feedback from the mp3 module itself.

I don't think there is a way to really check if a file has finished playing - nomatter what library you use.
Title: Re: New and Improved DFPlayer Mini Library!!
Post by: xl97 on Sep 27, 2018, 02:23 am
The DFRobot lib does offer a way to check for audio clip completion.

(its happens to have a bug in it however.. where it was sometimes firing twice.... but someone posted there work-around for it)

There is also the 'busy' pin if you want a physical approach.. (but its too slow to be used for anything timing critical or reactive..etc)
Title: Re: New and Improved DFPlayer Mini Library!!
Post by: Bobbs on Sep 30, 2018, 03:52 pm
I am also getting the errors

DFRobot DFPlayer Mini Demo
Initializing DFPlayer ... (May take 3~5 seconds)
Unable to begin:
1.Please recheck the connection!
2.Please insert the SD card!
it won't allow me to play the sound files. I am using mp3 files.

it was working 3 weeks ago finaly finshed other code for excape room whent to add
music now get error
Change the sketch from

 if (!myDFPlayer.begin(mySoftwareSerial)) {  //Use softwareSerial to communicate with mp3.
   Serial.println(F("Unable to begin:"));
   Serial.println(F("1.Please recheck the connection!"));
   Serial.println(F("2.Please insert the SD card!"));
   while(true);
 }

to

  while(!myDFPlayer.begin(mySoftwareSerial))
  {
      mySoftwareSerial.begin(9600);
      delay(500);
  }

It worked with my Arduino Uno
Title: Re: New and Improved DFPlayer Mini Library!!
Post by: Power_Broker on Mar 18, 2019, 12:50 am
Today I updated the library to add a lot more functionality.

So far all of the commands have been implemented, but I still have yet to add the queries. I plan on adding those sometime in the not so terribly far out future.
Title: Re: New and Improved DFPlayer Mini Library!!
Post by: blackphoenixpt on Mar 20, 2019, 10:51 am
Hello. Can you give me an example of your library controlling 2 players at the same time? SoftSerial1 pins 2,3 and SoftSerial2 pins 4,5. Serial will be used to debug messages from Arduino to the IDE terminal. I want to play a background music in loop with one player and another player will play sounds as needed when needed. I will mix the 2 players into one output and have a multichannel mp3 player. Like a beatbox. is it something like this?:
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");
  myMP3One.volume(30);
  myMP3Two.volume(30);
  delay(20);
 
  Serial.println("Playing track 1 player 1 for 5 sec");
  myMP3One.play(1);
  Serial.println("Playing track 2 player 2 for 5 sec");
  myMP3Two.play(2);
  delay(5000);

  Serial.println("Looping track 1");
  myMP3One.loop(1);
}

void loop()
{
  //do nothing
}
Title: Re: New and Improved DFPlayer Mini Library!!
Post by: Power_Broker on Mar 20, 2019, 10:19 pm
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
}