Advertisement
pleasedontcode

BlinkingLED rev_01

Sep 17th, 2023
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 2.96 KB | Source Code | 0 0
  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: BlinkingLED
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2023-09-16 17:21:01
  15.     - Source Code generated by: Alessandro
  16.  
  17. ********* Pleasedontcode.com **********/
  18.  
  19. /****** DEFINITION OF LIBRARIES *****/
  20. #include <Arduino.h>
  21. #include <EasyButton.h>
  22.  
  23. /****** SYSTEM REQUIREMENT 1 *****/
  24.     /* If push button is pressed two times in 1 second, */
  25.     /* so blink the LED for 5 seconds with a period of */
  26.     /* 500 milliseconds. */
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31. void buttonPressed(void);
  32. void blinkLED(void);
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t myPushButton_PushButton_PIN_D2 = 2;
  36.  
  37. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  38. const uint8_t myLED_LED_PIN_D3 = 3;
  39.  
  40. // Button object
  41. EasyButton myButton(myPushButton_PushButton_PIN_D2);
  42.  
  43. // LED state
  44. bool ledState = false;
  45.  
  46. // Blink interval
  47. const uint32_t blinkInterval = 500;
  48.  
  49. // Blink duration
  50. const uint32_t blinkDuration = 5000;
  51.  
  52. // Button press counter
  53. uint8_t buttonPressCount = 0;
  54.  
  55. // Last button press time
  56. uint32_t lastButtonPressTime = 0;
  57.  
  58. void setup(void)
  59. {
  60.     // Set the push button pin as input with pull-up resistor enabled
  61.     pinMode(myPushButton_PushButton_PIN_D2, INPUT_PULLUP);
  62.  
  63.     // Set the LED pin as output
  64.     pinMode(myLED_LED_PIN_D3, OUTPUT);
  65.  
  66.     // Initialize the button object
  67.     myButton.begin();
  68.  
  69.     // Attach the buttonPressed function to the button's pressed event
  70.     myButton.onPressed(buttonPressed);
  71. }
  72.  
  73. void loop(void)
  74. {
  75.     // Update the button object
  76.     myButton.update();
  77. }
  78.  
  79. void buttonPressed(void)
  80. {
  81.     // Get the current time
  82.     uint32_t currentTime = millis();
  83.  
  84.     // Check if it's a new button press within the time limit
  85.     if (currentTime - lastButtonPressTime > 1000)
  86.     {
  87.         // Reset the button press count
  88.         buttonPressCount = 0;
  89.     }
  90.  
  91.     // Increment the button press count
  92.     buttonPressCount++;
  93.  
  94.     // Update the last button press time
  95.     lastButtonPressTime = currentTime;
  96.  
  97.     // Check if the button press count is 2
  98.     if (buttonPressCount == 2)
  99.     {
  100.         // Enable the LED blinking
  101.         blinkLED();
  102.     }
  103. }
  104.  
  105. void blinkLED(void)
  106. {
  107.     // Get the start time
  108.     uint32_t startTime = millis();
  109.  
  110.     // Blink the LED for the specified duration
  111.     while (millis() - startTime < blinkDuration)
  112.     {
  113.         // Toggle the LED state
  114.         ledState = !ledState;
  115.  
  116.         // Set the LED state
  117.         digitalWrite(myLED_LED_PIN_D3, ledState);
  118.  
  119.         // Delay for the blink interval
  120.         delay(blinkInterval);
  121.     }
  122.  
  123.     // Turn off the LED
  124.     digitalWrite(myLED_LED_PIN_D3, LOW);
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement