Advertisement
pleasedontcode

Button Setup rev_01

Dec 24th, 2023
88
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 Setup
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2023-12-24 12:53:21
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Turn Fan on and off from phone */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /****** DEFINITION OF LIBRARIES *****/
  24. #include <Arduino.h>
  25. #include <EasyButton.h>
  26.  
  27. /****** FUNCTION PROTOTYPES *****/
  28. void setup(void);
  29. void loop(void);
  30.  
  31. /***** DEFINITION OF PIN ASSIGNMENTS *****/
  32. const uint8_t BUTTON_PIN = 2;
  33.  
  34. /****** DEFINITION OF LIBRARY CLASS INSTANCES *****/
  35. EasyButton button(BUTTON_PIN);
  36.  
  37. // Button pressed callback function
  38. void buttonPressed()
  39. {
  40.   Serial.println("Button pressed");
  41.   // Implement code here to turn the fan on or off
  42. }
  43.  
  44. // Double-click callback function
  45. void sequenceElapsed()
  46. {
  47.   Serial.println("Double click");
  48. }
  49.  
  50. void setup(void)
  51. {
  52.   // Initialize Serial for debugging purposes.
  53.   Serial.begin(115200);
  54.  
  55.   Serial.println();
  56.   Serial.println(">>> EasyButton interrupts example <<<");
  57.  
  58.   // Initialize the button.
  59.   button.begin();
  60.  
  61.   // Set up button pressed and double-click handlers
  62.   button.onPressed(buttonPressed);
  63.   button.onSequence(2, 1500, sequenceElapsed);
  64.  
  65.   // Check if the button pin supports interrupts
  66.   if (button.supportsInterrupt())
  67.   {
  68.     // Enable interrupt for the button
  69.     button.enableInterrupt(buttonPressed);
  70.     Serial.println("Button will be used through interrupts");
  71.   }
  72.  
  73.   // Set up the button pin as INPUT_PULLUP
  74.   pinMode(BUTTON_PIN, INPUT_PULLUP);
  75. }
  76.  
  77. void loop(void)
  78. {
  79.   // Read the button state
  80.   button.read();
  81.   // Add your main code here to run repeatedly if needed
  82. }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement