Advertisement
pleasedontcode

Button Blink rev_10

Apr 7th, 2024
109
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:46:40
  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.  
  32. /********* User code review feedback **********
  33. #### Feedback 1 ####
  34. - ok. i want the green led to blink 100ms
  35. ********* User code review feedback **********/
  36.  
  37. /****** DEFINITION OF LIBRARIES *****/
  38. #include <EasyButton.h>
  39.  
  40. /****** SYSTEM REQUIREMENTS *****/
  41. const uint8_t BUTTON_PIN = 2;
  42. const uint8_t GREEN_LED_PIN = 4;
  43. const uint8_t RED_LED_PIN = 3;
  44. const int BLINK_COUNT = 10;
  45. const int BLINK_DURATION = 100;
  46.  
  47. // Instance of the button.
  48. EasyButton button(BUTTON_PIN);
  49.  
  50. // Variables to track the state of the system requirements.
  51. bool isGreenLedBlinking = false;
  52. bool isRedLedOn = false;
  53. int blinkCount = 0;
  54.  
  55. void setup()
  56. {
  57.   pinMode(BUTTON_PIN, INPUT_PULLUP);
  58.   pinMode(GREEN_LED_PIN, OUTPUT);
  59.   pinMode(RED_LED_PIN, OUTPUT);
  60.  
  61.   // Initialize the button.
  62.   button.begin();
  63.   button.onPressed([]() {
  64.     if (!isGreenLedBlinking)
  65.     {
  66.       blinkCount = 0;
  67.       isGreenLedBlinking = true;
  68.       digitalWrite(GREEN_LED_PIN, HIGH);
  69.     }
  70.     else
  71.     {
  72.       blinkCount++;
  73.       if (blinkCount >= BLINK_COUNT)
  74.       {
  75.         isGreenLedBlinking = false;
  76.         digitalWrite(GREEN_LED_PIN, LOW);
  77.       }
  78.       else
  79.       {
  80.         digitalWrite(GREEN_LED_PIN, !digitalRead(GREEN_LED_PIN));
  81.       }
  82.     }
  83.   });
  84. }
  85.  
  86. void loop()
  87. {
  88.   // Continuously read the status of the button.
  89.   button.read();
  90.  
  91.   if (isGreenLedBlinking)
  92.   {
  93.     delay(BLINK_DURATION);
  94.   }
  95.   else if (isRedLedOn)
  96.   {
  97.     delay(10000);
  98.     isRedLedOn = false;
  99.     digitalWrite(RED_LED_PIN, LOW);
  100.   }
  101. }
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement