Advertisement
pleasedontcode

Button Blink rev_07

Apr 7th, 2024
67
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:22:41
  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.     /* σβήσει. */
  30. /****** END SYSTEM REQUIREMENTS *****/
  31.  
  32. /****** DEFINITION OF LIBRARIES *****/
  33. #include <EasyButton.h> // https://github.com/evert-arias/EasyButton
  34.  
  35. /****** FUNCTION PROTOTYPES *****/
  36. void setup();
  37. void loop();
  38. void blinkGreenLED();
  39. void turnOffRedLED();
  40.  
  41. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  42. const uint8_t BUTTON_PIN = 2;
  43.  
  44. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  45. const uint8_t RED_LED_PIN = 3;
  46. const uint8_t GREEN_LED_PIN = 4;
  47.  
  48. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  49. EasyButton button(BUTTON_PIN); // Instance of the EasyButton class
  50.  
  51. // Flag to track if the button is pressed
  52. bool buttonPressed = false;
  53.  
  54. void setup()
  55. {
  56.   // Initialize the digital input and output pins
  57.   pinMode(BUTTON_PIN, INPUT_PULLUP);
  58.   pinMode(RED_LED_PIN, OUTPUT);
  59.   pinMode(GREEN_LED_PIN, OUTPUT);
  60.  
  61.   // Initialize the button object
  62.   button.begin();
  63.  
  64.   // Attach a callback function to be called when the button is pressed
  65.   button.onPressed(blinkGreenLED);
  66. }
  67.  
  68. void loop()
  69. {
  70.   // Continuously read the status of the button
  71.   button.read();
  72.  
  73.   // Check if the button is pressed
  74.   if (button.isPressed())
  75.   {
  76.     // Check if the button is not already pressed
  77.     if (!buttonPressed)
  78.     {
  79.       // Set the flag to indicate that the button is pressed
  80.       buttonPressed = true;
  81.  
  82.       // Blink the green LED precisely 10 times
  83.       for (int i = 0; i < 10; i++)
  84.       {
  85.         digitalWrite(GREEN_LED_PIN, HIGH);
  86.         delay(250);
  87.         digitalWrite(GREEN_LED_PIN, LOW);
  88.         delay(250);
  89.       }
  90.  
  91.       // Stop blinking the green LED
  92.       digitalWrite(GREEN_LED_PIN, LOW);
  93.  
  94.       // Turn on the red LED for 10 seconds
  95.       digitalWrite(RED_LED_PIN, HIGH);
  96.       delay(10000);
  97.       digitalWrite(RED_LED_PIN, LOW);
  98.  
  99.       // Reset the flag
  100.       buttonPressed = false;
  101.     }
  102.   }
  103. }
  104.  
  105. void blinkGreenLED()
  106. {
  107.   // Blink the green LED once when the button is pressed
  108.   digitalWrite(GREEN_LED_PIN, HIGH);
  109.   delay(250);
  110.   digitalWrite(GREEN_LED_PIN, LOW);
  111. }
  112.  
  113. void turnOffRedLED()
  114. {
  115.   // Turn off the red LED
  116.   digitalWrite(RED_LED_PIN, LOW);
  117. }
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement