Advertisement
pleasedontcode

LED Audio rev_02

Sep 20th, 2024
78
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 Uno
  14.     - Source Code created on: 2024-09-20 20:24:24
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* turn on LED light.  Start the DFPlayer and play */
  21.     /* track 1.  then play a random track from 2 - 5. */
  22.     /* Stop after 3 minutes.  loop again after 5 minutes. */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <SoftwareSerial.h>
  27. #include <DFRobotDFPlayerMini.h> //https://github.com/DFRobot/DFRobotDFPlayerMini
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32. void updateOutputs(void);
  33. void playRandomTrack(); // Function to play a random track
  34.  
  35. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  36. const uint8_t LED_LED_PIN_D2 = 2;
  37.  
  38. /***** DEFINITION OF Software Serial *****/
  39. const uint8_t DFPlayer_DFPlayerMini_Serial_PIN_SERIAL_TX_A0 = A0;
  40. const uint8_t DFPlayer_DFPlayerMini_Serial_PIN_SERIAL_RX_A1 = A1;
  41. SoftwareSerial DFPlayer_DFPlayerMini_Serial(DFPlayer_DFPlayerMini_Serial_PIN_SERIAL_RX_A1, DFPlayer_DFPlayerMini_Serial_PIN_SERIAL_TX_A0);
  42.  
  43. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  44. /***** used to store raw data *****/
  45. bool LED_LED_PIN_D2_rawData = 0;
  46.  
  47. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  48. /***** used to store data after characteristic curve transformation *****/
  49. float LED_LED_PIN_D2_phyData = 0.0;
  50.  
  51. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  52. DFRobotDFPlayerMini myDFPlayer; // Instantiate the DFPlayer Mini object
  53.  
  54. // Timing variables
  55. unsigned long startTime;
  56. bool isPlaying = false;
  57.  
  58. void setup(void)
  59. {
  60.     // put your setup code here, to run once:
  61.     pinMode(LED_LED_PIN_D2, OUTPUT); // Set LED pin as output
  62.  
  63.     // Initialize software serial for DFPlayer
  64.     DFPlayer_DFPlayerMini_Serial.begin(9600); // Start serial communication at 9600 baud
  65.  
  66.     // Initialize DFPlayer
  67.     if (!myDFPlayer.begin(DFPlayer_DFPlayerMini_Serial, true, true)) { // Use serial to communicate with mp3
  68.         Serial.println(F("Unable to begin:"));
  69.         Serial.println(F("1. Please recheck the connection!"));
  70.         Serial.println(F("2. Please insert the SD card!"));
  71.         while (true); // Stop execution if initialization fails
  72.     }
  73.     Serial.println(F("DFPlayer Mini online."));
  74.     myDFPlayer.volume(10); // Set volume value (0~30)
  75.  
  76.     // Start the LED and play track 1
  77.     digitalWrite(LED_LED_PIN_D2, HIGH); // Turn on LED
  78.     myDFPlayer.play(1); // Play the first mp3
  79.     startTime = millis(); // Record the start time
  80.     isPlaying = true; // Set the playing state
  81. }
  82.  
  83. void loop(void)
  84. {
  85.     // put your main code here, to run repeatedly:
  86.     updateOutputs(); // Refresh output data
  87.  
  88.     // Check if 3 minutes have passed
  89.     if (isPlaying && (millis() - startTime >= 180000)) {
  90.         myDFPlayer.stop(); // Stop playing after 3 minutes
  91.         digitalWrite(LED_LED_PIN_D2, LOW); // Turn off LED
  92.         isPlaying = false; // Update playing state
  93.         delay(300000); // Wait for 5 minutes before looping again
  94.         digitalWrite(LED_LED_PIN_D2, HIGH); // Turn on LED again
  95.         myDFPlayer.play(1); // Play track 1 again
  96.         startTime = millis(); // Reset the start time
  97.         isPlaying = true; // Set the playing state
  98.     }
  99.  
  100.     // If not playing, play a random track from 2 to 5
  101.     if (!isPlaying) {
  102.         playRandomTrack(); // Play a random track
  103.     }
  104. }
  105.  
  106. void updateOutputs()
  107. {
  108.     digitalWrite(LED_LED_PIN_D2, LED_LED_PIN_D2_rawData); // Update LED state based on raw data
  109. }
  110.  
  111. void playRandomTrack() {
  112.     int randomTrack = random(2, 6); // Generate a random track number between 2 and 5
  113.     myDFPlayer.play(randomTrack); // Play the random track
  114.     delay(1000); // Delay to allow track to play before returning to loop
  115. }
  116.  
  117. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement