Advertisement
pleasedontcode

"Button Status" rev_31

Jul 4th, 2024
960
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 Status"
  13.     - Source Code compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-07-04 21:59:06
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* print on serial button state. */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23.  
  24. /********* User code review feedback **********
  25. #### Feedback 1 ####
  26. - provide button status feedback on serial every 2s.
  27. ********* User code review feedback **********/
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <EasyButton.h>  //https://github.com/evert-arias/EasyButton
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35. void onPressed();
  36.  
  37. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  38. const uint8_t button_PushButton_PIN_D4 = 4;
  39.  
  40. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  41. EasyButton button(button_PushButton_PIN_D4);  // Initialize EasyButton instance
  42.  
  43. // Callback function to be called when the button is pressed.
  44. void onPressed()
  45. {
  46.   Serial.println("Button pressed");
  47. }
  48.  
  49. void setup(void)
  50. {
  51.   // Initialize Serial for debugging purposes.
  52.   Serial.begin(115200);
  53.   Serial.println();
  54.   Serial.println(">>> EasyButton pressed example <<<");
  55.  
  56.   // Initialize the button.
  57.   button.begin();
  58.   // Add the callback function to be called when the button is pressed.
  59.   button.onPressed(onPressed);
  60. }
  61.  
  62. void loop(void)
  63. {
  64.   // Continuously read the status of the button.
  65.   button.read();
  66.  
  67.   // Print the current state of the button to the Serial monitor every 2 seconds.
  68.   static unsigned long lastPrintTime = 0;
  69.   unsigned long currentTime = millis();
  70.  
  71.   if (currentTime - lastPrintTime >= 2000) {
  72.     if (button.isPressed()) {
  73.       Serial.println("Button is currently pressed");
  74.     } else {
  75.       Serial.println("Button is currently released");
  76.     }
  77.     lastPrintTime = currentTime;
  78.   }
  79. }
  80.  
  81. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement