/********* 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 Uno
	- Source Code created on: 2024-09-20 20:24:24

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* turn on LED light.  Start the DFPlayer and play */
	/* track 1.  then play a random track from 2 - 5. */
	/* Stop after 3 minutes.  loop again after 5 minutes. */
/****** 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);
void updateOutputs(void);
void playRandomTrack(); // Function to play a random track

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

/***** 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 *****/
/***** used to store raw data *****/
bool LED_LED_PIN_D2_rawData = 0;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float LED_LED_PIN_D2_phyData = 0.0;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
DFRobotDFPlayerMini myDFPlayer; // Instantiate the DFPlayer Mini object

// Timing variables
unsigned long startTime;
bool isPlaying = false;

void setup(void)
{
    // put your setup code here, to run once:
    pinMode(LED_LED_PIN_D2, OUTPUT); // Set LED pin as output

    // Initialize software serial for DFPlayer
    DFPlayer_DFPlayerMini_Serial.begin(9600); // Start serial communication at 9600 baud

    // 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); // Stop execution if initialization fails
    }
    Serial.println(F("DFPlayer Mini online."));
    myDFPlayer.volume(10); // Set volume value (0~30)

    // Start the LED and play track 1
    digitalWrite(LED_LED_PIN_D2, HIGH); // Turn on LED
    myDFPlayer.play(1); // Play the first mp3
    startTime = millis(); // Record the start time
    isPlaying = true; // Set the playing state
}

void loop(void)
{
    // put your main code here, to run repeatedly:
    updateOutputs(); // Refresh output data

    // Check if 3 minutes have passed
    if (isPlaying && (millis() - startTime >= 180000)) {
        myDFPlayer.stop(); // Stop playing after 3 minutes
        digitalWrite(LED_LED_PIN_D2, LOW); // Turn off LED
        isPlaying = false; // Update playing state
        delay(300000); // Wait for 5 minutes before looping again
        digitalWrite(LED_LED_PIN_D2, HIGH); // Turn on LED again
        myDFPlayer.play(1); // Play track 1 again
        startTime = millis(); // Reset the start time
        isPlaying = true; // Set the playing state
    }

    // If not playing, play a random track from 2 to 5
    if (!isPlaying) {
        playRandomTrack(); // Play a random track
    }
}

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

void playRandomTrack() {
    int randomTrack = random(2, 6); // Generate a random track number between 2 and 5
    myDFPlayer.play(randomTrack); // Play the random track
    delay(1000); // Delay to allow track to play before returning to loop
}

/* END CODE */