Advertisement
pleasedontcode

Button LED rev_02

Apr 6th, 2024
48
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 LED
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-04-06 11:55:07
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Brighten or dim LED to full when myButton1 or */
  21.     /* myButton2 is pressed and kept pressed. Turn LED1 */
  22.     /* OFF or ON, opposite to existing state when either */
  23.     /* button is pressed momentarily */
  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. void updateOutputs(void);
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t myButton1_PushButton_PIN_D2 = 2;
  36. const uint8_t myButton2_PushButton_PIN_D3 = 3;
  37.  
  38. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  39. const uint8_t LED1_LED_PIN_D4 = 4;
  40.  
  41. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  42. bool LED1_LED_PIN_D4_rawData = 0;
  43.  
  44. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  45. float LED1_LED_PIN_D4_phyData = 0.0;
  46.  
  47. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  48. EasyButton myButton1(myButton1_PushButton_PIN_D2); // Create an instance of EasyButton for myButton1
  49. EasyButton myButton2(myButton2_PushButton_PIN_D3); // Create an instance of EasyButton for myButton2
  50.  
  51. void setup(void)
  52. {
  53.   // put your setup code here, to run once:
  54.  
  55.   pinMode(myButton1_PushButton_PIN_D2, INPUT_PULLUP);
  56.   pinMode(myButton2_PushButton_PIN_D3, INPUT_PULLUP);
  57.  
  58.   pinMode(LED1_LED_PIN_D4, OUTPUT);
  59.  
  60.   myButton1.onPressedFor(1000, []() {
  61.     // Callback function for button 1 press and hold for 1 second
  62.     Serial.println("Button 1 pressed and held for 1 second");
  63.     // Set the state of LED1 to full brightness
  64.     LED1_LED_PIN_D4_rawData = 1;
  65.   });
  66.  
  67.   myButton2.onPressedFor(1000, []() {
  68.     // Callback function for button 2 press and hold for 1 second
  69.     Serial.println("Button 2 pressed and held for 1 second");
  70.     // Set the state of LED1 to full brightness
  71.     LED1_LED_PIN_D4_rawData = 1;
  72.   });
  73.  
  74.   myButton1.onPressed([]() {
  75.     // Callback function for button 1 press and release
  76.     Serial.println("Button 1 pressed and released");
  77.     // Toggle the state of LED1
  78.     LED1_LED_PIN_D4_rawData = !LED1_LED_PIN_D4_rawData;
  79.   });
  80.  
  81.   myButton2.onPressed([]() {
  82.     // Callback function for button 2 press and release
  83.     Serial.println("Button 2 pressed and released");
  84.     // Toggle the state of LED1
  85.     LED1_LED_PIN_D4_rawData = !LED1_LED_PIN_D4_rawData;
  86.   });
  87.  
  88.   myButton1.begin();
  89.   myButton2.begin();
  90. }
  91.  
  92. void loop(void)
  93. {
  94.   // put your main code here, to run repeatedly:
  95.  
  96.   myButton1.read(); // Read the state of myButton1
  97.   myButton2.read(); // Read the state of myButton2
  98.  
  99.   updateOutputs(); // Refresh output data
  100. }
  101.  
  102. void updateOutputs()
  103. {
  104.   digitalWrite(LED1_LED_PIN_D4, LED1_LED_PIN_D4_rawData);
  105. }
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement