Advertisement
pleasedontcode

LED Audio rev_01

Sep 20th, 2024
83
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-09-20 18:41:22
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Turn on LED and DFPlayer when board starts.  Play */
  21.     /* track one while LED flickers.  Play a random track */
  22.     /* 2-6 immediately after track one while LED is lit. */
  23.     /* After 3 minutes the board stops.  After 5 minutes */
  24.     /* board starts sequence again. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <SoftwareSerial.h>
  29. #include <DFRobotDFPlayerMini.h>    //https://github.com/DFRobot/DFRobotDFPlayerMini
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void updateOutputs(void);
  35. void playRandomTrack(void);
  36.  
  37. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  38. const uint8_t LED_LED_PIN_D2        = 2;
  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. bool    LED_LED_PIN_D2_rawData      = 0;
  47.  
  48. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  49. float   LED_LED_PIN_D2_phyData      = 0.0;
  50.  
  51. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  52. // Create an instance of the DFPlayer Mini class
  53. DFRobotDFPlayerMini myDFPlayer; // Initialize the DFPlayerMini object
  54.  
  55. // Timing variables
  56. unsigned long startMillis;
  57. unsigned long currentMillis;
  58. const unsigned long threeMinutes = 180000; // 3 minutes in milliseconds
  59. const unsigned long fiveMinutes = 300000; // 5 minutes in milliseconds
  60. bool sequenceActive = false;
  61.  
  62. void setup(void)
  63. {
  64.     // put your setup code here, to run once:
  65.     pinMode(LED_LED_PIN_D2, OUTPUT);
  66.    
  67.     // Initialize the software serial for DFPlayer Mini
  68.     Player_DFPlayerMini_Serial.begin(9600);
  69.    
  70.     // Initialize the DFPlayer Mini
  71.     if (!myDFPlayer.begin(Player_DFPlayerMini_Serial, true, true)) { // Use serial to communicate with mp3.
  72.         Serial.println(F("Unable to begin:"));
  73.         Serial.println(F("1. Please recheck the connection!"));
  74.         Serial.println(F("2. Please insert the SD card!"));
  75.         while (true); // Loop forever if initialization fails
  76.     }
  77.    
  78.     // Set the volume (0 to 30)
  79.     myDFPlayer.volume(10);
  80.     Serial.println(F("DFPlayer Mini online."));
  81.    
  82.     // Start the sequence
  83.     startMillis = millis();
  84.     sequenceActive = true;
  85.     myDFPlayer.play(1); // Play track 1
  86. }
  87.  
  88. void loop(void)
  89. {
  90.     // put your main code here, to run repeatedly:
  91.     currentMillis = millis();
  92.    
  93.     if (sequenceActive) {
  94.         // Flicker LED while track 1 is playing
  95.         digitalWrite(LED_LED_PIN_D2, HIGH);
  96.         delay(250); // LED on for 250ms
  97.         digitalWrite(LED_LED_PIN_D2, LOW);
  98.         delay(250); // LED off for 250ms
  99.        
  100.         // Check if 3 minutes have passed
  101.         if (currentMillis - startMillis >= threeMinutes) {
  102.             sequenceActive = false; // Stop the sequence
  103.             digitalWrite(LED_LED_PIN_D2, LOW); // Turn off LED
  104.             myDFPlayer.stop(); // Stop playing
  105.         }
  106.     } else {
  107.         // Play a random track between 2 and 6 while LED is lit
  108.         if (currentMillis - startMillis < threeMinutes + 1000) { // Allow 1 second after stopping track 1
  109.             playRandomTrack(); // Play a random track
  110.             digitalWrite(LED_LED_PIN_D2, HIGH); // Keep LED on while playing random track
  111.             delay(1000); // Play random track for 1 second
  112.             digitalWrite(LED_LED_PIN_D2, LOW); // Turn off LED
  113.         }
  114.        
  115.         // Check if 5 minutes have passed to restart the sequence
  116.         if (currentMillis - startMillis >= fiveMinutes) {
  117.             startMillis = currentMillis; // Reset timer
  118.             sequenceActive = true; // Restart sequence
  119.             myDFPlayer.play(1); // Play track 1 again
  120.         }
  121.     }
  122.    
  123.     updateOutputs(); // Refresh output data
  124. }
  125.  
  126. void updateOutputs()
  127. {
  128.     // Update the LED state based on the raw data
  129.     digitalWrite(LED_LED_PIN_D2, LED_LED_PIN_D2_rawData);
  130. }
  131.  
  132. void playRandomTrack() {
  133.     // Play a random track between 2 and 6
  134.     int randomTrack = random(2, 7); // Generates a random number between 2 and 6
  135.     myDFPlayer.play(randomTrack);
  136. }
  137.  
  138. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement