From 79236ed5bd678f7742cd23408efe0f5ea9d3273b Mon Sep 17 00:00:00 2001 From: Sukarna Jana <47217084+Sukarnascience@users.noreply.github.com> Date: Wed, 18 Sep 2024 00:26:12 +0530 Subject: [PATCH] Create Play_particular_MP3_ESP32.ino New Features: The updated code simplifies usage for beginners by offering a more concise and user-friendly approach. It refines the example from the original library, focusing on a single function that allows users to play specific MP3 files for a desired duration. This streamlined method makes it easier to understand and implement, especially for those new to working with the DFPlayer Mini and ESP32. --- .../Play_particular_MP3_ESP32.ino | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 examples/Play_particular_MP3_ESP32/Play_particular_MP3_ESP32.ino 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 +}