Advertisement
pleasedontcode

DFPlayer Control rev_05

Sep 22nd, 2024
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: DFPlayer Control
  13.     - Source Code NOT compiled for: Arduino Nano
  14.     - Source Code created on: 2024-09-22 21:15:57
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The Haunted Radio project utilizes the DFRobot */
  21.     /* DFPlayer Mini for audio playback, controlled via */
  22.     /* SoftwareSerial on pins A0 and A1, enabling */
  23.     /* seamless integration of sound effects in a haunted */
  24.     /* theme.  Play track 1. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <SoftwareSerial.h>
  29. #include <DFRobotDFPlayerMini.h>  // https://github.com/DFRobot/DFRobotDFPlayerMini
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34.  
  35. /***** DEFINITION OF Software Serial *****/
  36. const uint8_t DFP_DFPlayerMini_Serial_PIN_SERIAL_TX_A0 = A0; // TX pin for SoftwareSerial
  37. const uint8_t DFP_DFPlayerMini_Serial_PIN_SERIAL_RX_A1 = A1; // RX pin for SoftwareSerial
  38. SoftwareSerial DFP_DFPlayerMini_Serial(DFP_DFPlayerMini_Serial_PIN_SERIAL_RX_A1, DFP_DFPlayerMini_Serial_PIN_SERIAL_TX_A0);
  39.  
  40. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  41. DFRobotDFPlayerMini myDFPlayer;  // Instance of the DFPlayer Mini class
  42.  
  43. void setup(void)
  44. {
  45.     // Initialize the software serial port for DFPlayer Mini
  46.     DFP_DFPlayerMini_Serial.begin(9600);
  47.    
  48.     // Initialize the serial monitor for debugging
  49.     Serial.begin(115200);
  50.    
  51.     // Wait for a moment to allow the DFPlayer to initialize
  52.     Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
  53.     if (!myDFPlayer.begin(DFP_DFPlayerMini_Serial, true, true)) {  // Use serial to communicate with the DFPlayer
  54.         Serial.println(F("Unable to begin:"));
  55.         Serial.println(F("1. Please recheck the connection!"));
  56.         Serial.println(F("2. Please insert the SD card!"));
  57.         while (true) {
  58.             delay(0); // Infinite loop to halt execution
  59.         }
  60.     }
  61.    
  62.     Serial.println(F("DFPlayer Mini online."));
  63.     myDFPlayer.volume(10);  // Set volume (0-30)
  64.     myDFPlayer.play(1);     // Play the first mp3
  65. }
  66.  
  67. void loop(void)
  68. {
  69.     static unsigned long timer = millis();
  70.    
  71.     // Play next mp3 every 3 seconds
  72.     if (millis() - timer > 3000) {
  73.         timer = millis();
  74.         myDFPlayer.next();  // Play next mp3
  75.     }
  76.    
  77.     // Check if the DFPlayer has available data to read
  78.     if (myDFPlayer.available()) {
  79.         printDetail(myDFPlayer.readType(), myDFPlayer.read()); // Print details of the DFPlayer status
  80.     }
  81. }
  82.  
  83. // Function to print details about the DFPlayer status
  84. void printDetail(uint8_t type, int value) {
  85.     switch (type) {
  86.         case DFPlayerCardInserted:
  87.             Serial.println(F("Card Inserted!"));
  88.             break;
  89.         case DFPlayerCardRemoved:
  90.             Serial.println(F("Card Removed!"));
  91.             break;
  92.         case DFPlayerPlayFinished:
  93.             Serial.print(F("Number:"));
  94.             Serial.print(value);
  95.             Serial.println(F(" Play Finished!"));
  96.             break;
  97.         case DFPlayerError:
  98.             switch (value) {
  99.                 case Busy:
  100.                     Serial.println(F("Card not found"));
  101.                     break;
  102.                 case Sleeping:
  103.                     Serial.println(F("Sleeping"));
  104.                     break;
  105.                 case FileIndexOut:
  106.                     Serial.println(F("File Index Out of Bound"));
  107.                     break;
  108.                 default:
  109.                     break;
  110.             }
  111.             break;
  112.         default:
  113.             break;
  114.     }
  115. }
  116.  
  117. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement