Advertisement
pleasedontcode

Audio LED rev_04

Sep 22nd, 2024
111
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 LED
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-09-22 21:10:42
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* play track for 3 minutes.  Shut down for 2 minutes */
  21.     /* then loop */
  22. /****** SYSTEM REQUIREMENT 2 *****/
  23.     /* Play a random audio track (from 2 to 5) using the */
  24.     /* DFRobot DFPlayer Mini module for 3 minutes, */
  25.     /* followed by a 2-minute shutdown, then repeat the */
  26.     /* cycle. Control LED indicators during playback and */
  27.     /* shutdown phases. */
  28. /****** END SYSTEM REQUIREMENTS *****/
  29.  
  30. /****** DEFINITION OF LIBRARIES *****/
  31. #include <SoftwareSerial.h>
  32. #include <DFRobotDFPlayerMini.h>    //https://github.com/DFRobot/DFRobotDFPlayerMini
  33.  
  34. /****** FUNCTION PROTOTYPES *****/
  35. void setup(void);
  36. void loop(void);
  37. void updateOutputs(void);
  38.  
  39. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  40. const uint8_t LED_LED_PIN_D2        = 2; // LED for playback indication
  41. const uint8_t LED_2_LED_PIN_D3      = 3; // LED for shutdown indication
  42. const uint8_t LED_3_LED_PIN_D4      = 4; // Unused LED
  43.  
  44. /***** DEFINITION OF Software Serial *****/
  45. const uint8_t DFPlayer_DFPlayerMini_Serial_PIN_SERIAL_TX_A0     = A0;
  46. const uint8_t DFPlayer_DFPlayerMini_Serial_PIN_SERIAL_RX_A1     = A1;
  47. SoftwareSerial DFPlayer_DFPlayerMini_Serial(DFPlayer_DFPlayerMini_Serial_PIN_SERIAL_RX_A1, DFPlayer_DFPlayerMini_Serial_PIN_SERIAL_TX_A0);
  48.  
  49. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  50. bool    LED_LED_PIN_D2_rawData      = 0; // Playback LED state
  51. bool    LED_2_LED_PIN_D3_rawData        = 0; // Shutdown LED state
  52. bool    LED_3_LED_PIN_D4_rawData        = 0; // Unused LED state
  53.  
  54. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  55. // Initialize DFPlayer object
  56. DFRobotDFPlayerMini myDFPlayer; // Create an instance of the DFPlayer class
  57.  
  58. void setup(void)
  59. {
  60.     // put your setup code here, to run once:
  61.  
  62.     pinMode(LED_LED_PIN_D2,  OUTPUT); // Set playback LED pin as output
  63.     pinMode(LED_2_LED_PIN_D3,    OUTPUT); // Set shutdown LED pin as output
  64.     pinMode(LED_3_LED_PIN_D4,    OUTPUT); // Set unused LED pin as output
  65.  
  66.     // Start the software serial for DFPlayer
  67.     DFPlayer_DFPlayerMini_Serial.begin(9600);
  68.  
  69.     // Initialize DFPlayer
  70.     if (!myDFPlayer.begin(DFPlayer_DFPlayerMini_Serial, true, true)) { // Use serial to communicate with mp3.
  71.         Serial.println(F("Unable to begin:"));
  72.         Serial.println(F("1. Please recheck the connection!"));
  73.         Serial.println(F("2. Please insert the SD card!"));
  74.         while (true); // Loop forever if initialization fails
  75.     }
  76.     Serial.println(F("DFPlayer Mini online."));
  77. }
  78.  
  79. void loop(void)
  80. {
  81.     // put your main code here, to run repeatedly:
  82.  
  83.     // Play a random audio track from 2 to 5 for 3 minutes
  84.     unsigned long playbackStartTime = millis(); // Record the start time of playback
  85.     while (millis() - playbackStartTime < 180000) { // Loop for 3 minutes (180000 ms)
  86.         int randomTrack = random(2, 6); // Generate a random track number between 2 and 5
  87.         myDFPlayer.play(randomTrack); // Play the selected track
  88.         LED_LED_PIN_D2_rawData = HIGH; // Turn on playback LED
  89.         updateOutputs(); // Update LED states
  90.         delay(60000); // Wait for 1 minute before playing the next track
  91.     }
  92.  
  93.     // Shutdown phase for 2 minutes
  94.     LED_LED_PIN_D2_rawData = LOW; // Turn off playback LED
  95.     LED_2_LED_PIN_D3_rawData = HIGH; // Turn on shutdown LED
  96.     updateOutputs(); // Update LED states
  97.     delay(120000); // Wait for 2 minutes
  98.  
  99.     // Turn off shutdown LED after the shutdown phase
  100.     LED_2_LED_PIN_D3_rawData = LOW; // Turn off shutdown LED
  101.     updateOutputs(); // Update LED states
  102. }
  103.  
  104. void updateOutputs(void) {
  105.     // Update the state of the LEDs based on the raw data
  106.     digitalWrite(LED_LED_PIN_D2, LED_LED_PIN_D2_rawData); // Update playback LED
  107.     digitalWrite(LED_2_LED_PIN_D3, LED_2_LED_PIN_D3_rawData); // Update shutdown LED
  108.     digitalWrite(LED_3_LED_PIN_D4, LED_3_LED_PIN_D4_rawData); // Update unused LED
  109. }
  110.  
  111. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement