CCCPaste

LED MP3 Trigger 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: LED MP3 Trigger
	- Source Code NOT compiled for: ESP32 DevKit V1
	- Source Code created on: 2024-07-27 08:48:14

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

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

/****** DEFINITION OF LIBRARIES *****/
#include 	// https://github.com/evert-arias/EasyButton
#include  // Include Arduino core library for basic functions
#include  // Include SPI library for MP3 module (if needed)
#include  // Include SD library for MP3 file access
#include  // Include TMRpcm library for playing audio

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

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t moment1_PushButton_PIN_D4 = 4; // Button pin
const uint8_t ledPin = 13; // LED pin (usually built-in on many boards)

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
// Create an instance of EasyButton for the button connected to pin D4
EasyButton button(moment1_PushButton_PIN_D4); // Initialize the EasyButton object
TMRpcm audio; // Create an instance of TMRpcm for audio playback

void setup(void)
{
	// put your setup code here, to run once:
	Serial.begin(115200); // Initialize Serial for debugging
	Serial.println();
	Serial.println(">>> EasyButton example <<<");

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

	button.begin(); // Initialize the button
	audio.speakerPin = 9; // Set the speaker pin for audio playback (change as needed)

	button.onPressed([]() { // Set the callback for button press
		Serial.println("Button pressed"); // Print message when button is pressed

		// Flash the LED
		digitalWrite(ledPin, HIGH); // Turn LED on
		delay(100); // Wait for 100 milliseconds
		digitalWrite(ledPin, LOW); // Turn LED off

		// Play MP3
		audio.play("sound.mp3"); // Play the MP3 file (ensure it's on the SD card)
	});
}

void loop(void)
{
	// put your main code here, to run repeatedly:
	button.read(); // Continuously read the status of the button
}

/* END CODE */