Playing a random sound file on the push of a button DFPlayer Mini

Hi All!

Apologies in advance for all my errors, I am very new to this, so bear with me please :wink:

I am trying to play a randomly chosen single sound file with the DFPlayer Mini connected to an arduino Nano (Old bootloader) when I push a button. (by single I mean that I have a pool of files on the SD Card (no folders), it chooses one of them, plays it once, then stops playing all together)

I have modified the code I found here and I am not using any library because – weirdly enough – the SD Card does not get recognised when using libraries, bu works fine with Hex commands (is that how it is called?)
For now what happens is that when I upload my code it starts playing all the sound files, one after the other, in order (not random), and as a loop (repeats all), and as soon as I push the button it stops playing, until I press the reset button on the arduino and everything starts again from the beginning... I feel just like in groundhog day :o

Anyone of you seeing where my error(s) are in the code, and maybe how to fix them? I would be so ethernally thankful!!!!

#include "SoftwareSerial.h"
SoftwareSerial mySerial(10, 11);
# define Start_Byte 0x7E
# define Version_Byte 0xFF
# define Command_Length 0x06
# define End_Byte 0xEF
// # define 
# define Acknowledge 0x00 //Returns info with command 0x41 [0x01: info, 0x00: no info]

#define ACTIVATED LOW // Button

int button = 4;
boolean isPlaying = false;


void setup () {

  pinMode(button, INPUT);
  digitalWrite(button,HIGH);

  mySerial.begin (9600);

  delay(1000);
  execute_CMD(0x3F,0,0);  // send initialization parameters
  delay(500);
  setVolume(30);  // sets the volume 0-30
  delay(500);
  execute_CMD(0x08,0,3);  // set the playback mode (3=random)
  delay(500);
  execute_CMD(0x11,0,1);  // repeat play (1=start repeat 0=stop repeat)
  isPlaying = true;
}



void loop () 
{
  if (digitalRead(button) == ACTIVATED)
    play();
}


void pause()
{
  execute_CMD(0x0E,0,0);  // Pause
  delay(500);
}


void play()
{
  execute_CMD(0x0D,0,1);  // Playback
  delay(500);
  execute_CMD(0x11,0,0);  // Repeat play stop
  delay(500);
  
}


void setVolume(int volume)
{
  execute_CMD(0x06, 0, volume);  // specify volume (0-30)
  delay(2000);
}

void execute_CMD(byte CMD, byte Par1, byte Par2)
// Excecute the command and parameters
{
// Calculate the checksum (2 bytes)
word checksum = -(Version_Byte + Command_Length + CMD + Acknowledge + Par1 + Par2);
// Build the command line
byte Command_line[10] = { Start_Byte, Version_Byte, Command_Length, CMD, Acknowledge,
Par1, Par2, highByte(checksum), lowByte(checksum), End_Byte};
//Send the command line to the module
for (byte k=0; k<10; k++)
{
mySerial.write( Command_line[k]);
}
}

Thank you!!!

Hi,
I have the same problem. It's a shame that nobody answered here. have you found a solution by now?

Not sure exactly what you are asking about, but I found the random code command to be fidgety, and if you really want it to play random, and not always play the first track in your list before playing every track after that randomly. You need to put a "delay(500);" code in after the random play command, and after that you need a "myDFPlayer.next();" command. In the data sheet for the DFplayer it mentions that it always plays the first track on the list even with the random play code, so that "next" line skips it past the first song. I think you can get the DFplayer to stop after a random song with the "break" command... I had it stopping before after a song, but I don't have the code I used from before saved.

Here's a simple block of code that works for me. I too have just an SD with tracks on it and no folders.

#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.EQ(DFPLAYER_EQ_JAZZ);
  myDFPlayer.randomAll(); //Random play all the mp3.
  delay(500);
  myDFPlayer.next();
 
}

void loop()
{
  
  }

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.