Advertisement
pleasedontcode

LED Audio rev_03

Aug 1st, 2024
355
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: LED Audio
  13.     - Source Code NOT compiled for: Arduino Nano
  14.     - Source Code created on: 2024-08-01 22:41:19
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* on button, flash the led 3x and play an mp3. user */
  21.     /* internal pull-up */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /****** DEFINITION OF LIBRARIES *****/
  25. #include <SoftwareSerial.h>
  26. #include <DFRobotDFPlayerMini.h>    //https://github.com/DFRobot/DFRobotDFPlayerMini
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31. void updateOutputs(void);
  32. void handleButtonPress(void); // Function to handle button press
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t tt_PushButton_PIN_D2      = 2;
  36.  
  37. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  38. const uint8_t hh_LED_PIN_D3     = 3;
  39.  
  40. /***** DEFINITION OF Software Serial *****/
  41. const uint8_t gg_DFPlayerMini_Serial_PIN_SERIAL_TX_A0       = A0;
  42. const uint8_t gg_DFPlayerMini_Serial_PIN_SERIAL_RX_A1       = A1;
  43. SoftwareSerial gg_DFPlayerMini_Serial(gg_DFPlayerMini_Serial_PIN_SERIAL_RX_A1, gg_DFPlayerMini_Serial_PIN_SERIAL_TX_A0);
  44.  
  45. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  46. /***** used to store raw data *****/
  47. bool    hh_LED_PIN_D3_rawData       = 0;
  48.  
  49. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  50. /***** used to store data after characteristic curve transformation *****/
  51. float   hh_LED_PIN_D3_phyData       = 0.0;
  52.  
  53. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  54. DFRobotDFPlayerMini myDFPlayer; // Instance of the DFPlayer Mini class
  55.  
  56. void setup(void)
  57. {
  58.     // put your setup code here, to run once:
  59.     pinMode(tt_PushButton_PIN_D2, INPUT_PULLUP); // Set button pin as input with internal pull-up
  60.     pinMode(hh_LED_PIN_D3, OUTPUT); // Set LED pin as output
  61.  
  62.     gg_DFPlayerMini_Serial.begin(9600); // Initialize software serial for DFPlayer
  63.     Serial.begin(115200); // Initialize serial for debugging
  64.  
  65.     // Initialize DFPlayer
  66.     if (!myDFPlayer.begin(gg_DFPlayerMini_Serial, true, true)) { // Use serial to communicate with mp3.
  67.         Serial.println(F("Unable to begin:"));
  68.         Serial.println(F("1. Please recheck the connection!"));
  69.         Serial.println(F("2. Please insert the SD card!"));
  70.         while (true); // Stop execution if initialization fails
  71.     }
  72.     Serial.println(F("DFPlayer Mini online."));
  73.     myDFPlayer.volume(10); // Set volume value (0~30)
  74. }
  75.  
  76. void loop(void)
  77. {
  78.     // put your main code here, to run repeatedly:
  79.     updateOutputs(); // Refresh output data
  80.     handleButtonPress(); // Check for button press
  81.  
  82.     if (myDFPlayer.available()) {
  83.         // Handle DFPlayer messages
  84.         printDetail(myDFPlayer.readType(), myDFPlayer.read());
  85.     }
  86. }
  87.  
  88. void updateOutputs()
  89. {
  90.     digitalWrite(hh_LED_PIN_D3, hh_LED_PIN_D3_rawData);
  91. }
  92.  
  93. void handleButtonPress() {
  94.     // Check if the button is pressed (LOW because of pull-up)
  95.     if (digitalRead(tt_PushButton_PIN_D2) == LOW) {
  96.         // Flash the LED 3 times
  97.         for (int i = 0; i < 3; i++) {
  98.             digitalWrite(hh_LED_PIN_D3, HIGH); // Turn LED on
  99.             delay(250); // Wait for 250 ms
  100.             digitalWrite(hh_LED_PIN_D3, LOW); // Turn LED off
  101.             delay(250); // Wait for 250 ms
  102.         }
  103.         // Play an MP3 file (e.g., the first file)
  104.         myDFPlayer.play(1); // Play the first MP3 file
  105.         delay(1000); // Delay to avoid multiple triggers
  106.     }
  107. }
  108.  
  109. void printDetail(uint8_t type, int value) {
  110.     // Function to print the details of DFPlayer state and errors
  111.     switch (type) {
  112.         case TimeOut:
  113.             Serial.println(F("Time Out!"));
  114.             break;
  115.         case WrongStack:
  116.             Serial.println(F("Stack Wrong!"));
  117.             break;
  118.         case DFPlayerCardInserted:
  119.             Serial.println(F("Card Inserted!"));
  120.             break;
  121.         case DFPlayerCardRemoved:
  122.             Serial.println(F("Card Removed!"));
  123.             break;
  124.         case DFPlayerCardOnline:
  125.             Serial.println(F("Card Online!"));
  126.             break;
  127.         case DFPlayerUSBInserted:
  128.             Serial.println(F("USB Inserted!"));
  129.             break;
  130.         case DFPlayerUSBRemoved:
  131.             Serial.println(F("USB Removed!"));
  132.             break;
  133.         case DFPlayerPlayFinished:
  134.             Serial.print(F("Number:"));
  135.             Serial.print(value);
  136.             Serial.println(F(" Play Finished!"));
  137.             break;
  138.         case DFPlayerError:
  139.             Serial.print(F("DFPlayerError:"));
  140.             switch (value) {
  141.                 case Busy:
  142.                     Serial.println(F("Card not found"));
  143.                     break;
  144.                 case Sleeping:
  145.                     Serial.println(F("Sleeping"));
  146.                     break;
  147.                 case SerialWrongStack:
  148.                     Serial.println(F("Get Wrong Stack"));
  149.                     break;
  150.                 case CheckSumNotMatch:
  151.                     Serial.println(F("Check Sum Not Match"));
  152.                     break;
  153.                 case FileIndexOut:
  154.                     Serial.println(F("File Index Out of Bound"));
  155.                     break;
  156.                 case FileMismatch:
  157.                     Serial.println(F("Cannot Find File"));
  158.                     break;
  159.                 case Advertise:
  160.                     Serial.println(F("In Advertise"));
  161.                     break;
  162.                 default:
  163.                     break;
  164.             }
  165.             break;
  166.         default:
  167.             break;
  168.     }
  169. }
  170.  
  171. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement