Advertisement
pleasedontcode

Button Detection rev_03

Jan 24th, 2024
100
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 compiled for: Arduino Uno
  14.     - Source Code created on: 2024-01-24 20:34:34
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* if button is pressed so print hello on serial */
  21.     /* monitor */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /****** DEFINITION OF LIBRARIES *****/
  25. #include <EasyButton.h>    // https://github.com/evert-arias/EasyButton
  26.  
  27. /****** FUNCTION PROTOTYPES *****/
  28. void setup(void);
  29. void loop(void);
  30.  
  31. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  32. const uint8_t button_PIN_D2 = 2;
  33.  
  34. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  35. EasyButton button(button_PIN_D2);
  36.  
  37.  
  38. bool was_oddly_pressed = false; // Variable to track the odd press state
  39.  
  40. void onPressed()
  41. {
  42.     Serial.println("Hello");
  43. }
  44.  
  45. void setup(void)
  46. {
  47.     // put your setup code here, to run once:
  48.     pinMode(button_PIN_D2, INPUT_PULLUP);
  49.  
  50.     button.begin();
  51.     button.onPressed(onPressed);
  52. }
  53.  
  54. void loop(void)
  55. {
  56.     // put your main code here, to run repeatedly:
  57.     button.read();
  58.  
  59.     if (button.isPressed())
  60.     {
  61.         was_oddly_pressed = !was_oddly_pressed;
  62.  
  63.         if (was_oddly_pressed)
  64.         {
  65.             Serial.println("Odd press");
  66.         }
  67.         else
  68.         {
  69.             Serial.println("Even press");
  70.         }
  71.     }
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement