Advertisement
pleasedontcode

Button Blink rev_08

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