Advertisement
pleasedontcode

MP3 Control rev_02

Jul 27th, 2024
191
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: MP3 Control
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-07-27 08:51:39
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* flash led and play mp3 at push of button */
  21. /****** SYSTEM REQUIREMENT 2 *****/
  22.     /* play a single mp3 and flash led at push of button */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <SoftwareSerial.h> //https://github.com/plerup/espsoftwareserial
  27. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  28. #include <DFRobotDFPlayerMini.h>    //https://github.com/DFRobot/DFRobotDFPlayerMini
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t moment1_PushButton_PIN_D4 = 4;
  36.  
  37. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  38. const uint8_t led1_LED_PIN_D16 = 16;
  39.  
  40. /***** DEFINITION OF Software Serial *****/
  41. const uint8_t mp3player_DFPlayerMini_Serial_PIN_SERIAL_TX_D13 = 13;
  42. const uint8_t mp3player_DFPlayerMini_Serial_PIN_SERIAL_RX_D14 = 14;
  43. EspSoftwareSerial::UART mp3player_DFPlayerMini_Serial;
  44.  
  45. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  46. // Create an instance of EasyButton for the push button
  47. EasyButton button(moment1_PushButton_PIN_D4); // Initialize EasyButton with the button pin
  48.  
  49. // Create an instance of DFRobotDFPlayerMini for the MP3 player
  50. DFRobotDFPlayerMini myDFPlayer; // Initialize DFPlayer object
  51.  
  52. void setup(void)
  53. {
  54.     // Initialize Serial for debugging purposes
  55.     Serial.begin(115200);
  56.     Serial.println();
  57.  
  58.     // Set button pin as input with pull-up resistor
  59.     pinMode(moment1_PushButton_PIN_D4, INPUT_PULLUP);
  60.     // Set LED pin as output
  61.     pinMode(led1_LED_PIN_D16, OUTPUT);
  62.  
  63.     // Initialize Software Serial for MP3 player
  64.     mp3player_DFPlayerMini_Serial.begin(9600, SWSERIAL_8N1, mp3player_DFPlayerMini_Serial_PIN_SERIAL_RX_D14, mp3player_DFPlayerMini_Serial_PIN_SERIAL_TX_D13, false);
  65.  
  66.     // Initialize the DFPlayer
  67.     if (!myDFPlayer.begin(mp3player_DFPlayerMini_Serial, true, true)) { // Use serial to communicate with MP3
  68.         Serial.println(F("Unable to begin:"));
  69.         Serial.println(F("1. Please recheck the connection!"));
  70.         Serial.println(F("2. Please insert the SD card!"));
  71.         while (true) {
  72.             delay(0); // Code to compatible with ESP8266 watch dog.
  73.         }
  74.     }
  75.     Serial.println(F("DFPlayer Mini online."));
  76.     myDFPlayer.volume(10);  // Set volume (0-30)
  77.  
  78.     // Initialize the EasyButton
  79.     button.begin(); // Initialize the button
  80.     button.onPressed([]() { // Set the callback for when the button is pressed
  81.         Serial.println("Button pressed");
  82.         digitalWrite(led1_LED_PIN_D16, HIGH); // Turn on the LED
  83.         myDFPlayer.play(1); // Play the first mp3
  84.         delay(1000); // Keep LED on for 1 second
  85.         digitalWrite(led1_LED_PIN_D16, LOW); // Turn off the LED
  86.     });
  87. }
  88.  
  89. void loop(void)
  90. {
  91.     // Continuously read the status of the button
  92.     button.read();
  93.  
  94.     // Check if DFPlayer has available data
  95.     if (myDFPlayer.available()) {
  96.         printDetail(myDFPlayer.readType(), myDFPlayer.read()); // Print the detail message from DFPlayer
  97.     }
  98. }
  99.  
  100. void printDetail(uint8_t type, int value) {
  101.     switch (type) {
  102.         case DFPlayerCardInserted: Serial.println(F("Card Inserted!")); break;
  103.         case DFPlayerCardRemoved: Serial.println(F("Card Removed!")); break;
  104.         case DFPlayerPlayFinished: Serial.print(F("Number:")); Serial.print(value); Serial.println(F(" Play Finished!")); break;
  105.         case DFPlayerError:
  106.             switch (value) {
  107.                 case Busy: Serial.println(F("Card not found")); break;
  108.                 case Sleeping: Serial.println(F("Sleeping")); break;
  109.                 case FileIndexOut: Serial.println(F("File Index Out of Bound")); break;
  110.                 default: break;
  111.             }
  112.             break;
  113.         default: break;
  114.     }
  115. }
  116.  
  117. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement