CCCPaste

Audio LED rev_04

/********* 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 LED
	- Source Code NOT compiled for: Arduino Uno
	- Source Code created on: 2024-09-22 21:10:42

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* play track for 3 minutes.  Shut down for 2 minutes */
	/* then loop */
/****** SYSTEM REQUIREMENT 2 *****/
	/* Play a random audio track (from 2 to 5) using the */
	/* DFRobot DFPlayer Mini module for 3 minutes, */
	/* followed by a 2-minute shutdown, then repeat the */
	/* cycle. Control LED indicators during playback and */
	/* shutdown phases. */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include 
#include 	//https://github.com/DFRobot/DFRobotDFPlayerMini

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs(void);

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t LED_LED_PIN_D2		= 2; // LED for playback indication
const uint8_t LED_2_LED_PIN_D3		= 3; // LED for shutdown indication
const uint8_t LED_3_LED_PIN_D4		= 4; // Unused LED

/***** DEFINITION OF Software Serial *****/
const uint8_t DFPlayer_DFPlayerMini_Serial_PIN_SERIAL_TX_A0		= A0;
const uint8_t DFPlayer_DFPlayerMini_Serial_PIN_SERIAL_RX_A1		= A1;
SoftwareSerial DFPlayer_DFPlayerMini_Serial(DFPlayer_DFPlayerMini_Serial_PIN_SERIAL_RX_A1, DFPlayer_DFPlayerMini_Serial_PIN_SERIAL_TX_A0);

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
bool	LED_LED_PIN_D2_rawData		= 0; // Playback LED state
bool	LED_2_LED_PIN_D3_rawData		= 0; // Shutdown LED state
bool	LED_3_LED_PIN_D4_rawData		= 0; // Unused LED state

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

void setup(void)
{
	// put your setup code here, to run once:

	pinMode(LED_LED_PIN_D2,	 OUTPUT); // Set playback LED pin as output
	pinMode(LED_2_LED_PIN_D3,	 OUTPUT); // Set shutdown LED pin as output
	pinMode(LED_3_LED_PIN_D4,	 OUTPUT); // Set unused LED pin as output

	// Start the software serial for DFPlayer
	DFPlayer_DFPlayerMini_Serial.begin(9600);

	// Initialize DFPlayer
	if (!myDFPlayer.begin(DFPlayer_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); // Loop forever if initialization fails
	}
	Serial.println(F("DFPlayer Mini online."));
}

void loop(void)
{
	// put your main code here, to run repeatedly:

	// Play a random audio track from 2 to 5 for 3 minutes
	unsigned long playbackStartTime = millis(); // Record the start time of playback
	while (millis() - playbackStartTime < 180000) { // Loop for 3 minutes (180000 ms)
		int randomTrack = random(2, 6); // Generate a random track number between 2 and 5
		myDFPlayer.play(randomTrack); // Play the selected track
		LED_LED_PIN_D2_rawData = HIGH; // Turn on playback LED
		updateOutputs(); // Update LED states
		delay(60000); // Wait for 1 minute before playing the next track
	}

	// Shutdown phase for 2 minutes
	LED_LED_PIN_D2_rawData = LOW; // Turn off playback LED
	LED_2_LED_PIN_D3_rawData = HIGH; // Turn on shutdown LED
	updateOutputs(); // Update LED states
	delay(120000); // Wait for 2 minutes

	// Turn off shutdown LED after the shutdown phase
	LED_2_LED_PIN_D3_rawData = LOW; // Turn off shutdown LED
	updateOutputs(); // Update LED states
}

void updateOutputs(void) {
	// Update the state of the LEDs based on the raw data
	digitalWrite(LED_LED_PIN_D2, LED_LED_PIN_D2_rawData); // Update playback LED
	digitalWrite(LED_2_LED_PIN_D3, LED_2_LED_PIN_D3_rawData); // Update shutdown LED
	digitalWrite(LED_3_LED_PIN_D4, LED_3_LED_PIN_D4_rawData); // Update unused LED
}

/* END CODE */