CCCPaste

MP3 Control rev_02

/********* 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: MP3 Control
	- Source Code NOT compiled for: ESP32 DevKit V1
	- Source Code created on: 2024-07-27 08:51:39

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

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

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

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

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t moment1_PushButton_PIN_D4 = 4;

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t led1_LED_PIN_D16 = 16;

/***** DEFINITION OF Software Serial *****/
const uint8_t mp3player_DFPlayerMini_Serial_PIN_SERIAL_TX_D13 = 13;
const uint8_t mp3player_DFPlayerMini_Serial_PIN_SERIAL_RX_D14 = 14;
EspSoftwareSerial::UART mp3player_DFPlayerMini_Serial;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
// Create an instance of EasyButton for the push button
EasyButton button(moment1_PushButton_PIN_D4); // Initialize EasyButton with the button pin

// Create an instance of DFRobotDFPlayerMini for the MP3 player
DFRobotDFPlayerMini myDFPlayer; // Initialize DFPlayer object

void setup(void)
{
    // Initialize Serial for debugging purposes
    Serial.begin(115200);
    Serial.println();

    // Set button pin as input with pull-up resistor
    pinMode(moment1_PushButton_PIN_D4, INPUT_PULLUP);
    // Set LED pin as output
    pinMode(led1_LED_PIN_D16, OUTPUT);

    // Initialize Software Serial for MP3 player
    mp3player_DFPlayerMini_Serial.begin(9600, SWSERIAL_8N1, mp3player_DFPlayerMini_Serial_PIN_SERIAL_RX_D14, mp3player_DFPlayerMini_Serial_PIN_SERIAL_TX_D13, false);

    // Initialize the DFPlayer
    if (!myDFPlayer.begin(mp3player_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) {
            delay(0); // Code to compatible with ESP8266 watch dog.
        }
    }
    Serial.println(F("DFPlayer Mini online."));
    myDFPlayer.volume(10);  // Set volume (0-30)

    // Initialize the EasyButton
    button.begin(); // Initialize the button
    button.onPressed([]() { // Set the callback for when the button is pressed
        Serial.println("Button pressed");
        digitalWrite(led1_LED_PIN_D16, HIGH); // Turn on the LED
        myDFPlayer.play(1); // Play the first mp3
        delay(1000); // Keep LED on for 1 second
        digitalWrite(led1_LED_PIN_D16, LOW); // Turn off the LED
    });
}

void loop(void)
{
    // Continuously read the status of the button
    button.read(); 

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

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 */