Advertisement
pleasedontcode

Button LED rev_01

Apr 6th, 2024
59
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:49:46
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Brighten LED to full bright when myButton1 or */
  21.     /* myButton2 is pressed and kept pressed. Dim LED1 */
  22.     /* when myButton1  or myButton2 is pressed and kept */
  23.     /* pressed. Turn LED1 off when either button is */
  24.     /* pressed and un-pressed quickly */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <EasyButton.h>    //https://github.com/evert-arias/EasyButton
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33. void updateOutputs(void);
  34.  
  35. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  36. const uint8_t myButton1_PushButton_PIN_D2    = 2;
  37. const uint8_t myButton2_PushButton_PIN_D3    = 3;
  38.  
  39. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  40. const uint8_t LED1_LED_PIN_D4        = 4;
  41.  
  42. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  43. /***** used to store raw data *****/
  44. bool    LED1_LED_PIN_D4_rawData        = 0;
  45.  
  46. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  47. /***** used to store data after characteristic curve transformation *****/
  48. float    LED1_LED_PIN_D4_phyData        = 0.0;
  49.  
  50. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  51. EasyButton myButton1(myButton1_PushButton_PIN_D2);
  52. EasyButton myButton2(myButton2_PushButton_PIN_D3);
  53.  
  54. bool myButton1Pressed = false;
  55. bool myButton2Pressed = false;
  56.  
  57. void setup(void)
  58. {
  59.     // put your setup code here, to run once:
  60.     pinMode(myButton1_PushButton_PIN_D2,    INPUT_PULLUP);
  61.     pinMode(myButton2_PushButton_PIN_D3,    INPUT_PULLUP);
  62.  
  63.     pinMode(LED1_LED_PIN_D4,     OUTPUT);
  64.  
  65.     myButton1.begin();
  66.     myButton2.begin();
  67. }
  68.  
  69.  
  70. void loop(void)
  71. {
  72.     // put your main code here, to run repeatedly:
  73.    
  74.     myButton1.read();
  75.     myButton2.read();
  76.  
  77.     if (myButton1.isPressed()) {
  78.         myButton1Pressed = true;
  79.     }
  80.  
  81.     if (myButton2.isPressed()) {
  82.         myButton2Pressed = true;
  83.     }
  84.  
  85.     if (myButton1.isReleased() && myButton2.isReleased()) {
  86.         myButton1Pressed = false;
  87.         myButton2Pressed = false;
  88.     }
  89.  
  90.     updateOutputs(); // Refresh output data
  91. }
  92.  
  93. void updateOutputs()
  94. {
  95.     if (myButton1Pressed || myButton2Pressed) {
  96.         LED1_LED_PIN_D4_rawData = 1; // Set LED1 to full bright
  97.     } else {
  98.         LED1_LED_PIN_D4_rawData = 0; // Set LED1 to off
  99.     }
  100.  
  101.     digitalWrite(LED1_LED_PIN_D4, LED1_LED_PIN_D4_rawData);
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement