/********* 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 MP3 Controller
- Source Code NOT compiled for: Arduino Nano
- Source Code created on: 2024-08-02 11:51:47
********* Pleasedontcode.com **********/
/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
/* On button, fire led rapidly 3 times and trigger an */
/* mp3 . Don’t use easy button */
/****** 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);
/***** 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 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 *****/
/***** used to store raw data *****/
bool Led_LED_PIN_D3_rawData = 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(Button_PushButton_PIN_D2, INPUT_PULLUP); // Set button pin as input with pull-up resistor
pinMode(Led_LED_PIN_D3, OUTPUT); // Set LED pin as output
// Initialize the software serial for the 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); // Stay here if initialization fails
}
Serial.println(F("DFPlayer Mini online."));
myDFPlayer.setTimeOut(500); // Set serial communication timeout to 500ms
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
}
void updateOutputs()
{
digitalWrite(Led_LED_PIN_D3, Led_LED_PIN_D3_rawData); // Update LED state
}
void handleButtonPress()
{
// Check if the button is pressed (active low)
if (digitalRead(Button_PushButton_PIN_D2) == LOW) {
// Fire LED rapidly 3 times
for (int i = 0; i < 3; i++) {
Led_LED_PIN_D3_rawData = HIGH; // Turn on LED
updateOutputs(); // Update LED state
delay(100); // Wait for 100 milliseconds
Led_LED_PIN_D3_rawData = LOW; // Turn off LED
updateOutputs(); // Update LED state
delay(100); // Wait for 100 milliseconds
}
// Trigger an MP3 file (e.g., play the first mp3)
myDFPlayer.play(1); // Play the first mp3
delay(1000); // Debounce delay to prevent multiple triggers
}
}
/* END CODE */