CCCPaste

LED Audio rev_01

/********* 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: LED Audio
	- Source Code NOT compiled for: Arduino Nano
	- Source Code created on: 2024-09-20 18:41:22

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Turn on LED and DFPlayer when board starts.  Play */
	/* track one while LED flickers.  Play a random track */
	/* 2-6 immediately after track one while LED is lit. */
	/* After 3 minutes the board stops.  After 5 minutes */
	/* board starts sequence again. */
/****** END SYSTEM REQUIREMENTS *****/

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

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

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t LED_LED_PIN_D2		= 2;

/***** DEFINITION OF Software Serial *****/
const uint8_t Player_DFPlayerMini_Serial_PIN_SERIAL_TX_A0		= A0;
const uint8_t Player_DFPlayerMini_Serial_PIN_SERIAL_RX_A1		= A1;
SoftwareSerial Player_DFPlayerMini_Serial(Player_DFPlayerMini_Serial_PIN_SERIAL_RX_A1, Player_DFPlayerMini_Serial_PIN_SERIAL_TX_A0);

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
bool	LED_LED_PIN_D2_rawData		= 0;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
float	LED_LED_PIN_D2_phyData		= 0.0;

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

// Timing variables
unsigned long startMillis;
unsigned long currentMillis;
const unsigned long threeMinutes = 180000; // 3 minutes in milliseconds
const unsigned long fiveMinutes = 300000; // 5 minutes in milliseconds
bool sequenceActive = false;

void setup(void)
{
	// put your setup code here, to run once:
	pinMode(LED_LED_PIN_D2, OUTPUT);
	
	// Initialize the software serial for DFPlayer Mini
	Player_DFPlayerMini_Serial.begin(9600);
	
	// Initialize the DFPlayer Mini
	if (!myDFPlayer.begin(Player_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
	}
	
	// Set the volume (0 to 30)
	myDFPlayer.volume(10);
	Serial.println(F("DFPlayer Mini online."));
	
	// Start the sequence
	startMillis = millis();
	sequenceActive = true;
	myDFPlayer.play(1); // Play track 1
}

void loop(void)
{
	// put your main code here, to run repeatedly:
	currentMillis = millis();
	
	if (sequenceActive) {
		// Flicker LED while track 1 is playing
		digitalWrite(LED_LED_PIN_D2, HIGH);
		delay(250); // LED on for 250ms
		digitalWrite(LED_LED_PIN_D2, LOW);
		delay(250); // LED off for 250ms
		
		// Check if 3 minutes have passed
		if (currentMillis - startMillis >= threeMinutes) {
			sequenceActive = false; // Stop the sequence
			digitalWrite(LED_LED_PIN_D2, LOW); // Turn off LED
			myDFPlayer.stop(); // Stop playing
		}
	} else {
		// Play a random track between 2 and 6 while LED is lit
		if (currentMillis - startMillis < threeMinutes + 1000) { // Allow 1 second after stopping track 1
			playRandomTrack(); // Play a random track
			digitalWrite(LED_LED_PIN_D2, HIGH); // Keep LED on while playing random track
			delay(1000); // Play random track for 1 second
			digitalWrite(LED_LED_PIN_D2, LOW); // Turn off LED
		}
		
		// Check if 5 minutes have passed to restart the sequence
		if (currentMillis - startMillis >= fiveMinutes) {
			startMillis = currentMillis; // Reset timer
			sequenceActive = true; // Restart sequence
			myDFPlayer.play(1); // Play track 1 again
		}
	}
	
	updateOutputs(); // Refresh output data
}

void updateOutputs()
{
	// Update the LED state based on the raw data
	digitalWrite(LED_LED_PIN_D2, LED_LED_PIN_D2_rawData);
}

void playRandomTrack() {
	// Play a random track between 2 and 6
	int randomTrack = random(2, 7); // Generates a random number between 2 and 6
	myDFPlayer.play(randomTrack);
}

/* END CODE */