Advertisement
pleasedontcode

"Button Callbacks" rev_02

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