DFPlayer without delay

Power_Broker:
I’m having a real hard time understanding what you’re trying to say here. That being said, you might be able to fix the problem by using the class member function myMP3.isPlaying() to determine if a song is being played.

Im sorry if you hard to understand my bad english :slight_smile: , therofore, Im grateful to you that you still willing to help me. i will clarify again what is my problem. so, I have project to make line follower with mp3. I have already made a robot line follower before, but its my first time using mp3 series. so, i want my robot would play some musics in some conditions. like, the robot will play song number track 1 if the robot is moving. and play song number 2 if the robot is stop

Power_Broker:
For example, the following code will play all of track 1 and then immediately play track 2. However, note that track 2 will loop:

#include <SoftwareSerial.h>

#include <DFPlayerMini_Fast.h>

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

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

myMP3.begin(mySerial);
 
 myMP3.volume(30);
 delay(20);
 
 myMP3.play(1);
 
}

void loop()
{
 if(!myMP3.isPlaying())
   myMP3.play(2);
}

i have followed your code and change a few , but it just still play song number 1 and cannot switch to the song number 2, even the line sensor has detected. could you give me some sugestions more? here is my code :

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


DFPlayerMini_Fast myMP3;

#define ARDUINO_RX 5//should connect to TX of the Serial MP3 Player module
#define ARDUINO_TX 6//connect to RX of the module
SoftwareSerial mySerial(ARDUINO_RX, ARDUINO_TX);//init the serial protocol, tell to myserial wich pins are TX and RX


void setup()
{
  
 Serial.begin(115200);
  mySerial.begin(9600);
  myMP3.begin(mySerial);
  myMP3.volume(30);
  delay(20);
  myMP3.play(1);
  

}
void loop()
{

Serial.print(analogRead(A0) );
Serial.println();



if(analogRead(A0) > 300 )// robot stops
{
if(!myMP3.isPlaying()){
myMP3.play(2);

}

}

}

Power_Broker: Why don't you use tabs in your code???? (hint: ctrl+a, ctrl+t)

wow i dont know that, im really newbie here hahaha. thanks for the hint, mate

Get rid of the analogRead() calls

Power_Broker: Get rid of the analogRead() calls

if i get rid analogRead, whats the input for switch the music?

Ok, I was confused, but now I think I understand what you're trying to do. You should google "Finite State Machine" and implement one in your project. Then you can call myMP3.loop(trackNum) within each state. Then, the state could dictate what track you play.

Power_Broker: Ok, I was confused, but now I think I understand what you're trying to do. You should google "Finite State Machine" and implement one in your project. Then you can call myMP3.loop(trackNum) within each state. Then, the state could dictate what track you play.

ok i will learn about that , thanks for the advices

Its been a long day I learn about finite state machine, and its working! its perfectly donee. I can switch the song perfectly ;D. thx for the advices !

Power_Broker:
Ok, I was confused, but now I think I understand what you’re trying to do. You should google “Finite State Machine” and implement one in your project. Then you can call myMP3.loop(trackNum) within each state. Then, the state could dictate what track you play.

here my code

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


DFPlayerMini_Fast myMP3;

#define ARDUINO_TX 5//should connect to TX of the Serial MP3 Player module
#define ARDUINO_RX 6//connect to RX of the module
SoftwareSerial mySerial(ARDUINO_RX, ARDUINO_TX);



int state = 0;

int Sensor1 = 0;



void setup() {

  Serial.begin(9600);
  mySerial.begin(9600);
  myMP3.begin(mySerial);
  myMP3.volume(30);
  delay(10);



}


void loop()
{

  MP3();




}
void MP3 () {

  Sensor1 = analogRead(A0);


  switch (state)
  {
    case 0:// music 1
      if (Sensor1 < 300) {
        myMP3.loop(1);
        state = 1;
      }
      break;

    case 1://music 2

      if (Sensor1 > 300 )
      {
        myMP3.loop(2);
        state = 0;

      }


      break;


  }


}

Hi,

Can someone help me with the script below?
I would like to play a number of short mp3 files (with spoken words) one after the other, so that it forms a sentence.

For example first 001.mp3 then 004.mp3 and then 002.mp3.

I ended up with this script because I don’t want to use a delay.
With a delay I have to specify the time that the MP3 plays and that is not the same for every MP3.

The best thing is that it just plays the mp3 and when it is finished immediately plays the next one.

I have already tried everything, but I don’t want to succeed.

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

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

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

  myMP3.begin(mySerial);

  Serial.println("Setting volume");
  myMP3.volume(10);
  delay(20);
}

void loop()
{
  if (myMP3.isPlaying() == false) {
    myMP3.play(1);

  }
}

I would really appreciate it if someone can help me on the right track.

myMP3_is_playing.ino (392 Bytes)

Are you using the lib version 1.1.9?

The querying feature (i.e. "myMP3.isPlaying()") was broken up until the fix in 1.1.9.

@George_Verboven

Other post/duplicate DELETED Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum. It will help you get the best out of the forum in the future. Other general help and troubleshooting advice can be found here. It will help you get the best out of the forum in the future.

@ballscrewbob
I’m sorry, about the other cross post / duplicate.
I wasn’t aware of it.
Probably spent too long at the computer

Power_Broker,
Thanks for your response, yes I use lib version 1.1.9
Downloaded here

After a good night’s sleep and help from this great forum, I’ve made some progress.
The script below works (It plays the 3 tracks consecutively):

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

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

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

  myMP3.begin(mySerial);

  Serial.println("Setting volume");
  myMP3.volume(20);
  delay(20);

  //start first audio
  if (myMP3.isPlaying() == false) {
    myMP3.play(4);
    while (myMP3.isPlaying() == true) {
      delay(1);
    }
  }

  //start other audio
  if (myMP3.isPlaying() == false) {
    myMP3.play(1);
    while (myMP3.isPlaying() == true) {
      delay(1);
    }
  }

  //start other audio
  if (myMP3.isPlaying() == false) {
    myMP3.play(3);
    while (myMP3.isPlaying() == true) {
      delay(1);
    }
  }


}
void loop()
{
}

But I want to be able to define the tracks myself (so that they are not based on the time stamp during copying to the SD card.

Moreover, I want to be able to choose from which folder the MP3s should be selected.

So instead of myMP3.play (trackNum), I’m using myMP3.playFolder (folderNum, trackNum):

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

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

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

  myMP3.begin(mySerial);

  Serial.println("Setting volume");
  myMP3.volume(20);
  delay(20);

  //start first audio
  if (myMP3.isPlaying() == false) {
    myMP3.playFolder(3,1);
    while (myMP3.isPlaying() == true) {
      delay(1);
    }
  }

  //start other audio
  if (myMP3.isPlaying() == false) {
    myMP3.playFolder(2,4);
    while (myMP3.isPlaying() == true) {
      delay(1);
    }
  }

  //start other audio
  if (myMP3.isPlaying() == false) {
    myMP3.playFolder(4,3);
    while (myMP3.isPlaying() == true) {
      delay(1);
    }
  }


}
void loop()
{
}

Now the relevant track is neatly played from the chosen folder. (track 1 located in folder 3)
But it does not move on to the next. :sob:
(Which was the case in the first script)

I feel like I am close to the solution, But I can’t do it without your advice, thank you very much for that.

20200903_Play_next_when_finished_first.ino (996 Bytes)

Folders in root SD-card.JPG

tracks in folder.JPG