Advertisement
pleasedontcode

Audio Relay rev_07

Sep 22nd, 2024
94
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:53:04
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The Haunted Radio project utilizes the DFRobot */
  21.     /* DFPlayer Mini for audio playback, controlled via */
  22.     /* SoftwareSerial on pins A0 and A1, ensuring */
  23.     /* seamless integration with Arduino for sound */
  24.     /* effects. */
  25. /****** SYSTEM REQUIREMENT 2 *****/
  26.     /* Turn on relay on D2  Play track 1 for 14 seconds. */
  27.     /* Play a random track between 2 and 7 for 1 minute */
  28.     /* Shut down and wait 1 minute.  restart */
  29. /****** END SYSTEM REQUIREMENTS *****/
  30.  
  31. /****** DEFINITION OF LIBRARIES *****/
  32. #include <SoftwareSerial.h>
  33. #include <DFRobotDFPlayerMini.h>    //https://github.com/DFRobot/DFRobotDFPlayerMini
  34.  
  35. /****** FUNCTION PROTOTYPES *****/
  36. void setup(void);
  37. void loop(void);
  38.  
  39. /***** DEFINITION OF Software Serial *****/
  40. const uint8_t DFP_DFPlayerMini_Serial_PIN_SERIAL_TX_A0      = A0;
  41. const uint8_t DFP_DFPlayerMini_Serial_PIN_SERIAL_RX_A1      = A1;
  42. SoftwareSerial DFP_DFPlayerMini_Serial(DFP_DFPlayerMini_Serial_PIN_SERIAL_RX_A1, DFP_DFPlayerMini_Serial_PIN_SERIAL_TX_A0);
  43.  
  44. /****** DEFINITION OF RELAY PIN *****/
  45. const uint8_t RELAY_PIN = 2; // Define the pin for the relay
  46.  
  47. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  48. // Create an instance of the DFRobotDFPlayerMini class
  49. DFRobotDFPlayerMini myDFPlayer; // Instance of DFPlayer Mini
  50.  
  51. void setup(void)
  52. {
  53.     // Initialize the software serial for DFPlayer Mini
  54.     DFP_DFPlayerMini_Serial.begin(9600);
  55.    
  56.     // Initialize the serial monitor for debugging
  57.     Serial.begin(115200);
  58.     Serial.println(F("DFRobot DFPlayer Mini Demo"));
  59.  
  60.     // Initialize the DFPlayer Mini
  61.     if (!myDFPlayer.begin(DFP_DFPlayerMini_Serial, /*isACK = */true, /*doReset = */true)) {  
  62.         Serial.println(F("Unable to begin:"));
  63.         Serial.println(F("1.Please recheck the connection!"));
  64.         Serial.println(F("2.Please insert the SD card!"));
  65.         while(true); // Stop the program if initialization fails
  66.     }
  67.     Serial.println(F("DFPlayer Mini online."));
  68.  
  69.     // Set volume and other configurations
  70.     myDFPlayer.volume(10);  // Set volume value (0~30)
  71.     myDFPlayer.setTimeOut(500); // Set serial communication timeout
  72.    
  73.     // Initialize the relay pin
  74.     pinMode(RELAY_PIN, OUTPUT);
  75. }
  76.  
  77. void loop(void)
  78. {
  79.     // Turn on relay
  80.     digitalWrite(RELAY_PIN, HIGH); // Activate relay
  81.     myDFPlayer.play(1); // Play track 1
  82.     delay(14000); // Wait for 14 seconds
  83.  
  84.     // Turn off relay
  85.     digitalWrite(RELAY_PIN, LOW); // Deactivate relay
  86.     delay(1000); // Short delay before next action
  87.  
  88.     // Play a random track between 2 and 7 for 1 minute
  89.     unsigned long startTime = millis();
  90.     while (millis() - startTime < 60000) { // Loop for 1 minute
  91.         int randomTrack = random(2, 8); // Generate random track number between 2 and 7
  92.         myDFPlayer.play(randomTrack); // Play the random track
  93.         delay(10000); // Wait for 10 seconds before playing another track
  94.     }
  95.  
  96.     // Shut down and wait for 1 minute
  97.     myDFPlayer.stop(); // Stop playback
  98.     delay(60000); // Wait for 1 minute before restarting
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement