Advertisement
pleasedontcode

Button Detection rev_01

Sep 20th, 2024
66
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 Detection
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-09-20 14:35:15
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The system shall utilize two push buttons */
  21.     /* connected to digital pins D2 and D3, using the */
  22.     /* EasyButton library for handling button presses */
  23.     /* with pull-up resistors enabled. */
  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 Mybut_PushButton_PIN_D2       = 2; // Pin for button 1
  35. const uint8_t Mybut_PushButton_PIN_D3       = 3; // Pin for button 2
  36.  
  37. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  38. // Create EasyButton instances for each button with pull-up resistors enabled
  39. EasyButton button1(Mybut_PushButton_PIN_D2, 35, true); // Instance for button on pin D2
  40. EasyButton button2(Mybut_PushButton_PIN_D3, 35, true); // Instance for button on pin D3
  41.  
  42. // Function to be called when button1 is pressed
  43. void onButton1Pressed() {
  44.   Serial.println("Button1 pressed");
  45. }
  46.  
  47. // Function to be called when button2 is pressed
  48. void onButton2Pressed() {
  49.   Serial.println("Button2 pressed");
  50. }
  51.  
  52. void setup(void)
  53. {
  54.     // Initialize serial communication for debugging
  55.     Serial.begin(115200);
  56.  
  57.     // Print a welcome message
  58.     Serial.println();
  59.     Serial.println(">>> EasyButton example <<<");
  60.  
  61.     // Initialize the button instances
  62.     button1.begin(); // Initialize button1
  63.     button2.begin(); // Initialize button2
  64.  
  65.     // Set up the button press event handlers
  66.     button1.onPressed(onButton1Pressed); // Set the handler for button1
  67.     button2.onPressed(onButton2Pressed); // Set the handler for button2
  68.  
  69.     // No need to set pin modes for buttons since EasyButton handles it
  70. }
  71.  
  72. void loop(void)
  73. {
  74.     // Read the button states
  75.     button1.read(); // Read the state of button1
  76.     button2.read(); // Read the state of button2
  77. }
  78.  
  79. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement