Advertisement
pleasedontcode

Button Callbacks rev_02

Jun 16th, 2024
477
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 Pro Mini 3.3V
  14.     - Source Code created on: 2024-06-16 13:02:57
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Implement a system to detect button presses using */
  21.     /* EasyButton library. Utilize digital pins D2 and D3 */
  22.     /* as input pins for two push buttons. Ensure the */
  23.     /* system initializes pins in setup() and */
  24.     /* continuously checks button states in loop(). */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  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. void onButton1Pressed(void);
  35. void onButton2Pressed(void);
  36.  
  37. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  38. const uint8_t XXMX_PushButton_PIN_D2 = 2;
  39. const uint8_t XXMX_PushButton_PIN_D3 = 3;
  40.  
  41. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  42. EasyButton button1(XXMX_PushButton_PIN_D2);  // Initialize EasyButton object for button1
  43. EasyButton button2(XXMX_PushButton_PIN_D3);  // Initialize EasyButton object for button2
  44.  
  45. void onButton1Pressed() {
  46.   Serial.println("Button1 pressed");
  47. }
  48.  
  49. void onButton2Pressed() {
  50.   Serial.println("Button2 pressed");
  51. }
  52.  
  53. void setup(void) {
  54.   // Initialize serial communication
  55.   Serial.begin(115200);
  56.   Serial.println();
  57.   Serial.println(">>> EasyButton multiple buttons example <<<");
  58.  
  59.   // Initialize buttons
  60.   button1.begin();  // Initialize button1
  61.   button2.begin();  // Initialize button2
  62.  
  63.   // Attach callback functions for button presses
  64.   button1.onPressed(onButton1Pressed);  // Attach callback for button1
  65.   button2.onPressed(onButton2Pressed);  // Attach callback for button2
  66. }
  67.  
  68. void loop(void) {
  69.   // Continuously check the state of the buttons
  70.   button1.read();  // Read the state of button1
  71.   button2.read();  // Read the state of button2
  72. }
  73.  
  74. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement