DF Player Mini nightmares (can't play correct file) - Page 1
Forum section: electronics / projects

author: Youkai
author_rank: Regular Contributor
timestamp: October 10, 2023, 07:04:01 am
content: I'm trying to hook up a DF Player Mini to my Arduino Uno and get it to play some sounds on button press. I have 13 mp3 files on my SD card. For the life of me I can't figure out how the hell to get it to play the correct sounds. Some attempts it plays a "random" file, usually one of the same few. Another attempt I was able to get it to play consistently off by 1. So play(1) played the last file, and play(2) played the first, etc.I have my files named using the format "[0 padded 4 digit number][other name text].mp3".I read somewhere when trying to debug this that created or possibly edited dates were affecting things. So for this latest attempt (the one that is off by one) I took my original .wav files and saved them to .mp3 in the number order so their created dates would all be in sequence.My circuit is configured like the "Connection Diagram" inthis site.Additionally my circuit has a button and an LED that I toggle to confirm the arduino is seeing the button presses.Here is my program:Code:#include <Arduino.h>#include <DFPlayerMini_Fast.h>#include <YaTimer.h>#if !defined(UBRR1H)#include <SoftwareSerial.h>SoftwareSerial mySerial(10, 11); // RX, TX#endif#define LED_CONTROL_PIN 7#define BUTTON_READ_PIN 6boolean pressed = false;boolean ledOn = false;DFPlayerMini_Fast speaker;YaTimer speakerTimer = YaTimer(50);void setup(){Serial.begin(115200);#if !defined(UBRR1H)mySerial.begin(9600);speaker.begin(mySerial, true);#elseSerial1.begin(9600);speaker.begin(Serial1, true);#endifpinMode(BUTTON_READ_PIN, INPUT);pinMode(LED_CONTROL_PIN, OUTPUT);digitalWrite(LED_CONTROL_PIN, LOW);Serial.println("Setting volume to max");speaker.volume(30);speakerTimer.start();}void loop(){if (triggered()) {pressed = true;ledOn = !ledOn;triggerAction();}if (digitalRead(BUTTON_READ_PIN) == false) {pressed = false;}if (speakerTimer.elapsedCheck(3000)) {speaker.stop();}}boolean triggered() {if (digitalRead(BUTTON_READ_PIN) == true && pressed == false) {return true;}return false;}void triggerAction() {if (ledOn) {digitalWrite(LED_CONTROL_PIN, HIGH);} else {digitalWrite(LED_CONTROL_PIN, LOW);}Serial.println("Firing");speaker.play(2);speakerTimer.start();}Has anyone run into these issues before? Any idea what I can do to correct this? If I just need to use "off by one" numbers for my play's I can make that happen but I'm concerned that it may not work the same when I move the DFPlayer over to my Sparkfun Pro Micro for the final circuit.I don't know much about serial communication in general and even less about software serial. Is it possible something there is causing an issue? I couldn't figure out how to get it to play any sound at all using hardware serial and there are basically no examples of it that I could find. I'd like to stick with software serial because my final circuit already has wires in place for this.Thanks for your help!

author: Chriss
author_rank: Frequent Contributor
timestamp: October 11, 2023, 06:55:24 am
content: Hi!Ok, but what about button de-bouncing?Are you have let the hardware to de-bounce the button action ?Maybe that cos your problem.

author: Infraviolet
author_rank: Super Contributor
timestamp: October 11, 2023, 07:16:15 pm
content: My main recommendation, alongside debouncing (see herehttps://hackaday.com/2015/12/09/embed-with-elliot-debounce-your-noisy-buttons-part-i/https://hackaday.com/2015/12/10/embed-with-elliot-debounce-your-noisy-buttons-part-ii/), would be to litter your code with print statements showing what the inputs you read from are actually detecting, which functios get run when, and what precise output command (and when) is going to the player device. This way you can "peer inside the mind" of the arduino, and be sure what it is really up to, and where in its sequence of actions the actions it actually takes start to diverge from what you wanted it to do.

author: DavidAlfa
author_rank: Super Contributor
timestamp: October 11, 2023, 08:33:20 pm
content: Start with the simplest.Remove the button code, just play the files in a loop with some delay:Code:Serial.println("Playing 2");speaker.play(2);And so on. Once you get the files working in the order you want, continue with the rest.Had this issue with a different MP3 device, were it didn't use the name at all, but the file sequence as stored in the filesystem.So if you copied 002, then 001, the first file was actually 002.Better you format the SD card and copy all the files in a single operation, ensure they go in like 001, 002...Here I posted some macros for YAT terminal to manually drive it (Plus added some new details).https://www.eevblog.com/forum/microcontrollers/yx5200-(dfplayer-chip)-naked-with-flash-spi-nor/msg4552229/#msg4552229

author: Youkai
author_rank: Regular Contributor
timestamp: October 12, 2023, 03:16:25 am
content: Thanks for the replys guys. I'm confused though about how debouncing could help. If my understanding of what that means is correct that would mean it's registering multiple button presses in rapid succession.There is exactly 1 play command in my script and it always plays the same file. So if debouncing were the issue that would lead to it restarting the file but not playing an incorrect file. Am I wrong?