Are your buttons properly wired up with pull-down resistors? Your code expects that.
You are connected to the MP3 player with the hardware serial port but you are printing debug statements to the hardware serial port. You can't do both unless you are using a Mega and then use the second hardware port for the MP3. If you are using a Uno/Nano, then you need to use the SoftwareSerial version.
Once you start playing a song, you con't use delay() because that blocks. That is why the library has the waitMilliseconds() function since it calls mp3.loop() once in a while. If you did that, you would get the notification of when the track is done playing and then react to it however you want.
blh64:
Are your buttons properly wired up with pull-down resistors? Your code expects that.
Yes,
and yes, i'am using a NANO/
blh64:
You are connected to the MP3 player with the hardware serial port but you are printing debug statements to the hardware serial port. You can't do both unless you are using a Mega and then use the second hardware port for the MP3.
OK, i will just delate printing codes.
blh64:
Once you start playing a song, you con't use delay() because that blocks. That is why the library has the waitMilliseconds() function since it calls mp3.loop() once in a while. If you did that, you would get the notification of when the track is done playing and then react to it however you want.
Kayumba I would ditch the current library you are using and use PowerBroker's Mini Fast Library, available here:
It might be downloadable now from Libraries in the IDE menu. I would highly recommend it, it cuts out all the unnecessary lines in the program. For reference, here is a Halloween Prop program I built that fires a solenoid relay as the DF Mini MP3 player plays a random track off the player.
#include <SoftwareSerial.h>
#include <DFPlayerMini_Fast.h>
int buttonPin = 4;
int relay1 = 2;
SoftwareSerial mySerial(10, 11); // RX, TX
DFPlayerMini_Fast myMP3;
void setup()
{
pinMode(4, INPUT_PULLUP); // pushbutton pin
pinMode(relay1, OUTPUT); //relay 1 on solenoid
Serial.begin(115200);
mySerial.begin(9600);
myMP3.begin(mySerial);
Serial.println("Setting volume to max");
myMP3.volume(30);
delay(20);
}
void loop(){
// put your main code here, to run repeatedly:
if (digitalRead(buttonPin) == LOW){
delay(100);
myMP3.playNext(); //Play the first mp3
Serial.println("1st MP3 playing");
digitalWrite(relay1, LOW);
Serial.println("Solenoid Relay ON");
delay(12000); // Give the DFPlayer some time
}
else {
myMP3.sleep (); //mp3 player off
Serial.println("MP3 player off");
digitalWrite(relay1,HIGH);
//do nothing
}
}
As you can see, I still use the DELAY function, but its such a simple prop there was no need to do anything else. You may need to use the millis() function referenced above. Check out the State Case example to guide you in the IDE. Hope that helps.
Also note that DFPlayerMini_Fast.h allows the call of '.randomAll();'. I do think, however, that it will only play one song per call. That being said, you can constantly poll '.isPlaying();' and if it ever returns false, you can call '.randomAll()' again to play another random track.
Ok, I will try to do something with DFPlayerMini_Fast.h library.
I'am very novice with Arduino codes.
Car somebody give me with exemple code for DFPlayerMini_Fast.h
That makes two push buttons plays random files in different folders if button pushed.
Example button1 plays once file randomly in folder "sounds1", button2 plays once randomly in folder "sounds2".
Power_Broker:
Also note that DFPlayerMini_Fast.h allows the call of '.randomAll();'. I do think, however, that it will only play one song per call. That being said, you can constantly poll '.isPlaying();' and if it ever returns false, you can call '.randomAll()' again to play another random track. T
That will fix your problem for sure!
Here is what I done.
But it's still playing non stop.
#include <SoftwareSerial.h>
#include <DFPlayerMini_Fast.h>
SoftwareSerial mySerial(10, 11); // RX, TX
DFPlayerMini_Fast myMP3;
int sw1 = 3; // Push Button n°1 (pin3) // Random plaing
int sw2 = 4; // Push Button n°2 (pin4)
int sw3 = 5; // Push Button n°1 (pin5)
//int sw4 = 6; // Push Button n°1 (pin6)
void setup()
{
//4 push buttons with pulldown
pinMode(sw1,INPUT); // Random play button
pinMode(sw2,INPUT);
pinMode(sw3,INPUT);
//pinMode(sw4,INPUT);
Serial.begin(115200);
mySerial.begin(9600);
myMP3.begin(mySerial);
Serial.println("Setting volume to max");
myMP3.volume(5);
delay(20);
Serial.println("Playing track 1 for 5 sec");
myMP3.play(1);
delay(5000);
}
void loop()
{
if(digitalRead(sw1))
{
Serial.println("Playing track 1 for 5 sec");
myMP3.play(3);
delay(5000);
}
if(digitalRead(sw2))
{
Serial.println("Playing Random sounds");
myMP3.randomAll();
}
What will be a code for sw2 to stop playing at the end of track?
if(digitalRead(sw2))
{
Serial.println("Playing Random sounds");
myMP3.randomAll();
}
I thought that's what you wanted. I think we're misunderstanding each other. Keep in mind that without a video of your setup, I have no idea what your project is doing nor of what you want it to do, so you have to be VERY detailed in your descriptions of both.
1.) What EXACTLY do you want your project to do? Use as much detail as you can to make it clear. Do you want it to play all files in random order? How do you know what folder to play the files out of? etc.
2.) What EXACTLY is your project currently doing?
The answers to this question will help us answer your original question and fix your project.
Power_Broker:
I thought that's what you wanted. I think we're misunderstanding each other. Keep in mind that without a video of your setup, I have no idea what your project is doing nor of what you want it to do, so you have to be VERY detailed in your descriptions of both.
1.) What EXACTLY do you want your project to do? Use as much detail as you can to make it clear. Do you want it to play all files in random order? How do you know what folder to play the files out of? etc.
2.) What EXACTLY is your project currently doing?
The answers to this question will help us answer your original question and fix your project.
Sorry, it's most be my bad English.
I will try to be clear.
Project :
Make a box with 3 buttons that plays sounds.
Every button plays randomly and separately mp3 audio files.
For each button one folder.
Exemple: Button1 = folder1_mp3, Button2 = folder2_mp3, Button3 = folder3_mp3
Fonctions:
When Button1 are pressed = play only once, one mp3 file in random selection from "folder1_mp3".
When playback of audio file is finish = waiting for another push.
When Button2 are pressed = play only once, one mp3 file in random selection from "folder2_mp3".
When playback of audio file is finish = waiting for another push.
Ok, so what you'll have to do is use the random() function to come up with a random track to play every time a button is pressed. Then feed that random number to '.playFolder(uint8_t folderNum, uint8_t trackNum)' to play the random track from a specific folder.
This might sound really dumb, but did you name the file:
0001.mp3
because you just need to name it 0001.
If you do a deep dive into your files you'll see that the sd card adds ANOTHER .mp3 extension- which means you've actually got a file name of:
0001.mp3.mp3
And that's why your player keeps playing the one file over and over, as it can't find the file its looking for.
And yes, it took me an hour to figure that out the first time too.
If all your files are in "mp3", use 'myMP3.play(randomInt);'. I had you use 'myMP3.playFolder(1, randomInt);' because you said you wanted folders for mp3 files for each button.