Advertisement
pleasedontcode

Button Handler rev_01

Sep 25th, 2024
77
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 Handler
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-09-25 21:25:13
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Implement a button control system using EasyButton */
  21.     /* library for two push buttons connected to digital */
  22.     /* pins D2 and D3, ensuring reliable input handling */
  23.     /* and responsiveness in the Arduino environment. */
  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.  
  33. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  34. const uint8_t Suman_PushButton_PIN_D2       = 2; // Pin for button 1
  35. const uint8_t Suman_PushButton_PIN_D3       = 3; // Pin for button 2
  36.  
  37. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  38. // Initialize EasyButton objects for each button
  39. EasyButton button1(Suman_PushButton_PIN_D2); // Instance for button connected to pin D2
  40. EasyButton button2(Suman_PushButton_PIN_D3); // Instance for button connected to pin D3
  41.  
  42. // Function prototypes for button press actions
  43. void onButton1Pressed(); // Function to be called when button 1 is pressed
  44. void onButton2Pressed(); // Function to be called when button 2 is pressed
  45.  
  46. void setup(void)
  47. {
  48.     // Start the serial communication
  49.     Serial.begin(115200);
  50.    
  51.     // Print a message to the serial monitor
  52.     Serial.println();
  53.     Serial.println(">>> EasyButton multiple buttons example <<<");
  54.  
  55.     // Initialize the buttons
  56.     button1.begin(); // Initialize button 1
  57.     button2.begin(); // Initialize button 2
  58.  
  59.     // Attach the onPressed event handlers for each button
  60.     button1.onPressed(onButton1Pressed); // Set the function to be called when button 1 is pressed
  61.     button2.onPressed(onButton2Pressed); // Set the function to be called when button 2 is pressed
  62. }
  63.  
  64. void loop(void)
  65. {
  66.     // Continuously read the state of the buttons
  67.     button1.read(); // Read the state of button 1
  68.     button2.read(); // Read the state of button 2
  69. }
  70.  
  71. // Function to handle button 1 press event
  72. void onButton1Pressed() {
  73.     Serial.println("Button 1 pressed"); // Print message to serial monitor
  74. }
  75.  
  76. // Function to handle button 2 press event
  77. void onButton2Pressed() {
  78.     Serial.println("Button 2 pressed"); // Print message to serial monitor
  79. }
  80.  
  81. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement