Advertisement
pleasedontcode

EasyButton Control rev_03

Apr 25th, 2024
54
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: EasyButton Control
  13.     - Source Code NOT compiled for: Arduino Nano
  14.     - Source Code created on: 2024-04-25 11:46:22
  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. Button is */
  24.     /* connected to pin 6, diode is connected to pin 7 */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28.  
  29. #include <EasyButton.h>
  30.  
  31. EasyButton button(6); // Button connected to pin 6
  32. const uint8_t LED_PIN = 7; // LED diode connected to pin 7
  33.  
  34. bool buttonPressed = false;
  35. unsigned long buttonPressTime = 0;
  36. unsigned long lastDeactivationTime = 0;
  37.  
  38. void setup()
  39. {
  40.   pinMode(LED_PIN, OUTPUT);
  41.  
  42.   button.begin();
  43.   button.onPressed(onButtonPressed);
  44. }
  45.  
  46. void loop()
  47. {
  48.   button.read();
  49.  
  50.   if (buttonPressed && millis() - buttonPressTime >= 50)
  51.   {
  52.     digitalWrite(LED_PIN, LOW);
  53.     buttonPressed = false;
  54.     lastDeactivationTime = millis();
  55.   }
  56.  
  57.   if (!buttonPressed && millis() - lastDeactivationTime >= 2000)
  58.   {
  59.     button.activate();
  60.   }
  61. }
  62.  
  63. void onButtonPressed()
  64. {
  65.   buttonPressTime = millis();
  66.   digitalWrite(LED_PIN, HIGH);
  67.   buttonPressed = true;
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement