Advertisement
pleasedontcode

LED MP3 Trigger rev_01

Jul 27th, 2024
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: LED MP3 Trigger
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-07-27 08:48:14
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* flash led and play mp3 at push of button */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /****** DEFINITION OF LIBRARIES *****/
  24. #include <EasyButton.h> // https://github.com/evert-arias/EasyButton
  25. #include <Arduino.h> // Include Arduino core library for basic functions
  26. #include <SPI.h> // Include SPI library for MP3 module (if needed)
  27. #include <SD.h> // Include SD library for MP3 file access
  28. #include <TMRpcm.h> // Include TMRpcm library for playing audio
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t moment1_PushButton_PIN_D4 = 4; // Button pin
  36. const uint8_t ledPin = 13; // LED pin (usually built-in on many boards)
  37.  
  38. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  39. // Create an instance of EasyButton for the button connected to pin D4
  40. EasyButton button(moment1_PushButton_PIN_D4); // Initialize the EasyButton object
  41. TMRpcm audio; // Create an instance of TMRpcm for audio playback
  42.  
  43. void setup(void)
  44. {
  45.     // put your setup code here, to run once:
  46.     Serial.begin(115200); // Initialize Serial for debugging
  47.     Serial.println();
  48.     Serial.println(">>> EasyButton example <<<");
  49.  
  50.     pinMode(moment1_PushButton_PIN_D4, INPUT_PULLUP); // Set the button pin as input with pull-up resistor
  51.     pinMode(ledPin, OUTPUT); // Set the LED pin as output
  52.  
  53.     button.begin(); // Initialize the button
  54.     audio.speakerPin = 9; // Set the speaker pin for audio playback (change as needed)
  55.  
  56.     button.onPressed([]() { // Set the callback for button press
  57.         Serial.println("Button pressed"); // Print message when button is pressed
  58.  
  59.         // Flash the LED
  60.         digitalWrite(ledPin, HIGH); // Turn LED on
  61.         delay(100); // Wait for 100 milliseconds
  62.         digitalWrite(ledPin, LOW); // Turn LED off
  63.  
  64.         // Play MP3
  65.         audio.play("sound.mp3"); // Play the MP3 file (ensure it's on the SD card)
  66.     });
  67. }
  68.  
  69. void loop(void)
  70. {
  71.     // put your main code here, to run repeatedly:
  72.     button.read(); // Continuously read the status of the button
  73. }
  74.  
  75. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement