Advertisement
pleasedontcode

Audio Relay rev_06

Sep 22nd, 2024
75
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 Relay
  13.     - Source Code NOT compiled for: Arduino Nano
  14.     - Source Code created on: 2024-09-22 21:35:38
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* DFRobot DFPlayer Mini via SoftwareSerial on pins */
  21.     /* A0 and A1  change a relay on D2. Play track 1 for */
  22.     /* 14 seconds then play a random track between 2 and */
  23.     /* 7 for 1 minute.  Shut down after 1 minute.  wait 1 */
  24.     /* minute and restart */
  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.  
  35. /***** DEFINITION OF Software Serial *****/
  36. const uint8_t DFP_DFPlayerMini_Serial_PIN_SERIAL_TX_A0      = A0;
  37. const uint8_t DFP_DFPlayerMini_Serial_PIN_SERIAL_RX_A1      = A1;
  38. SoftwareSerial DFP_DFPlayerMini_Serial(DFP_DFPlayerMini_Serial_PIN_SERIAL_RX_A1, DFP_DFPlayerMini_Serial_PIN_SERIAL_TX_A0);
  39.  
  40. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  41. // Instantiate the DFRobotDFPlayerMini object
  42. DFRobotDFPlayerMini myDFPlayer; // Create an instance of the DFPlayer class
  43.  
  44. // Define relay pin
  45. const uint8_t RELAY_PIN = 2; // Pin to control the relay
  46.  
  47. void setup(void)
  48. {
  49.     // Initialize the software serial communication
  50.     DFP_DFPlayerMini_Serial.begin(9600);
  51.     Serial.begin(115200); // Initialize serial for debugging
  52.  
  53.     // Initialize the relay pin as output
  54.     pinMode(RELAY_PIN, OUTPUT);
  55.     digitalWrite(RELAY_PIN, LOW); // Ensure relay is off initially
  56.  
  57.     // Initialize the DFPlayer
  58.     if (!myDFPlayer.begin(DFP_DFPlayerMini_Serial, true, true)) { // Use serial to communicate with mp3
  59.         Serial.println(F("Unable to begin:"));
  60.         Serial.println(F("1. Please recheck the connection!"));
  61.         Serial.println(F("2. Please insert the SD card!"));
  62.         while (true); // Stop execution if initialization fails
  63.     }
  64.     Serial.println(F("DFPlayer Mini online."));
  65.    
  66.     // Set volume and other settings as needed
  67.     myDFPlayer.volume(10); // Set volume value (0~30)
  68.     myDFPlayer.setTimeOut(500); // Set serial communication timeout
  69. }
  70.  
  71. void loop(void)
  72. {
  73.     // Play track 1 for 14 seconds
  74.     myDFPlayer.play(1);
  75.     digitalWrite(RELAY_PIN, HIGH); // Activate relay
  76.     delay(14000); // Wait for 14 seconds
  77.  
  78.     // Play a random track between 2 and 7 for 1 minute
  79.     unsigned long startTime = millis();
  80.     while (millis() - startTime < 60000) { // Loop for 1 minute
  81.         int randomTrack = random(2, 8); // Generate a random track number between 2 and 7
  82.         myDFPlayer.play(randomTrack); // Play the random track
  83.         delay(10000); // Wait for 10 seconds before playing the next track
  84.     }
  85.  
  86.     // Shut down after 1 minute
  87.     digitalWrite(RELAY_PIN, LOW); // Deactivate relay
  88.     delay(60000); // Wait for 1 minute before restarting
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement