DFPlayer mini random play function

Hi, I'm new in programming Arduino (I have an Arduino Uno Rev3 board), so I'm having some problems programming it together with the DFPlayer Mini MP3 Player module.

I connected the DFPlayer MIni to the Arduino board in I / O mode, as explained in the WIKI of the official site;
my aim is to have a simple code that has:

  1. autostart function - it starts to play MP3s on the micro SD card as soon as the Arduino interface is powered up, without the aid of physical buttons (play, stop, etc)
  2. Random Play for all mp3 songs on the micro SD, INCLUDING the first song (I explain better: even the first song must be randomly played every time the interface is powered)
  3. Stop at the completion of the mp3 list on the micro SD.

I used the following code suggested and posted by AVRDUDE64 in this forum some years ago (link: Playing a random sound file on the push of a button DFPlayer Mini - #2 by maximus_decimus_meridius)

#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"


SoftwareSerial mySoftwareSerial(10, 11); // RX, TX

DFRobotDFPlayerMini myDFPlayer;

void setup()
{
  mySoftwareSerial.begin(9600);
  Serial.begin(115200);
  
  if (!myDFPlayer.begin(mySoftwareSerial)) {  //Use softwareSerial to communicate with mp3.
    while(true){
      delay(0); // Code to compatible with ESP8266 watch dog.
    }
  }

  myDFPlayer.volume(10);  //Set volume value. From 0 to 30
  myDFPlayer.randomAll(); //Random play all the mp3.
  delay(500);
  myDFPlayer.next();
 
}

void loop()
{
  
  }

when tested, this code plays in random mode only the first mp3 everytime power is on (and it's ok for my purpose), but then it stops playing and it does not continue to play next mp3.

Can someone help me? Thanks to anyone who can give me an example of code that works according to my needs .

At a guess, i'd say that you need to call the next() function after the current mp3 has finished playing.

A quick look at the library and I would think that you may need to make repeated calls to readState() to determine if the current track has finished playing. However, looking at the wiki, it suggests that pin 16 of the module is a busy signal with a LOW meaning that a track is currently being played.

Perhaps monitoring this busy signal and waiting for it to go HIGH, and then making another call to the next() function would do what you want.

Thanks for your tips, markd833! But I don't know how to set the commands you tell me; is it possible to show me a code example?

Connect the BUSY pin of your DFPlayer to pin 8 on your UNO.

I can't test this code as I don't have a DFPlayer device, but you should get the idea and be able to get it going if there's an error:

#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

SoftwareSerial mySoftwareSerial(10, 11); // RX, TX

DFRobotDFPlayerMini myDFPlayer;

#define LED_PIN  13
#define BUSY_PIN 8
int busyState = 0;
int prevBusyState = 0;

void setup()
{
  pinMode( LED_PIN, OUTPUT );
  pinMode( BUSY_PIN, INPUT_PULLUP );
  
  mySoftwareSerial.begin(9600);
  Serial.begin(115200);
  
  if (!myDFPlayer.begin(mySoftwareSerial)) {  //Use softwareSerial to communicate with mp3.
    while(true){
      delay(0); // Code to compatible with ESP8266 watch dog.
    }
  }

  myDFPlayer.volume(10);  //Set volume value. From 0 to 30
  myDFPlayer.randomAll(); //Random play all the mp3.
  delay(500);
  myDFPlayer.next();

  // assume we need a delay to give the player time to start playing the initial track
  delay( 1000 );

  // initialise the BUSY states
  busyState = digitalRead( BUSY_PIN );
  prevBusyState = busyState;
}

void loop()
{
  // copy the state of the BUSY pin onto the UNO built-in LED
  busyState = digitalRead( BUSY_PIN );
  digitalWrite( LED_PIN, busyState );

  // has the player BUSY pin changed state?
  if ( busyState != prevBusyState ) {
    // state change - has the player stopped playing?
    if ( busyState == HIGH ) {
      // stopped playing so start next track
      myDFPlayer.next();  
    }
    prevBusyState = busyState;
  }
  delay( 2000 );
}

markd833, you're great!
your code works, without modifying anything!
thank you very much! You are my savior