Advertisement
pleasedontcode

LED MP3 Controller rev_01

Aug 2nd, 2024
361
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 MP3 Controller
  13.     - Source Code NOT compiled for: Arduino Nano
  14.     - Source Code created on: 2024-08-02 11:51:47
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* On button, fire led rapidly 3 times and trigger an */
  21.     /* mp3 . Don’t use easy button */
  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);
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t Button_PushButton_PIN_D2 = 2;
  36.  
  37. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  38. const uint8_t Led_LED_PIN_D3 = 3;
  39.  
  40. /***** DEFINITION OF Software Serial *****/
  41. const uint8_t Player_DFPlayerMini_Serial_PIN_SERIAL_TX_A0 = A0;
  42. const uint8_t Player_DFPlayerMini_Serial_PIN_SERIAL_RX_A1 = A1;
  43. SoftwareSerial Player_DFPlayerMini_Serial(Player_DFPlayerMini_Serial_PIN_SERIAL_RX_A1, Player_DFPlayerMini_Serial_PIN_SERIAL_TX_A0);
  44.  
  45. /****** DEFINITION OF OUTPUT RAW VARIABLES *****/
  46. /***** used to store raw data *****/
  47. bool Led_LED_PIN_D3_rawData = 0;
  48.  
  49. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  50. DFRobotDFPlayerMini myDFPlayer;  // Instance of the DFPlayer Mini class
  51.  
  52. void setup(void)
  53. {
  54.     // put your setup code here, to run once:
  55.     pinMode(Button_PushButton_PIN_D2, INPUT_PULLUP); // Set button pin as input with pull-up resistor
  56.     pinMode(Led_LED_PIN_D3, OUTPUT); // Set LED pin as output
  57.  
  58.     // Initialize the software serial for the DFPlayer Mini
  59.     Player_DFPlayerMini_Serial.begin(9600);
  60.  
  61.     // Initialize the DFPlayer Mini
  62.     if (!myDFPlayer.begin(Player_DFPlayerMini_Serial, true, true)) {  // Use serial to communicate with mp3
  63.         Serial.println(F("Unable to begin:"));
  64.         Serial.println(F("1. Please recheck the connection!"));
  65.         Serial.println(F("2. Please insert the SD card!"));
  66.         while (true);  // Stay here if initialization fails
  67.     }
  68.     Serial.println(F("DFPlayer Mini online."));
  69.  
  70.     myDFPlayer.setTimeOut(500); // Set serial communication timeout to 500ms
  71.     myDFPlayer.volume(10);  // Set volume value (0~30)
  72. }
  73.  
  74. void loop(void)
  75. {
  76.     // put your main code here, to run repeatedly:
  77.     updateOutputs(); // Refresh output data
  78.     handleButtonPress(); // Check for button press
  79. }
  80.  
  81. void updateOutputs()
  82. {
  83.     digitalWrite(Led_LED_PIN_D3, Led_LED_PIN_D3_rawData); // Update LED state
  84. }
  85.  
  86. void handleButtonPress()
  87. {
  88.     // Check if the button is pressed (active low)
  89.     if (digitalRead(Button_PushButton_PIN_D2) == LOW) {
  90.         // Fire LED rapidly 3 times
  91.         for (int i = 0; i < 3; i++) {
  92.             Led_LED_PIN_D3_rawData = HIGH; // Turn on LED
  93.             updateOutputs(); // Update LED state
  94.             delay(100); // Wait for 100 milliseconds
  95.             Led_LED_PIN_D3_rawData = LOW; // Turn off LED
  96.             updateOutputs(); // Update LED state
  97.             delay(100); // Wait for 100 milliseconds
  98.         }
  99.         // Trigger an MP3 file (e.g., play the first mp3)
  100.         myDFPlayer.play(1); // Play the first mp3
  101.         delay(1000); // Debounce delay to prevent multiple triggers
  102.     }
  103. }
  104.  
  105. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement