Advertisement
pleasedontcode

Button Toggle rev_01

Mar 16th, 2024
42
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 Nano
  14.     - Source Code created on: 2024-03-16 19:55:32
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The button should turned on and off on one click */
  21. /****** SYSTEM REQUIREMENT 2 *****/
  22.     /* The button should turned on and off on one click */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31. void button1Pressed();
  32. void button2Pressed();
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t Please_PushButton_PIN_D2 = 2;
  36. const uint8_t Please_PushButton_PIN_D3 = 3;
  37.  
  38. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  39. EasyButton button1(Please_PushButton_PIN_D2);
  40. EasyButton button2(Please_PushButton_PIN_D3);
  41.  
  42. bool button1State = false;
  43. bool button2State = false;
  44.  
  45. void setup(void)
  46. {
  47.   // put your setup code here, to run once:
  48.   pinMode(Please_PushButton_PIN_D2, INPUT_PULLUP);
  49.   pinMode(Please_PushButton_PIN_D3, INPUT_PULLUP);
  50.  
  51.   button1.begin();
  52.   button2.begin();
  53.  
  54.   button1.onPressed(button1Pressed);
  55.   button2.onPressed(button2Pressed);
  56. }
  57.  
  58. void loop(void)
  59. {
  60.   // put your main code here, to run repeatedly:
  61.   button1.read();
  62.   button2.read();
  63. }
  64.  
  65. void button1Pressed()
  66. {
  67.   button1State = !button1State;
  68.   if (button1State) {
  69.     // Turn on the button 1 functionality
  70.     Serial.println("Button 1 turned ON");
  71.     // Add your code to turn on the button 1 functionality here
  72.   } else {
  73.     // Turn off the button 1 functionality
  74.     Serial.println("Button 1 turned OFF");
  75.     // Add your code to turn off the button 1 functionality here
  76.   }
  77. }
  78.  
  79. void button2Pressed()
  80. {
  81.   button2State = !button2State;
  82.   if (button2State) {
  83.     // Turn on the button 2 functionality
  84.     Serial.println("Button 2 turned ON");
  85.     // Add your code to turn on the button 2 functionality here
  86.   } else {
  87.     // Turn off the button 2 functionality
  88.     Serial.println("Button 2 turned OFF");
  89.     // Add your code to turn off the button 2 functionality here
  90.   }
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement