Advertisement
pleasedontcode

"Button Detection" rev_28

Jul 3rd, 2024
667
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 Detection"
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-07-03 22:31:43
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* print on serial button state. */
  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. void onPressed();
  30.  
  31. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  32. const uint8_t button_PushButton_PIN_D4 = 4;
  33.  
  34. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  35. EasyButton button(button_PushButton_PIN_D4);  // Initialize EasyButton instance
  36.  
  37. // Callback function to be called when the button is pressed.
  38. void onPressed()
  39. {
  40.   Serial.println("Button pressed");
  41. }
  42.  
  43. void setup(void)
  44. {
  45.   // Initialize Serial for debugging purposes.
  46.   Serial.begin(115200);
  47.   Serial.println();
  48.   Serial.println(">>> EasyButton pressed example <<<");
  49.  
  50.   // Initialize the button.
  51.   button.begin();
  52.   // Add the callback function to be called when the button is pressed.
  53.   button.onPressed(onPressed);
  54. }
  55.  
  56. void loop(void)
  57. {
  58.   // Continuously read the status of the button.
  59.   button.read();
  60.  
  61.   // Print the current state of the button to the Serial monitor.
  62.   static bool lastState = !button.isPressed();  // Initialize with opposite state
  63.   bool currentState = button.isPressed();
  64.  
  65.   if (currentState != lastState) {
  66.     if (currentState) {
  67.       Serial.println("Button is currently pressed");
  68.     } else {
  69.       Serial.println("Button is currently released");
  70.     }
  71.     lastState = currentState;
  72.   }
  73. }
  74.  
  75. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement