Advertisement
pleasedontcode

Button Blink rev_06

Apr 7th, 2024
47
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:07:59
  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, που είναι συνδεδεμένη */
  26.     /* στον ακροδέκτη D4, σταματήσει να αναβοσβήνει, η */
  27.     /* κόκκινη λυχνία LED, που είναι συνδεδεμένη στην */
  28.     /* ακίδα D3, θα πρέπει να ανάβει για 15 δευτερόλεπτα */
  29.     /* και μετα να σβηνη */
  30. /****** END SYSTEM REQUIREMENTS *****/
  31.  
  32. /****** DEFINITION OF LIBRARIES *****/
  33. #include <EasyButton.h> // Include the EasyButton library
  34.  
  35. /****** FUNCTION PROTOTYPES *****/
  36. void setup(void);
  37. void loop(void);
  38. void blinkGreenLED(void);
  39. void turnOnRedLED(void);
  40. void turnOffRedLED(void);
  41.  
  42. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  43. const uint8_t buttonPin = 2;
  44.  
  45. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  46. const uint8_t redLEDPin = 3;
  47. const uint8_t greenLEDPin = 4;
  48.  
  49. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  50. bool isButtonPressed = false;
  51. bool isGreenLEDOn = false;
  52. bool isRedLEDOn = false;
  53.  
  54. /****** DEFINITION OF LIBRARY CLASS INSTANCES*****/
  55. EasyButton button(buttonPin); // Create an instance of the EasyButton class for the button
  56.  
  57. void setup(void)
  58. {
  59.   // put your setup code here, to run once:
  60.  
  61.   pinMode(buttonPin, INPUT_PULLUP);
  62.   pinMode(redLEDPin, OUTPUT);
  63.   pinMode(greenLEDPin, OUTPUT);
  64.  
  65.   button.begin(); // Initialize the button
  66.  
  67.   button.onPressed(blinkGreenLED); // Set the callback function for button pressed event
  68. }
  69.  
  70. void loop(void)
  71. {
  72.   // put your main code here, to run repeatedly:
  73.  
  74.   // Read the state of the button
  75.   button.read();
  76.  
  77.   // Check if the button is pressed
  78.   if (button.isPressed())
  79.   {
  80.     isButtonPressed = true;
  81.   }
  82.   else
  83.   {
  84.     isButtonPressed = false;
  85.   }
  86.  
  87.   // Check if the green LED is on
  88.   if (isGreenLEDOn)
  89.   {
  90.     if (isButtonPressed)
  91.     {
  92.       // Blink the green LED 10 times
  93.       for (int i = 0; i < 10; i++)
  94.       {
  95.         digitalWrite(greenLEDPin, HIGH);
  96.         delay(500);
  97.         digitalWrite(greenLEDPin, LOW);
  98.         delay(500);
  99.       }
  100.       isGreenLEDOn = false;
  101.     }
  102.   }
  103.   else
  104.   {
  105.     if (!isButtonPressed)
  106.     {
  107.       // Turn on the red LED for 15 seconds
  108.       turnOnRedLED();
  109.       delay(15000);
  110.       turnOffRedLED();
  111.     }
  112.   }
  113. }
  114.  
  115. void blinkGreenLED()
  116. {
  117.   // Callback function for button pressed event
  118.   // Perform any actions you want when the button is pressed
  119.   Serial.println("Button pressed");
  120.  
  121.   // Turn on the green LED
  122.   isGreenLEDOn = true;
  123. }
  124.  
  125. void turnOnRedLED()
  126. {
  127.   digitalWrite(redLEDPin, HIGH);
  128.   isRedLEDOn = true;
  129. }
  130.  
  131. void turnOffRedLED()
  132. {
  133.   digitalWrite(redLEDPin, LOW);
  134.   isRedLEDOn = false;
  135. }
  136.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement