Advertisement
pleasedontcode

Button Management rev_01

Sep 30th, 2024
55
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 Management
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-09-30 16:21:39
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* you have to make sure that the first loop repeats */
  21.     /* until the button is pressed. if it is pressed */
  22.     /* while Arduino is executing the first loop, Arduino */
  23.     /* must be able to remember that the key was pressed */
  24.     /* to thus start the second loop if the memory that */
  25.     /* the */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34.  
  35. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  36. const uint8_t button_PushButton_PIN_D2      = 2;
  37.  
  38. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  39. // Instance of the button using the EasyButton library.
  40. EasyButton button(button_PushButton_PIN_D2); // Initialize the EasyButton object with the button pin
  41.  
  42. // Callback function to be called when the button is pressed for the given duration.
  43. void onPressedForDuration()
  44. {
  45.   // Code to be executed when the button is pressed for the given duration.
  46. }
  47.  
  48. // Variables to manage the state of the loops
  49. bool firstLoopComplete = false; // Flag to indicate if the first loop is complete
  50.  
  51. void setup(void)
  52. {
  53.     // put your setup code here, to run once:
  54.     pinMode(button_PushButton_PIN_D2, INPUT_PULLUP); // Set the button pin as input with pull-up resistor
  55.  
  56.     // Initialize the button.
  57.     button.begin();
  58.     // Add the callback function to be called when the button is pressed for at least the given time.
  59.     button.onPressedFor(2000, onPressedForDuration); // Set the duration for the button press
  60. }
  61.  
  62. void loop(void)
  63. {
  64.     // Continuously read the status of the button
  65.     button.read(); // Update button state
  66.  
  67.     // Check if the button is pressed
  68.     if (button.isPressed()) {
  69.         // If the button is pressed, mark the first loop as incomplete
  70.         firstLoopComplete = true; // Remember that the button was pressed
  71.     }
  72.  
  73.     // First loop logic
  74.     if (!firstLoopComplete) {
  75.         // Execute the first loop code here
  76.         // This loop will repeat until the button is pressed
  77.         // Add your first loop code here
  78.     } else {
  79.         // Second loop logic
  80.         // Execute the second loop code here
  81.         // This loop will execute after the button has been pressed
  82.         // Add your second loop code here
  83.         // Optionally, reset the flag if you want to return to the first loop after the second loop
  84.         firstLoopComplete = false; // Reset the flag to allow returning to the first loop
  85.     }
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement