Advertisement
pleasedontcode

Button Example rev_01

Oct 26th, 2024
123
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: Button Example
  13.     - Source Code NOT compiled for: Arduino Mega
  14.     - Source Code created on: 2024-10-26 19:51:02
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Implement a push button interface using EasyButton */
  21.     /* library to detect button presses on digital pin */
  22.     /* D2, ensuring reliable input handling with pull-up */
  23.     /* resistor configuration. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <EasyButton.h>  // https://github.com/evert-arias/EasyButton
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32.  
  33. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  34. const uint8_t PUSH_PushButton_PIN_D2 = 2; // Define the pin for the push button
  35.  
  36. /***** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  37. // Create an instance of EasyButton for the push button
  38. EasyButton pushButton(PUSH_PushButton_PIN_D2, 35, true, true); // Initialize EasyButton with the pin number, debounce time, pull-up enabled, and active low
  39.  
  40. // Function to be called when the button is pressed
  41. void onPushButtonPressed() {
  42.     Serial.println("Push button pressed"); // Print message to Serial Monitor
  43. }
  44.  
  45. void setup(void) {
  46.     // Start serial communication at 115200 baud rate
  47.     Serial.begin(115200);
  48.  
  49.     // Print a message to indicate setup is complete
  50.     Serial.println();
  51.     Serial.println(">>> EasyButton example <<<");
  52.  
  53.     // Initialize the push button
  54.     pushButton.begin(); // Call begin() to set up the button
  55.     pushButton.onPressed(onPushButtonPressed); // Attach the onPressed event handler
  56.  
  57.     // Set the pin mode for the push button
  58.     // The pin mode is already handled by EasyButton's begin() method with pull-up enabled
  59. }
  60.  
  61. void loop(void) {
  62.     // Continuously read the button state
  63.     pushButton.read(); // Call read() to check the button state
  64. }
  65.  
  66. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement