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;*
* }*
}