Advertisement
pleasedontcode

Button LED Control rev_01

Apr 25th, 2024
70
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 Control
  13.     - Source Code NOT compiled for: Arduino Nano
  14.     - Source Code created on: 2024-04-25 11:35:11
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Ensure LED diode turns on for 50ms after pressing */
  21.     /* the EasyButton. It should turn off after 50ms, */
  22.     /* even if the button is still pressed. Allow */
  23.     /* reactivation only after a 2s delay. */
  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 onPressedForDuration();
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t Przycisk_PushButton_PIN_D2 = 2;
  36.  
  37. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  38. const uint8_t Zawor_LED_PIN_D3 = 3;
  39.  
  40. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  41. /***** used to store raw data *****/
  42. bool Zawor_LED_PIN_D3_rawData = 0;
  43.  
  44. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  45. /***** used to store data after characteristic curve transformation *****/
  46. float Zawor_LED_PIN_D3_phyData = 0.0;
  47.  
  48. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  49. // Instance of the button.
  50. EasyButton button(Przycisk_PushButton_PIN_D2);
  51.  
  52. bool ledActive = false;
  53. unsigned long lastActivationTime = 0;
  54.  
  55. void setup(void)
  56. {
  57.     // put your setup code here, to run once:
  58.     pinMode(Przycisk_PushButton_PIN_D2, INPUT_PULLUP);
  59.     pinMode(Zawor_LED_PIN_D3, OUTPUT);
  60.  
  61.     // Initialize the button.
  62.     button.begin();
  63.     // Add the callback function to be called when the button is pressed for at least the given time.
  64.     button.onPressedFor(2000, onPressedForDuration);
  65. }
  66.  
  67. void loop(void)
  68. {
  69.     // put your main code here, to run repeatedly:
  70.     button.read(); // Continuously read the status of the button.
  71.     updateOutputs(); // Refresh output data
  72. }
  73.  
  74. void updateOutputs()
  75. {
  76.     if (ledActive && millis() - lastActivationTime >= 50) {
  77.         Zawor_LED_PIN_D3_rawData = 0;
  78.         ledActive = false;
  79.         lastActivationTime = millis();
  80.     }
  81.  
  82.     digitalWrite(Zawor_LED_PIN_D3, Zawor_LED_PIN_D3_rawData);
  83. }
  84.  
  85. void onPressedForDuration()
  86. {
  87.     if (millis() - lastActivationTime >= 2000) {
  88.         Zawor_LED_PIN_D3_rawData = 1;
  89.         ledActive = true;
  90.         lastActivationTime = millis();
  91.     }
  92. }
  93.  
  94. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement