CCCPaste

LED Audio rev_03

/********* 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-08-01 22:41:19

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* on button, flash the led 3x and play an mp3. user */
	/* internal pull-up */
/****** 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 handleButtonPress(void); // Function to handle button press

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t tt_PushButton_PIN_D2		= 2;

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t hh_LED_PIN_D3		= 3;

/***** DEFINITION OF Software Serial *****/
const uint8_t gg_DFPlayerMini_Serial_PIN_SERIAL_TX_A0		= A0;
const uint8_t gg_DFPlayerMini_Serial_PIN_SERIAL_RX_A1		= A1;
SoftwareSerial gg_DFPlayerMini_Serial(gg_DFPlayerMini_Serial_PIN_SERIAL_RX_A1, gg_DFPlayerMini_Serial_PIN_SERIAL_TX_A0);

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
bool	hh_LED_PIN_D3_rawData		= 0;

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

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

void setup(void)
{
	// put your setup code here, to run once:
	pinMode(tt_PushButton_PIN_D2, INPUT_PULLUP); // Set button pin as input with internal pull-up
	pinMode(hh_LED_PIN_D3, OUTPUT); // Set LED pin as output

	gg_DFPlayerMini_Serial.begin(9600); // Initialize software serial for DFPlayer
	Serial.begin(115200); // Initialize serial for debugging

	// Initialize DFPlayer
	if (!myDFPlayer.begin(gg_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)
}

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

	if (myDFPlayer.available()) {
		// Handle DFPlayer messages
		printDetail(myDFPlayer.readType(), myDFPlayer.read());
	}
}

void updateOutputs()
{
	digitalWrite(hh_LED_PIN_D3, hh_LED_PIN_D3_rawData);
}

void handleButtonPress() {
	// Check if the button is pressed (LOW because of pull-up)
	if (digitalRead(tt_PushButton_PIN_D2) == LOW) {
		// Flash the LED 3 times
		for (int i = 0; i < 3; i++) {
			digitalWrite(hh_LED_PIN_D3, HIGH); // Turn LED on
			delay(250); // Wait for 250 ms
			digitalWrite(hh_LED_PIN_D3, LOW); // Turn LED off
			delay(250); // Wait for 250 ms
		}
		// Play an MP3 file (e.g., the first file)
		myDFPlayer.play(1); // Play the first MP3 file
		delay(1000); // Delay to avoid multiple triggers
	}
}

void printDetail(uint8_t type, int value) {
	// Function to print the details of DFPlayer state and errors
	switch (type) {
		case TimeOut:
			Serial.println(F("Time Out!"));
			break;
		case WrongStack:
			Serial.println(F("Stack Wrong!"));
			break;
		case DFPlayerCardInserted:
			Serial.println(F("Card Inserted!"));
			break;
		case DFPlayerCardRemoved:
			Serial.println(F("Card Removed!"));
			break;
		case DFPlayerCardOnline:
			Serial.println(F("Card Online!"));
			break;
		case DFPlayerUSBInserted:
			Serial.println(F("USB Inserted!"));
			break;
		case DFPlayerUSBRemoved:
			Serial.println(F("USB Removed!"));
			break;
		case DFPlayerPlayFinished:
			Serial.print(F("Number:"));
			Serial.print(value);
			Serial.println(F(" Play Finished!"));
			break;
		case DFPlayerError:
			Serial.print(F("DFPlayerError:"));
			switch (value) {
				case Busy:
					Serial.println(F("Card not found"));
					break;
				case Sleeping:
					Serial.println(F("Sleeping"));
					break;
				case SerialWrongStack:
					Serial.println(F("Get Wrong Stack"));
					break;
				case CheckSumNotMatch:
					Serial.println(F("Check Sum Not Match"));
					break;
				case FileIndexOut:
					Serial.println(F("File Index Out of Bound"));
					break;
				case FileMismatch:
					Serial.println(F("Cannot Find File"));
					break;
				case Advertise:
					Serial.println(F("In Advertise"));
					break;
				default:
					break;
			}
			break;
		default:
			break;
	}
}

/* END CODE */