Advertisement
pleasedontcode

Button Control rev_02

Sep 30th, 2024
47
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 Control
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-09-30 16:23:30
  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.  
  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.  
  36. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  37. const uint8_t button_PushButton_PIN_D2      = 2;
  38.  
  39. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  40. // Instance of the button using the EasyButton library.
  41. EasyButton button(button_PushButton_PIN_D2); // Initialize the EasyButton object with the button pin
  42.  
  43. // Callback function to be called when the button is pressed for the given duration.
  44. void onPressedForDuration()
  45. {
  46.   // Code to be executed when the button is pressed for the given duration.
  47. }
  48.  
  49. // Variables to manage the state of the loops
  50. bool firstLoopComplete = false; // Flag to indicate if the first loop is complete
  51.  
  52. void setup(void)
  53. {
  54.     // put your setup code here, to run once:
  55.     pinMode(button_PushButton_PIN_D2, INPUT_PULLUP); // Set the button pin as input with pull-up resistor
  56.  
  57.     // Initialize the button.
  58.     button.begin();
  59.     // Add the callback function to be called when the button is pressed for at least the given time.
  60.     button.onPressedFor(2000, onPressedForDuration); // Set the duration for the button press
  61. }
  62.  
  63. void loop(void)
  64. {
  65.     // Continuously read the status of the button
  66.     button.read(); // Update button state
  67.  
  68.     // Check if the button is pressed
  69.     if (button.isPressed()) {
  70.         // If the button is pressed, mark the first loop as incomplete
  71.         firstLoopComplete = true; // Remember that the button was pressed
  72.     }
  73.  
  74.     // First loop logic
  75.     if (!firstLoopComplete) {
  76.         // Execute the first loop code here
  77.         // This loop will repeat until the button is pressed
  78.         // Add your first loop code here
  79.     } else {
  80.         // Second loop logic
  81.         // Execute the second loop code here
  82.         // This loop will execute after the button has been pressed
  83.         // Add your second loop code here
  84.         // Optionally, reset the flag if you want to return to the first loop after the second loop
  85.         firstLoopComplete = false; // Reset the flag to allow returning to the first loop
  86.     }
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement