Advertisement
pleasedontcode

"Button Actions" rev_01

Jun 16th, 2024
598
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 Actions"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-06-17 00:16:49
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Implement a system using the EasyButton library to */
  21.     /* detect single and long presses on a push button */
  22.     /* connected to pin D2, triggering specific actions */
  23.     /* for each event. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <EasyButton.h>  //https://github.com/evert-arias/EasyButton
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32. void onPressed(void);
  33. void onPressedForDuration(void);
  34.  
  35. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  36. const uint8_t mybutton_PushButton_PIN_D2 = 2;
  37.  
  38. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  39. EasyButton button(mybutton_PushButton_PIN_D2);  // Initialize the EasyButton instance with the pin number
  40.  
  41. void setup(void)
  42. {
  43.   // Initialize serial communication
  44.   Serial.begin(9600);
  45.  
  46.   // Initialize the button
  47.   button.begin();
  48.  
  49.   // Add the callback function to be called when the button is pressed
  50.   button.onPressed(onPressed);
  51.  
  52.   // Add the callback function to be called when the button is pressed for at least the given time
  53.   button.onPressedFor(2000, onPressedForDuration);
  54. }
  55.  
  56. void loop(void)
  57. {
  58.   // Continuously read the status of the button
  59.   button.read();
  60. }
  61.  
  62. // Callback function to be called when the button is pressed
  63. void onPressed()
  64. {
  65.   // Code to be executed when the button is pressed
  66.   Serial.println("Button pressed");
  67. }
  68.  
  69. // Callback function to be called when the button is pressed for the given duration
  70. void onPressedForDuration()
  71. {
  72.   // Code to be executed when the button is pressed for the given duration
  73.   Serial.println("Button pressed for 2 seconds");
  74. }
  75.  
  76. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement