I have a "robot" that has been working for years now. I use the DFPlayer Mini to play an audio track (out of a few stored in an SD card) at a given time.
The code had been working just fine, and I used to update the code/add new tracks often, without any problems.
Problem:
Now I added two more tracks, and it suddenly started playing the wrong track number. In particular, this happened at two stages. First I added tracks 19 and 20, and it would play track 18 instead. When I tried to fix it (here I can't remember what I did exactly, but nothing dramatic), it started playing only track 1.
Also, for some reason I started to get the error that the SD card isn't recognised. I need to put it back into my computer and re-extract it. Only then it works. This never used to happen, so I wonder if this is a sign that I should just replace the mini SD card.
I am at a loss for what it was that I did to break it, and how to fix it. Any hints welcome.
I tried:
Removing all tracks in the micro SD card, adding them anew, in order, one by one.
Extracting the SD card from my computer every time I added a new track.
Removing track 1 -> it then just started playing the "new" first track (labelled as 0002, now first in the SD card)
Code
(Simplified)
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
#include "Wire.h"
#include "RTClib.h"
RTC_DS1307 RTC;
SoftwareSerial mySoftwareSerial(10, 11); // RX, TX //Actually these are wrongly labelled I think. It should be TX, RX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
int weekday(int year, int month, int day)
/* Calculate day of week in proleptic Gregorian calendar. Sunday == 0. */
{
int adjustment, mm, yy;
if (year < 2000) year += 2000;
adjustment = (14 - month) / 12;
mm = month + 12 * adjustment - 2;
yy = year - adjustment;
return (day + (13 * mm - 1) / 5 + yy + yy / 4 - yy / 100 + yy / 400) % 7;
}
void setup() {
mySoftwareSerial.begin(9600);
Serial.begin(9600);
Serial.println();
Serial.println(F("DFRobot DFPlayer Mini Demo"));
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
Wire.begin();
RTC.begin();
RTC.adjust(DateTime(__DATE__, __TIME__));
if (!RTC.isrunning()) {
Serial.println("RTC is NOT running!");
RTC.adjust(DateTime(__DATE__, __TIME__));
}
//sometimes this error can be fixed by putting the sd card again into the computer and extracting it properly (again)
if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
while (true) {
delay(0); // Code to compatible with ESP8266 watch dog.
}
}
Serial.println(F("DFPlayer Mini online."));
myDFPlayer.volume(19); //Set volume value. From 0 to 30
}
void loop() {
DateTime now = RTC.now();
if (RTC.isrunning()) {
int weekDayNumber = weekday(now.year(), now.month(), now.day());
if (weekDayNumber < 6 && weekDayNumber > 0) {
if (now.hour() == 7 && now.minute() == 15) {
myDFPlayer.play(1);
} else if (now.hour() == 7 && now.minute() == 30) {
myDFPlayer.play(2);
}
}
delay(10000);
}
}
It don't fully understand it, but it seems that regardless the title of track, the 'operating system' is looking for/at the date -- and the oldest is "#1".
Changing the files, file contents, complicates things.
How? Are you clicking "eject disk" or something, before you physically remove the SD from the PC? I'm not sure how Windows works in this respect, but on Ubuntu, I think if you remove the SD card without clicking "eject" first, it may not have written any or all the changes to the SD card. This could corrupt the file system on the card.
Have you tried re-formatting the SD card? Maybe a "full reformat" rather than a "quick format" would be worth trying.
Also, why did you post that circuit diagram? Presumably you haven't changed the wiring since before when it was previously working. But anyway, there's a glaring error that would prevent the player, or the RTC from working at all.
Yeah, I didn't change the circuit. So that would not explain it. Perhaps the glaring error is in my drawing and not in the circuit itself, but could you point me to the error? I (obviously) don't see it
It's ok to edit previous posts to correct spelling errors and such, but not to change the post in a way that makes the following responses stop making sense.
The "glaring" error is that there is no power (meaning 5V, 3.3V or whatever) to the player or the RTC.
(To be honest, it's not clear how the Uno is powered either, so the normal assumption is that it's powered by USB cable from the PC.)
Oh I see. Obviously I do power it, as it’s been working until now. I see there’s a cable missing. (Powered With 5V normally, fed to the Uno, works also with USB)
Doesn’t solve my problem, but thank you for your insight
As i remember the DF player doesn't care about file names, dates, or whatever (unless you try to play by name). It looks at the order the files appear in the FAT. So the first file you add to the sd card is file 1. Add a second later, that should be file 2.
Add more than one file in one go and whatever your PC decides to write first is going to become the next - so the order of files becomes unpredictable. I've had to write them one by one to keep the order predictable. That was rather annoying.
And for the missing power... considering you say your DF Player is actually playing your tracks, I think it's rather safe to assume this is simply yet another case of Fritzing diagrams not representing reality.
Removing hidden files (seems to) have worked. Thanks.
I checked before running the dot_clean command that
Yes, I had checked this, too. Added one by one, clicking "eject" after each time. That didn't do the trick. It was also always playing the first one, not the files in random order, so I wouldn't have expected that. But thanks, it's a good reminder, I had forgotten that was the case before I started troubleshooting.
@PaulRB's solution seems to have worked though. I say "seems to" because I checked before running the dot_clean command that there were no hidden mp3 files. But it did fix it, so I'll take it. Thank you.
PS: For the record, there's nothing embarrassing in making an error, even not seeing it Embarrassing is something else.
We will have to agree to disagree about that opinion In my opinion, embarrassment, or fear of it, is valuable because it helps us to avoid errors and avoid taking the time of others when a few minutes of research might reveal an answer. That link I posted was the first hit of a Google search for "df player mini" (other than sponsored links).
Please do some research before posting. Most beginner problems have already been dealt with on the Forum so please use the forum search tool and the Arduino Help Center to try and find an answer to your question. You can also use your favourite search engine to find answers too.
In order to "play by name", don't use myDFPlayer.play(). Instead, use myDFPlayer.playMp3Folder().
You must name your files beginning with a number, and put them in a folder named mp3, like this: