diff --git a/examples/Play_particular_MP3_ESP32/Play_particular_MP3_ESP32.ino b/examples/Play_particular_MP3_ESP32/Play_particular_MP3_ESP32.ino new file mode 100644 index 0000000..bada773 --- /dev/null +++ b/examples/Play_particular_MP3_ESP32/Play_particular_MP3_ESP32.ino @@ -0,0 +1,55 @@ +#include "Arduino.h" +#include "DFRobotDFPlayerMini.h" + +// Define the serial port for communication with the DFPlayer Mini +#define FPSerial Serial2 + +// Create an instance of the DFPlayer Mini +DFRobotDFPlayerMini myDFPlayer; + +// Function to print detailed messages from the DFPlayer Mini +void printDetail(uint8_t type, int value) { + // ... (same implementation as before) +} + +// Function to play a specific sound file with a given duration +void playSound(int soundNumber, int duration) { + myDFPlayer.play(soundNumber); + delay(duration * 1000); // Convert duration from seconds to milliseconds +} + +void setup() { + FPSerial.begin(9600); + Serial.begin(115200); + + Serial.println(F("DFRobot DFPlayer Mini Demo")); + Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)")); + + if (!myDFPlayer.begin(FPSerial)) { + 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(30); // Set volume value. From 0 to 30 +} + +void loop() { + // Example usage: Play sound file 1 for 5 seconds + playSound(1, 5); + /* + Important: MP3 File Organization + Create a Folder: On your TF card (microSD card), create a folder named mp3 in the root directory. + Organize MP3 Files: Place your MP3 files inside this mp3 folder. Name the files in the format 0001.mp3, 0002.mp3, 0003.mp3, and so on. + /mp3 + /0001.mp3 + /0002.mp3 + /0003.mp3 + ... + */ + // Add more code here to play other sound files or implement other functionality +}