Advertisement
pleasedontcode

Button LED rev_04

Apr 25th, 2024
76
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 LED
  13.     - Source Code NOT compiled for: Arduino Nano
  14.     - Source Code created on: 2024-04-25 11:48:52
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Ensure LED diode turns on for 50ms after pressing */
  21.     /* the Button. It should turn off after 50ms, even if */
  22.     /* the button is still pressed. Allow reactivation */
  23.     /* only after a 2s delay. Button is connected to pin */
  24.     /* 6, diode is connected to pin 7. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28.  
  29. #include <Arduino.h>
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void updateOutputs(void);
  35.  
  36. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  37. const uint8_t Button_PIN = 6;
  38.  
  39. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  40. const uint8_t LED_PIN = 7;
  41.  
  42. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  43. bool LED_state = LOW;
  44. unsigned long lastDeactivationTime = 0;
  45.  
  46. void setup(void)
  47. {
  48.     // put your setup code here, to run once:
  49.     pinMode(Button_PIN, INPUT_PULLUP);
  50.     pinMode(LED_PIN, OUTPUT);
  51. }
  52.  
  53. void loop(void)
  54. {
  55.     // put your main code here, to run repeatedly:
  56.     if (digitalRead(Button_PIN) == LOW) {
  57.         // Button is pressed
  58.         digitalWrite(LED_PIN, HIGH); // Turn on LED
  59.         delay(50); // Keep LED on for 50ms
  60.         digitalWrite(LED_PIN, LOW); // Turn off LED
  61.         lastDeactivationTime = millis(); // Record the time of deactivation
  62.     }
  63.  
  64.     // Check if reactivation is allowed after 2s delay
  65.     if (millis() - lastDeactivationTime >= 2000) {
  66.         updateOutputs(); // Refresh output data
  67.     }
  68. }
  69.  
  70. void updateOutputs()
  71. {
  72.     // Update LED state based on the condition
  73.     if (digitalRead(Button_PIN) == HIGH) {
  74.         LED_state = LOW; // Reset LED state
  75.     }
  76.     digitalWrite(LED_PIN, LED_state);
  77. }
  78.  
  79. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement