Advertisement
pleasedontcode

LED Button rev_01

Aug 6th, 2024
313
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 Button
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-08-06 12:39:39
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* turn on LED ehen button is pressed */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /****** DEFINITION OF LIBRARIES *****/
  24. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  25.  
  26. /****** FUNCTION PROTOTYPES *****/
  27. void setup(void);
  28. void loop(void);
  29.  
  30. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  31. const uint8_t B1_PushButton_PIN_D2 = 2; // Button pin
  32. const uint8_t LED_PIN = 13; // LED pin (usually the built-in LED on Arduino Uno)
  33.  
  34. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  35. // Initialize the EasyButton object with the defined button pin
  36. EasyButton button(B1_PushButton_PIN_D2);
  37.  
  38. /****** FUNCTION DEFINITIONS *****/
  39. void onPressed() {
  40.     // Function to be called when the button is pressed
  41.     Serial.println("Button pressed");
  42.     digitalWrite(LED_PIN, HIGH); // Turn on the LED when the button is pressed
  43. }
  44.  
  45. void onReleased() {
  46.     // Function to be called when the button is released
  47.     Serial.println("Button released");
  48.     digitalWrite(LED_PIN, LOW); // Turn off the LED when the button is released
  49. }
  50.  
  51. void setup(void) {
  52.     // Start serial communication for debugging
  53.     Serial.begin(115200);
  54.     Serial.println();
  55.     Serial.println(">>> EasyButton pressed example <<<");
  56.  
  57.     // Initialize the button
  58.     button.begin();
  59.     // Attach the onPressed function to the button press event
  60.     button.onPressed(onPressed);
  61.     // Attach the onReleased function to the button release event
  62.     button.onReleased(onReleased);
  63.  
  64.     // Set the button pin as input with pull-up resistor
  65.     pinMode(B1_PushButton_PIN_D2, INPUT_PULLUP);
  66.     // Set the LED pin as output
  67.     pinMode(LED_PIN, OUTPUT);
  68.     digitalWrite(LED_PIN, LOW); // Ensure the LED is off at startup
  69. }
  70.  
  71. void loop(void) {
  72.     // Continuously read the button state
  73.     button.read();
  74. }
  75.  
  76. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement