Advertisement
pleasedontcode

Audio Control rev_01

Jul 29th, 2024
205
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: Audio Control
  13.     - Source Code NOT compiled for: Arduino Nano
  14.     - Source Code created on: 2024-07-29 05:07:06
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* play single mp3 and flash led at each push of a */
  21.     /* momentary button */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /****** DEFINITION OF LIBRARIES *****/
  25. #include <SoftwareSerial.h>
  26. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  27. #include <DFRobotDFPlayerMini.h>    //https://github.com/DFRobot/DFRobotDFPlayerMini
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32. void onPressed(); // Function prototype for the onPressed callback
  33. void printDetail(uint8_t type, int value); // Function prototype for printing DFPlayer details
  34.  
  35. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  36. const uint8_t button_PushButton_PIN_D2      = 2;
  37.  
  38. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  39. const uint8_t led_LED_PIN_D3        = 3;
  40.  
  41. /***** DEFINITION OF Software Serial *****/
  42. const uint8_t plait_DFPlayerMini_Serial_PIN_SERIAL_TX_A0        = A0;
  43. const uint8_t plait_DFPlayerMini_Serial_PIN_SERIAL_RX_A1        = A1;
  44. SoftwareSerial plait_DFPlayerMini_Serial(plait_DFPlayerMini_Serial_PIN_SERIAL_RX_A1, plait_DFPlayerMini_Serial_PIN_SERIAL_TX_A0);
  45.  
  46. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  47. // Initialize the EasyButton object with the button pin
  48. EasyButton button(button_PushButton_PIN_D2);
  49.  
  50. // Initialize the DFPlayer Mini object
  51. DFRobotDFPlayerMini myDFPlayer; // Create an instance of the DFPlayer Mini
  52.  
  53. /****** FUNCTION DEFINITIONS *****/
  54. void setup(void)
  55. {
  56.     // put your setup code here, to run once:
  57.     pinMode(led_LED_PIN_D3, OUTPUT); // Set LED pin as output
  58.  
  59.     plait_DFPlayerMini_Serial.begin(9600); // Initialize Software Serial for DFPlayer Mini
  60.  
  61.     button.begin(); // Initialize the EasyButton
  62.     button.onPressed(onPressed); // Attach the onPressed callback function
  63.  
  64.     // Initialize the DFPlayer Mini
  65.     if (!myDFPlayer.begin(plait_DFPlayerMini_Serial, true, true)) { // Use serial to communicate with DFPlayer
  66.         Serial.println(F("Unable to begin. Check connection and SD card!")); // Error message if initialization fails
  67.         while (true) delay(0); // Halt execution
  68.     }
  69.     myDFPlayer.volume(10);  // Set volume (0-30)
  70. }
  71.  
  72. void loop(void)
  73. {
  74.     // put your main code here, to run repeatedly:
  75.     button.read(); // Read the button state
  76.  
  77.     // Check if DFPlayer has available data
  78.     if (myDFPlayer.available()) {
  79.         printDetail(myDFPlayer.readType(), myDFPlayer.read()); // Print details from DFPlayer
  80.     }
  81. }
  82.  
  83. void onPressed()
  84. {
  85.     // Callback function for button press
  86.     digitalWrite(led_LED_PIN_D3, HIGH); // Turn on LED
  87.     myDFPlayer.play(1); // Play the first mp3
  88.     delay(100); // Keep LED on for a short duration
  89.     digitalWrite(led_LED_PIN_D3, LOW); // Turn off LED
  90. }
  91.  
  92. void printDetail(uint8_t type, int value) {
  93.     switch (type) {
  94.         case DFPlayerCardInserted: Serial.println(F("Card Inserted!")); break;
  95.         case DFPlayerCardRemoved: Serial.println(F("Card Removed!")); break;
  96.         case DFPlayerPlayFinished: Serial.print(F("Number:")); Serial.print(value); Serial.println(F(" Play Finished!")); break;
  97.         case DFPlayerError:
  98.             switch (value) {
  99.                 case Busy: Serial.println(F("Card not found")); break;
  100.                 case Sleeping: Serial.println(F("Sleeping")); break;
  101.                 case FileIndexOut: Serial.println(F("File Index Out of Bound")); break;
  102.                 default: break;
  103.             }
  104.             break;
  105.         default: break;
  106.     }
  107. }
  108.  
  109. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement