Advertisement
pleasedontcode

Button Toggle rev_01

Feb 16th, 2024
81
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 Toggle
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-02-16 07:12:48
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The system shall control the blinking of a bulb */
  21.     /* connected to pin D2 using a push button connected */
  22.     /* to pin D3. */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <EasyButton.h>
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31.  
  32. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  33. const uint8_t bulb_PIN_D2 = 2;
  34. const uint8_t pushButton_PIN_D3 = 3;
  35.  
  36. /****** DEFINITION OF LIBRARY CLASS INSTANCES*****/
  37. EasyButton pushButton(pushButton_PIN_D3);
  38.  
  39. void setup(void)
  40. {
  41.   // initialize the bulb pin as an output
  42.   pinMode(bulb_PIN_D2, OUTPUT);
  43.  
  44.   // initialize the push button pin as an input with the internal pull-up resistor enabled
  45.   pinMode(pushButton_PIN_D3, INPUT_PULLUP);
  46.  
  47.   // initialize the push button object
  48.   pushButton.begin();
  49. }
  50.  
  51. void loop(void)
  52. {
  53.   // read the state of the push button
  54.   pushButton.read();
  55.  
  56.   // check if the push button is pressed
  57.   if (pushButton.isPressed())
  58.   {
  59.     // toggle the state of the bulb
  60.     digitalWrite(bulb_PIN_D2, !digitalRead(bulb_PIN_D2));
  61.     delay(200); // add a small delay to debounce the button
  62.   }
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement