Advertisement
pleasedontcode

Button Blink rev_09

Apr 7th, 2024
64
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 Blink
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-04-07 11:37:14
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* When the EasyButton connected to pin D2 is */
  21.     /* pressed, the green LED connected to pin D4 shall */
  22.     /* blink precisely 10 times and then stop blinking, */
  23.     /* waiting for the EasyButton to be pressed again. */
  24. /****** SYSTEM REQUIREMENT 2 *****/
  25.     /* Αφού η πράσινη λυχνία LED στον ακροδέκτη D4 */
  26.     /* ολοκληρώσει να αναβοσβήνει, η κόκκινη λυχνία LED */
  27.     /* στον ακροδέκτη D3 θα πρέπει να παραμείνει αναμμένη */
  28.     /* για 10 δευτερόλεπτα και στη συνέχεια να σβήσει. */
  29. /****** END SYSTEM REQUIREMENTS *****/
  30.  
  31. /****** DEFINITION OF LIBRARIES *****/
  32. #include <EasyButton.h>
  33.  
  34. /****** SYSTEM REQUIREMENTS *****/
  35. const uint8_t BUTTON_PIN = 2;
  36. const uint8_t GREEN_LED_PIN = 4;
  37. const uint8_t RED_LED_PIN = 3;
  38. const int BLINK_COUNT = 10;
  39. const int BLINK_DURATION = 200;
  40.  
  41. // Instance of the button.
  42. EasyButton button(BUTTON_PIN);
  43.  
  44. // Variables to track the state of the system requirements.
  45. bool isGreenLedBlinking = false;
  46. bool isRedLedOn = false;
  47. int blinkCount = 0;
  48.  
  49. void setup()
  50. {
  51.   pinMode(BUTTON_PIN, INPUT_PULLUP);
  52.   pinMode(GREEN_LED_PIN, OUTPUT);
  53.   pinMode(RED_LED_PIN, OUTPUT);
  54.  
  55.   // Initialize the button.
  56.   button.begin();
  57.   button.onPressedFor(BLINK_DURATION * BLINK_COUNT, []() {
  58.     isGreenLedBlinking = false;
  59.     digitalWrite(GREEN_LED_PIN, LOW);
  60.     isRedLedOn = true;
  61.     digitalWrite(RED_LED_PIN, HIGH);
  62.     delay(10000);
  63.     isRedLedOn = false;
  64.     digitalWrite(RED_LED_PIN, LOW);
  65.   });
  66. }
  67.  
  68. void loop()
  69. {
  70.   // Continuously read the status of the button.
  71.   button.read();
  72.  
  73.   if (button.wasPressed())
  74.   {
  75.     if (!isGreenLedBlinking)
  76.     {
  77.       blinkCount = 0;
  78.       isGreenLedBlinking = true;
  79.       digitalWrite(GREEN_LED_PIN, HIGH);
  80.       delay(BLINK_DURATION);
  81.     }
  82.     else
  83.     {
  84.       blinkCount++;
  85.       if (blinkCount >= BLINK_COUNT)
  86.       {
  87.         isGreenLedBlinking = false;
  88.         digitalWrite(GREEN_LED_PIN, LOW);
  89.       }
  90.       else
  91.       {
  92.         delay(BLINK_DURATION);
  93.         digitalWrite(GREEN_LED_PIN, !digitalRead(GREEN_LED_PIN));
  94.       }
  95.     }
  96.   }
  97. }
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement