For years I have successfully used mp3-tp-16p players. All of a sudden none of my sketches loop. It is very strange. I have tried using half a dozen different boards and several different sketches.
One song plays then the sketch stops just before the tune is to be played again in the following loop. The following just prints "Music plays" every 100 milliseconds.
I have been scratching my head for days and tempted to give up. Any tip on what I am doing wrong?
Thank you Terrypin but it still just runs once. This is what the serial monitor reads:
Music plays
music has played
music plays
and then nothing. It is the same as before.
I copied your code exactly.
Thanks, I appreciate you trying to help.
It all went wrong after I updated to arduino 2.2.1 but I have tried using earlier versions without success. I have been using the mp3 players for years. I am at a loss.
This behavior is expected. Many arduino sketches do not work properly/unstable/do not compile, as IDE and libraries develop thru time. Propably, if you go back to your old IDE and library, it works perfectly fine. When I code MCUs using Arduino IDE, i write the IDE and library version in the comments. So, it may work and compile even after 10 years, i just need to know what it was compibled with. One famous example is the PING sketch with the ethernet.h library. After the ethernet.h "update" this sketch was rendered usless and the last time i checked, nobody was able to make a stable/working PING sketch with the new library.
#include "Arduino.h"
#include "DFRobotDFPlayerMini.h"
// Conditional compilation for the serial port
#if (defined(ARDUINO_AVR_UNO) || defined(ESP8266)) // Using a soft serial port
#include <SoftwareSerial.h>
SoftwareSerial softSerial(/*rx =*/4, /*tx =*/5);
#define FPSerial softSerial
#else
#define FPSerial Serial1
#endif
// Declare the DFPlayer object
DFRobotDFPlayerMini myDFPlayer;
void setup() {
// Initialize the serial port
#if (defined ESP32)
FPSerial.begin(9600, SERIAL_8N1, /*rx =*/D3, /*tx =*/D2);
#else
FPSerial.begin(9600);
#endif
// Initialize the Serial for debugging
Serial.begin(115200);
// Print initialization messages
Serial.println();
Serial.println(F("DFRobot DFPlayer Mini Demo"));
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
// Initialize DFPlayer
if (!myDFPlayer.begin(FPSerial, /*isACK = */true, /*doReset = */true)) {
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);
}
Serial.println(F("DFPlayer Mini online."));
// Set DFPlayer configuration
myDFPlayer.setTimeOut(500); // Set serial communication timeout to 500ms
myDFPlayer.volume(20); // Set volume value (0~30).
myDFPlayer.EQ(DFPLAYER_EQ_NORMAL); // Set EQ to normal
myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD); // Set SD as the output device
myDFPlayer.loop(1); // Loop the first mp3
}
void loop() {
// Check for available commands from DFPlayer
if (myDFPlayer.available()) {
uint8_t type = myDFPlayer.readType();
int value = myDFPlayer.read();
// Handle playback completion
if (type == DFPlayerPlayFinished) {
Serial.print(F("Number:"));
Serial.print(value);
Serial.println(F(" Play Finished!"));
myDFPlayer.play(1); // Play the first mp3 again
}
}
}
Thank you all. I uninstalled 2.2.1 and it still did not work. I tried three players with no luck but the forth worked!
I think all three mp3 players must have been damaged.
Thank you all for helping me problem solve this. Although no-one in particular solved it you all assisted by getting me to think outside the square. I greatly appreciate your help.
Boy I have spent some time trying to fix this
Yes, these modules do have have frustrating inconsistencies. As you saw, I deliberately changed only one line in your original sketch. But before finally dumping the other three modules, try substituting my code below for the corresponding initialisation lines, using each of those suspects. Maybe we can salvage one or more of them.
`if (!player.begin(softwareSerial, true, false))
{
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);
}
Serial.println(F("DFPlayer Mini online."));
player.setTimeOut(1000);
//----Set volume----
player.volume(20); // BTW, 30 can cause voltage drop
// issue in some circuits; have not seen yours.
delay(500);
Serial.println("end setup"); // No crash so far
} `