Advertisement
pleasedontcode

Button Control rev_01

Oct 2nd, 2024
59
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 NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-10-02 08:44:36
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Arduino library for debouncing momentary contact */
  21.     /* switches, detect press, release, long press and */
  22.     /* sequences with event definitions and callbacks. */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <EasyButton.h> // https://github.com/evert-arias/EasyButton
  27. #include <EasyButtonBase.h> // Include EasyButtonBase library
  28. #include <EasyButtonTouch.h> // Include EasyButtonTouch library
  29. #include <EasyButtonVirtual.h> // Include EasyButtonVirtual library
  30. #include <Sequence.h> // Include Sequence library
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35.  
  36. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  37. const uint8_t elkewk_PushButton_PIN_D2 = 2;
  38. const uint8_t elkewk_TouchButton_PIN_D3 = 3; // Example pin for touch button
  39. const uint8_t elkewk_VirtualButton_PIN_D4 = 4; // Example pin for virtual button
  40.  
  41. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  42. // Initialize EasyButton object for the defined pin
  43. EasyButton button1(elkewk_PushButton_PIN_D2);
  44. EasyButtonTouch touchButton(elkewk_TouchButton_PIN_D3); // Initialize EasyButtonTouch object
  45. EasyButtonVirtual virtualButton(button1.read(), true); // Initialize EasyButtonVirtual object
  46.  
  47. // Initialize a Sequence object (example usage)
  48. Sequence buttonSequence(3, 1000); // Example for 3 sequences with a duration of 1000 ms
  49.  
  50. /****** FUNCTION DEFINITIONS *****/
  51. void onButton1Pressed() {
  52.   Serial.println("Button1 pressed"); // Callback function for button press
  53. }
  54.  
  55. void onTouchButtonPressed() {
  56.   Serial.println("Touch button pressed"); // Callback function for touch button press
  57. }
  58.  
  59. void onVirtualButtonPressed() {
  60.   Serial.println("Virtual button pressed"); // Callback function for virtual button press
  61. }
  62.  
  63. void setup(void)
  64. {
  65.     // Start serial communication for debugging
  66.     Serial.begin(115200);
  67.    
  68.     // Print a message to the serial monitor
  69.     Serial.println();
  70.     Serial.println(">>> EasyButton example <<<");
  71.  
  72.     // Initialize the buttons
  73.     button1.begin();
  74.     touchButton.begin(); // Initialize touch button
  75.     virtualButton.begin(); // Initialize virtual button
  76.  
  77.     button1.onPressed(onButton1Pressed); // Set the callback for button press
  78.     touchButton.onPressed(onTouchButtonPressed); // Set the callback for touch button press
  79.     virtualButton.onPressed(onVirtualButtonPressed); // Set the callback for virtual button press
  80.  
  81.     // Set the pin mode for the button (not necessary for EasyButton, but kept for reference)
  82.     pinMode(elkewk_PushButton_PIN_D2, INPUT_PULLUP);
  83.     pinMode(elkewk_TouchButton_PIN_D3, INPUT_PULLUP); // Set touch button pin mode
  84.     pinMode(elkewk_VirtualButton_PIN_D4, INPUT_PULLUP); // Set virtual button pin mode
  85. }
  86.  
  87. void loop(void)
  88. {
  89.     // Read the button states
  90.     button1.read(); // This checks the button state and triggers the callback if pressed
  91.     touchButton._readPin(); // Read the touch button state
  92.     virtualButton.read(); // Read the virtual button state
  93. }
  94.  
  95. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement