CCCPaste

Audio Relay rev_07

/********* 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:53:04

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* The Haunted Radio project utilizes the DFRobot */
	/* DFPlayer Mini for audio playback, controlled via */
	/* SoftwareSerial on pins A0 and A1, ensuring */
	/* seamless integration with Arduino for sound */
	/* effects. */
/****** SYSTEM REQUIREMENT 2 *****/
	/* Turn on relay on D2  Play track 1 for 14 seconds. */
	/* Play a random track between 2 and 7 for 1 minute */
	/* Shut down and wait 1 minute.  restart */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include 
#include 	//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 RELAY PIN *****/
const uint8_t RELAY_PIN = 2; // Define the pin for the relay

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
// Create an instance of the DFRobotDFPlayerMini class
DFRobotDFPlayerMini myDFPlayer; // Instance of DFPlayer Mini

void setup(void)
{
	// Initialize the software serial for DFPlayer Mini
	DFP_DFPlayerMini_Serial.begin(9600);
	
	// Initialize the serial monitor for debugging
	Serial.begin(115200);
	Serial.println(F("DFRobot DFPlayer Mini Demo"));

	// Initialize the DFPlayer Mini
	if (!myDFPlayer.begin(DFP_DFPlayerMini_Serial, /*isACK = */true, /*doReset = */true)) {  
		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 the program if initialization fails
	}
	Serial.println(F("DFPlayer Mini online."));

	// Set volume and other configurations
	myDFPlayer.volume(10);  // Set volume value (0~30)
	myDFPlayer.setTimeOut(500); // Set serial communication timeout
	
	// Initialize the relay pin
	pinMode(RELAY_PIN, OUTPUT);
}

void loop(void)
{
	// Turn on relay
	digitalWrite(RELAY_PIN, HIGH); // Activate relay
	myDFPlayer.play(1); // Play track 1
	delay(14000); // Wait for 14 seconds

	// Turn off relay
	digitalWrite(RELAY_PIN, LOW); // Deactivate relay
	delay(1000); // Short delay before next action

	// 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 random track number between 2 and 7
		myDFPlayer.play(randomTrack); // Play the random track
		delay(10000); // Wait for 10 seconds before playing another track
	}

	// Shut down and wait for 1 minute
	myDFPlayer.stop(); // Stop playback
	delay(60000); // Wait for 1 minute before restarting
}