pleasedontcode

LED Toggle rev_02

Oct 6th, 2025
124
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: LED Toggle
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-10-06 22:37:28
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* give me example led */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23.  
  24. /* START CODE */
  25.  
  26. /* BUTTON EXAMPLE: Toggle LED with push button on Arduino Uno
  27.  * Button on pin 2 (INPUT_PULLUP). LED on pin 13 (built-in).
  28.  * Debounced toggle on each press.
  29.  */
  30.  
  31. // Pin definitions
  32. const uint8_t BUTTON_PIN = 2; // Digital pin connected to the button (active LOW with INPUT_PULLUP)
  33. const uint8_t LED_PIN = 13;   // Built-in LED on the Uno
  34.  
  35. // Debounce logic
  36. bool lastButtonState = HIGH;       // Previous reading from the button
  37. bool ledState = false;             // Current LED state
  38. unsigned long lastDebounceTime = 0; // Timestamp of the last state change
  39. const unsigned long debounceDelay = 50; // Debounce delay in milliseconds
  40.  
  41. void setup(void)
  42. {
  43.     // Initialize button with internal pull-up and LED as output
  44.     pinMode(BUTTON_PIN, INPUT_PULLUP);
  45.     pinMode(LED_PIN, OUTPUT);
  46.     digitalWrite(LED_PIN, LOW);
  47.  
  48.     // Read initial button state to set baseline
  49.     lastButtonState = digitalRead(BUTTON_PIN);
  50. }
  51.  
  52. void loop(void)
  53. {
  54.     int currentReading = digitalRead(BUTTON_PIN);
  55.  
  56.     // If button state changed, reset the debouncing timer
  57.     if (currentReading != lastButtonState)
  58.     {
  59.         lastDebounceTime = millis();
  60.     }
  61.  
  62.     // If enough time has passed, consider the button state stabilized
  63.     if ((millis() - lastDebounceTime) > debounceDelay)
  64.     {
  65.         // If the reading changed since last stable state, act on it
  66.         if (currentReading != lastButtonState)
  67.         {
  68.             lastButtonState = currentReading;
  69.  
  70.             // Button is pressed when the reading is LOW (due to pull-up)
  71.             if (currentReading == LOW)
  72.             {
  73.                 ledState = !ledState; // Toggle LED state
  74.                 digitalWrite(LED_PIN, ledState ? HIGH : LOW);
  75.             }
  76.         }
  77.     }
  78. }
  79.  
  80. /* END CODE */
  81.  
Advertisement
Add Comment
Please, Sign In to add comment