Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Audio Relay
- - Source Code NOT compiled for: Arduino Nano
- - Source Code created on: 2024-09-22 21:35:38
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* DFRobot DFPlayer Mini via SoftwareSerial on pins */
- /* A0 and A1 change a relay on D2. Play track 1 for */
- /* 14 seconds then play a random track between 2 and */
- /* 7 for 1 minute. Shut down after 1 minute. wait 1 */
- /* minute and restart */
- /****** END SYSTEM REQUIREMENTS *****/
- /****** DEFINITION OF LIBRARIES *****/
- #include <SoftwareSerial.h>
- #include <DFRobotDFPlayerMini.h> // https://github.com/DFRobot/DFRobotDFPlayerMini
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /***** DEFINITION OF Software Serial *****/
- const uint8_t DFP_DFPlayerMini_Serial_PIN_SERIAL_TX_A0 = A0;
- const uint8_t DFP_DFPlayerMini_Serial_PIN_SERIAL_RX_A1 = A1;
- SoftwareSerial DFP_DFPlayerMini_Serial(DFP_DFPlayerMini_Serial_PIN_SERIAL_RX_A1, DFP_DFPlayerMini_Serial_PIN_SERIAL_TX_A0);
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- // Instantiate the DFRobotDFPlayerMini object
- DFRobotDFPlayerMini myDFPlayer; // Create an instance of the DFPlayer class
- // Define relay pin
- const uint8_t RELAY_PIN = 2; // Pin to control the relay
- void setup(void)
- {
- // Initialize the software serial communication
- DFP_DFPlayerMini_Serial.begin(9600);
- Serial.begin(115200); // Initialize serial for debugging
- // Initialize the relay pin as output
- pinMode(RELAY_PIN, OUTPUT);
- digitalWrite(RELAY_PIN, LOW); // Ensure relay is off initially
- // Initialize the DFPlayer
- if (!myDFPlayer.begin(DFP_DFPlayerMini_Serial, true, true)) { // Use serial to communicate with mp3
- Serial.println(F("Unable to begin:"));
- Serial.println(F("1. Please recheck the connection!"));
- Serial.println(F("2. Please insert the SD card!"));
- while (true); // Stop execution if initialization fails
- }
- Serial.println(F("DFPlayer Mini online."));
- // Set volume and other settings as needed
- myDFPlayer.volume(10); // Set volume value (0~30)
- myDFPlayer.setTimeOut(500); // Set serial communication timeout
- }
- void loop(void)
- {
- // Play track 1 for 14 seconds
- myDFPlayer.play(1);
- digitalWrite(RELAY_PIN, HIGH); // Activate relay
- delay(14000); // Wait for 14 seconds
- // Play a random track between 2 and 7 for 1 minute
- unsigned long startTime = millis();
- while (millis() - startTime < 60000) { // Loop for 1 minute
- int randomTrack = random(2, 8); // Generate a random track number between 2 and 7
- myDFPlayer.play(randomTrack); // Play the random track
- delay(10000); // Wait for 10 seconds before playing the next track
- }
- // Shut down after 1 minute
- digitalWrite(RELAY_PIN, LOW); // Deactivate relay
- delay(60000); // Wait for 1 minute before restarting
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement