CCCPaste

Audio Control 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: Audio Control
	- Source Code NOT compiled for: Arduino Nano
	- Source Code created on: 2024-07-29 05:07:06

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* play single mp3 and flash led at each push of a */
	/* momentary button */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include 
#include 	//https://github.com/evert-arias/EasyButton
#include 	//https://github.com/DFRobot/DFRobotDFPlayerMini

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void onPressed(); // Function prototype for the onPressed callback
void printDetail(uint8_t type, int value); // Function prototype for printing DFPlayer details

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

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

/***** DEFINITION OF Software Serial *****/
const uint8_t plait_DFPlayerMini_Serial_PIN_SERIAL_TX_A0		= A0;
const uint8_t plait_DFPlayerMini_Serial_PIN_SERIAL_RX_A1		= A1;
SoftwareSerial plait_DFPlayerMini_Serial(plait_DFPlayerMini_Serial_PIN_SERIAL_RX_A1, plait_DFPlayerMini_Serial_PIN_SERIAL_TX_A0);

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
// Initialize the EasyButton object with the button pin
EasyButton button(button_PushButton_PIN_D2);

// Initialize the DFPlayer Mini object
DFRobotDFPlayerMini myDFPlayer; // Create an instance of the DFPlayer Mini

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

	plait_DFPlayerMini_Serial.begin(9600); // Initialize Software Serial for DFPlayer Mini

	button.begin(); // Initialize the EasyButton
	button.onPressed(onPressed); // Attach the onPressed callback function

	// Initialize the DFPlayer Mini
	if (!myDFPlayer.begin(plait_DFPlayerMini_Serial, true, true)) { // Use serial to communicate with DFPlayer
		Serial.println(F("Unable to begin. Check connection and SD card!")); // Error message if initialization fails
		while (true) delay(0); // Halt execution
	}
	myDFPlayer.volume(10);  // Set volume (0-30)
}

void loop(void)
{
	// put your main code here, to run repeatedly:
	button.read(); // Read the button state

	// Check if DFPlayer has available data
	if (myDFPlayer.available()) {
		printDetail(myDFPlayer.readType(), myDFPlayer.read()); // Print details from DFPlayer
	}
}

void onPressed()
{
	// Callback function for button press
	digitalWrite(led_LED_PIN_D3, HIGH); // Turn on LED
	myDFPlayer.play(1); // Play the first mp3
	delay(100); // Keep LED on for a short duration
	digitalWrite(led_LED_PIN_D3, LOW); // Turn off LED
}

void printDetail(uint8_t type, int value) {
	switch (type) {
		case DFPlayerCardInserted: Serial.println(F("Card Inserted!")); break;
		case DFPlayerCardRemoved: Serial.println(F("Card Removed!")); break;
		case DFPlayerPlayFinished: Serial.print(F("Number:")); Serial.print(value); Serial.println(F(" Play Finished!")); break;
		case DFPlayerError:
			switch (value) {
				case Busy: Serial.println(F("Card not found")); break;
				case Sleeping: Serial.println(F("Sleeping")); break;
				case FileIndexOut: Serial.println(F("File Index Out of Bound")); break;
				default: break;
			}
			break;
		default: break;
	}
}

/* END CODE */