DFPlayer based mp3 player, struggle playing next tracks based play finished.

Hi All,

I have the dfplayer setup using the following code. I am using DFRobotDFPlayerMini library for accessing DFPlayer. The player has 2 defined folder for each genre of songs. I am using a 2 dimensional array to maintain the folder numbers and the total count in folder. The total count of tracks is dynamically saved to the array during the setup. The changing genre everything works fine. I have put up a code so that everytime Arduino senses the play has finished I check the first dimension of the corresponding genre array to see if the next folder is part of the currently selected genre, if so i start playing the first of the next folder and continue playing. If not then i stop the playback. This is the idea. However, what happens is first time when i use changeGenre in the setup(), the first song of the first genre plays successfully and as i wished the next track is selected at the end. However when the next track is playing, the loop doesn’t seem to wait for the track to finish, it goes on to next loop and finishes. how can i avoid this. here is the code.

SD card setup
01 - introductory music
02,03 - 2 tracks in each folder
04,05 - 2 tracks in each
06,07 - 2 tracks in each

#include <MD_REncoder.h>
#include <DFRobotDFPlayerMini.h>
#include “Arduino.h”
#include “SoftwareSerial.h”

#define ACTIVATED LOW
#define MAKEFAVORITE 4

#define PLAYPAUSE A2

//PIN CONFIGURATION
SoftwareSerial mySoftwareSerial(2, 3);
DFRobotDFPlayerMini myDFPlayer;

void printDetail(uint8_t type, int value);
int _currentGenre = 0;
int _currentFolder = 1;
int _totalTracks = 0;
int _currentTrack =1;
int CHANGEMOOD = 5;
int _favTracks[20];
int _favs = -1;
int _dev[2][2] = {{2,0},{3,0}};
int _oldFilm[2][2] = {{4,0},{5,0}};
int _modern[2][2] = {{6,0},{7,0}};
bool _lastTrack = false;
boolean _isPlaying = true;
boolean _playFinished = false;
void setup() {

// put your setup code here, to run once:
//Register Change Genre
pinMode(CHANGEMOOD, INPUT);
digitalWrite(CHANGEMOOD,HIGH);

//Register Play Pause
pinMode(PLAYPAUSE, INPUT);
digitalWrite(PLAYPAUSE,HIGH);

//Register Make Favorite
pinMode(MAKEFAVORITE, INPUT);
digitalWrite(MAKEFAVORITE,HIGH);

mySoftwareSerial.begin(9600);
Serial.begin(115200);

Serial.println(" ");
Serial.println(“Initializing Player”);

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

myDFPlayer.volume(5); //Set volume value. From 0 to 30
myDFPlayer.setTimeOut(5000);
myDFPlayer.playFolder(1,1);//Play introductory track
delay(5000);
populateTotalTracks(_dev);
populateTotalTracks(_oldFilm);
populateTotalTracks(_modern);
changeGenre();
}

void populateTotalTracks(int arr[2][2])
{
for(int i=0;i<2;i++)
{
arr_[1] = myDFPlayer.readFileCountsInFolder(arr*[0]);_
_
}_
_
}_
void loop() {
_
if (digitalRead(CHANGEMOOD) == ACTIVATED)_
_
{_
_
changeGenre();_
_
Serial.println(“change genre”);_
_
}_
_
if (digitalRead(PLAYPAUSE) == ACTIVATED)_
_
{_
if(isPlaying)
{isPlaying = false;
_
myDFPlayer.pause();*

* delay(500);*
* }*
* else*
* {*
* if(playFinished)
_
{*

* playFolder(currentFolder,currentTrack);
_
}*

* else*
* {*
* isPlaying = true;
_
myDFPlayer.start();*

* delay(500);*
* }*
* }*
* }*
* if (myDFPlayer.available()) {*
* printDetail(myDFPlayer.readType(), myDFPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states.*
* }*
}
void nextTrack(int currArray[2][2])
{
* if(currentTrack > totalTracks)
_
{*

* int tmp = currentFolder + 1;
_
copyReqArray(currArray);*

* for(int i=0;i<2;i++)*
* {*
if(tmp == currArray*[0])*

* {_
currentFolder = tmp;
_totalTracks = currArray[1];
currentTrack = 1;
lastTrack = false;*
* }*

* else*
* {_
currentTrack = totalTracks;
lastTrack = true;*
* }*

* }
}
}
void playFolder(int foldNo,int filNo)
{_

String mess = String("playing file ") + currentTrack + String(" from folder ") + foldNo;*
* Serial.println(mess);_
Serial.println(totalTracks);
currentFolder = foldNo;
currentTrack = filNo;
isPlaying = true;*
* delay(500);*

* myDFPlayer.playFolder(foldNo,filNo);*

}
void copyReqArray(int currArray[2][2])
{
* if(currentGenre == 1)*
* {_
copy(dev,currArray,2);*
* }_
else if(currentGenre == 2)*
* {_
copy(oldFilm,currArray,2);*
* }
else*

* {_
copy(modern,currArray,2);*
* }
}
void copy(int src[2][2], int dst[2][2], int len) {
memcpy(dst, src, sizeof(src[0])len);
}

void changeGenre(){_

* int tmpFolderNo = 0;*
* currentGenre < 3 ? currentGenre++ : currentGenre = 1;
switch(currentGenre)*
* {*

* case 1:*

* tmpFolderNo = dev[0][0];
totalTracks = dev[0][1];*
* break;*

* case 2:_
tmpFolderNo = oldFilm[0][0];
totalTracks = oldFilm[0][1];*
* break;*

* case 3:_
tmpFolderNo = modern[0][0];
totalTracks = modern[0][1];*
* }*

* playFolder(tmpFolderNo,1);
}
void printDetail(uint8_t type, int value){
switch (type) {
case TimeOut:
Serial.println(“Time Out!”);
break;
case WrongStack:
Serial.println(“Stack Wrong!”);
break;
case DFPlayerCardInserted:
Serial.println(“Card Inserted!”);
break;
case DFPlayerCardRemoved:
Serial.println(“Card Removed!”);
break;
case DFPlayerCardOnline:
Serial.println(“Card Online!”);
break;
case DFPlayerPlayFinished:_

currentTrack++;*
* int currArray[2][2];
Serial.println(“Analysing next track”);
nextTrack(currArray);
Serial.println(“folder:”);_

Serial.println(currentFolder);*
* Serial.println(“track:”);_
Serial.println(currentTrack);
if(lastTrack)*
* {*

* playFinished = true;
isPlaying = false;*
* Serial.println(" Play Finished!");*

* }*
* else*
* {*
* playFolder(currentFolder,currentTrack);*
* delay(500);*

* }*
* break;*
* case DFPlayerError:*
* Serial.print(“DFPlayerError:”);*
* switch (value) {*
* case Busy:*
* Serial.println(“Card not found”);*
* break;*
* case Sleeping:*
* Serial.println(“Sleeping”);*
* break;*
* case SerialWrongStack:*
* Serial.println(“Get Wrong Stack”);*
* break;*
* case CheckSumNotMatch:*
* Serial.println(“Check Sum Not Match”);*
* break;*
* case FileIndexOut:*
* Serial.println(“File Index Out of Bound”);*
* break;*
* case FileMismatch:*
* Serial.println(“Cannot Find File”);*
* break;*
* case Advertise:*
* Serial.println(“In Advertise”);*
* break;*
* default:*
* Serial.println(“unhandled”);*
* Serial.println(value);*
* break;*
* }*
* break;*
* default:*
* Serial.println(“unhandled 1”);*
* Serial.println(type);*
* break;*
* }*
}

Please post your code using code tags. Posting it like this causes issues.

Look at these posting guidelines

Read this before posting a programming question ...

From https://www.dfrobot.com/blog-277.htm

DFPlayer Mini read both MP3 and WAV files.
All audio files must be placed under /mp3 folder.
Audio file must be named 0001.mp3, 0002.mp3…0255.mp3 (4 letters not 2 or 3 letters)
Audio file names can have any characters after 4 digits, such as 0001_Bruno_Mars_Count_On Me.mp3
Tested few under 1 second WAV files and they did not work
If one of the audio file is removed from SD cared, DFPlayer still can find it and play it! Weird! In the end, I formatted the SD card to make DFPlayer to play the music I specified.
Delay(seconds); You have to specify number of seconds delay for the audio file to play completely or the next audio file might be started before current audio is over!
mp3_play (74); means play audio file 0074.mp3

halloweenrick:
Delay(seconds); You have to specify number of seconds delay for the audio file to play completely or the next audio file might be started before current audio is over!

If you use DFPlayerMini_Fast.h, you can poll the MP3 module with <DFPlayerMini_Fast>.isPlaying(). The function call will return true as long as the module is playing the file. As soon as it returns false, you know the current file is done and can start the next file.