Advertisement
Magella

DFPlayer

May 5th, 2024
744
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.98 KB | None | 0 0
  1. /***************************************************
  2. DFPlayer - A Mini MP3 Player For Arduino
  3.  <https://www.dfrobot.com/product-1121.html>
  4.  
  5.  ***************************************************
  6.  This example shows the basic function of library for DFPlayer.
  7.  
  8.  Created 2016-12-07
  9.  By [Angelo qiao](Angelo.qiao@dfrobot.com)
  10.  
  11.  GNU Lesser General Public License.
  12.  See <http://www.gnu.org/licenses/> for details.
  13.  All above must be included in any redistribution
  14.  ****************************************************/
  15.  
  16. /***********Notice and Trouble shooting***************
  17.  1.Connection and Diagram can be found here
  18.  <https://www.dfrobot.com/wiki/index.php/DFPlayer_Mini_SKU:DFR0299#Connection_Diagram>
  19.  2.This code is tested on Arduino Uno, Leonardo, Mega boards.
  20.  ****************************************************/
  21.  
  22. #include "Arduino.h"
  23. #include "DFRobotDFPlayerMini.h"
  24.  
  25. #if (defined(ARDUINO_AVR_UNO) || defined(ESP8266))   // Using a soft serial port
  26. #include <SoftwareSerial.h>
  27. SoftwareSerial softSerial(/*rx =*/4, /*tx =*/5);
  28. #define FPSerial softSerial
  29. #else
  30. #define FPSerial Serial1
  31. #endif
  32.  
  33. DFRobotDFPlayerMini myDFPlayer;
  34. void printDetail(uint8_t type, int value);
  35.  
  36. void setup()
  37. {
  38. #if (defined ESP32)
  39.   FPSerial.begin(9600, SERIAL_8N1, /*rx =*/D3, /*tx =*/D2);
  40. #else
  41.   FPSerial.begin(9600);
  42. #endif
  43.  
  44.   Serial.begin(115200);
  45.  
  46.   Serial.println();
  47.   Serial.println(F("DFRobot DFPlayer Mini Demo"));
  48.   Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
  49.  
  50.   if (!myDFPlayer.begin(FPSerial, /*isACK = */true, /*doReset = */true)) {  //Use serial to communicate with mp3.
  51.     Serial.println(F("Unable to begin:"));
  52.     Serial.println(F("1.Please recheck the connection!"));
  53.     Serial.println(F("2.Please insert the SD card!"));
  54.     while(true){
  55.       delay(0); // Code to compatible with ESP8266 watch dog.
  56.     }
  57.   }
  58.   Serial.println(F("DFPlayer Mini online."));
  59.  
  60.   myDFPlayer.volume(10);  //Set volume value. From 0 to 30
  61.   myDFPlayer.play(1);  //Play the first mp3
  62. }
  63.  
  64. void loop()
  65. {
  66.   static unsigned long timer = millis();
  67.  
  68.   if (millis() - timer > 3000) {
  69.     timer = millis();
  70.     myDFPlayer.next();  //Play next mp3 every 3 second.
  71.   }
  72.  
  73.   if (myDFPlayer.available()) {
  74.     printDetail(myDFPlayer.readType(), myDFPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states.
  75.   }
  76. }
  77.  
  78. void printDetail(uint8_t type, int value){
  79.   switch (type) {
  80.     case TimeOut:
  81.       Serial.println(F("Time Out!"));
  82.       break;
  83.     case WrongStack:
  84.       Serial.println(F("Stack Wrong!"));
  85.       break;
  86.     case DFPlayerCardInserted:
  87.       Serial.println(F("Card Inserted!"));
  88.       break;
  89.     case DFPlayerCardRemoved:
  90.       Serial.println(F("Card Removed!"));
  91.       break;
  92.     case DFPlayerCardOnline:
  93.       Serial.println(F("Card Online!"));
  94.       break;
  95.     case DFPlayerUSBInserted:
  96.       Serial.println("USB Inserted!");
  97.       break;
  98.     case DFPlayerUSBRemoved:
  99.       Serial.println("USB Removed!");
  100.       break;
  101.     case DFPlayerPlayFinished:
  102.       Serial.print(F("Number:"));
  103.       Serial.print(value);
  104.       Serial.println(F(" Play Finished!"));
  105.       break;
  106.     case DFPlayerError:
  107.       Serial.print(F("DFPlayerError:"));
  108.       switch (value) {
  109.         case Busy:
  110.           Serial.println(F("Card not found"));
  111.           break;
  112.         case Sleeping:
  113.           Serial.println(F("Sleeping"));
  114.           break;
  115.         case SerialWrongStack:
  116.           Serial.println(F("Get Wrong Stack"));
  117.           break;
  118.         case CheckSumNotMatch:
  119.           Serial.println(F("Check Sum Not Match"));
  120.           break;
  121.         case FileIndexOut:
  122.           Serial.println(F("File Index Out of Bound"));
  123.           break;
  124.         case FileMismatch:
  125.           Serial.println(F("Cannot Find File"));
  126.           break;
  127.         case Advertise:
  128.           Serial.println(F("In Advertise"));
  129.           break;
  130.         default:
  131.           break;
  132.       }
  133.       break;
  134.     default:
  135.       break;
  136.   }
  137.  
  138. }
  139.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement