pleasedontcode

Debounced Button rev_08

Oct 5th, 2025
140
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: Debounced Button
  13.     - Source Code NOT compiled for: Arduino Opta WiFi
  14.     - Source Code created on: 2025-10-05 12:05:27
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Detect a debounced push-button event on A0 using */
  21.     /* EasyButton; the input uses INPUT_PULLUP, so a */
  22.     /* pressed state is LOW and should trigger the system */
  23.     /* logic. Integrate flash over the air. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26.  
  27. /* START CODE */
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  31. #ifdef OTA_ENABLED
  32. #include <POTA.h> //https://github.com/pleasedontcode/POTA
  33. #endif
  34.  
  35. /****** FUNCTION PROTOTYPES *****/
  36. void setup(void);
  37. void loop(void);
  38. void onButtonPressed(void);
  39.  
  40. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  41. const uint8_t button_PushButton_PIN_A0 = A0;
  42.  
  43. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  44. EasyButton button(button_PushButton_PIN_A0);
  45.  
  46. #ifdef OTA_ENABLED
  47. POTA ota; // OTA manager (requires credentials configured in secrets.h)
  48. volatile bool ota_trigger_requested = false;
  49. bool ota_initialized = false;
  50. #endif
  51.  
  52. void onButtonPressed(void)
  53. {
  54.   Serial.println("Button pressed"); // Debounced press detected by EasyButton
  55. #ifdef OTA_ENABLED
  56.   ota_trigger_requested = true;
  57. #endif
  58. }
  59.  
  60. void setup(void)
  61. {
  62.   // put your setup code here, to run once:
  63.   Serial.begin(115200);
  64.   // Ensure the input pin uses internal pull-up (pressed is LOW)
  65.   pinMode(button_PushButton_PIN_A0, INPUT_PULLUP);
  66.  
  67.   // Initialize EasyButton
  68.   button.begin();
  69.   button.onPressed(onButtonPressed);
  70.  
  71. #ifdef OTA_ENABLED
  72.   // Optional OTA initialization
  73.   // Uncomment and fill credentials to enable OTA updates
  74.   // Example (requires secrets.h):
  75.   // ota.begin(WIFI_SSID, WIFI_PASSWORD, DEVICE_TYPE, FIRMWARE_VERSION, AUTH_TOKEN, SERVER_SECRET);
  76.   // ota_initialized = true;
  77. #endif
  78. }
  79.  
  80. void loop(void)
  81. {
  82.   // Update button state
  83.   button.read();
  84.  
  85. #ifdef OTA_ENABLED
  86.   if (ota_trigger_requested)
  87.   {
  88.     ota_trigger_requested = false;
  89.     Serial.println("OTA update requested via button.");
  90.     // If OTA is properly configured, perform OTA check/start here.
  91.     // This sample intentionally avoids hard-coding credentials.
  92.     // Users should configure OTA by uncommenting initialization above
  93.     // and calling: ota.checkAndPerformOTA();
  94.   }
  95. #endif
  96.   // Other non-blocking tasks can be placed here
  97. }
  98.  
  99. /* END CODE */
  100.  
Advertisement
Add Comment
Please, Sign In to add comment