pleasedontcode

Debounced Button rev_02

Oct 6th, 2025
43
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: Debounced Button
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-10-06 21:29:38
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* give example of button. */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23.  
  24.  
  25.  
  26. /* START CODE */
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33.  
  34. /*
  35.   Button example on Arduino UNO:
  36.   - Button connected between pin 2 and GND
  37.   - Internal pull-up resistor enabled via INPUT_PULLUP
  38.   - LED on built-in pin 13 toggles on each press
  39.   - Debouncing implemented for reliable presses
  40. */
  41.  
  42. // Pin assignments
  43. const int BUTTON_PIN = 2;
  44. const int LED_PIN = 13;
  45.  
  46. // Debounce state
  47. int lastButtonState = HIGH;
  48. unsigned long lastDebounceTime = 0;
  49. const unsigned long DEBOUNCE_DELAY = 50; // milliseconds
  50.  
  51. // LED state
  52. int ledState = LOW;
  53.  
  54. void setup(void)
  55. {
  56.     // Initialize serial for debugging (optional)
  57.     Serial.begin(9600);
  58.     // Initialize pins
  59.     pinMode(BUTTON_PIN, INPUT_PULLUP);
  60.     pinMode(LED_PIN, OUTPUT);
  61.     digitalWrite(LED_PIN, ledState);
  62.     Serial.println("Button example started. Press the button to toggle LED.");
  63. }
  64.  
  65. void loop(void)
  66. {
  67.     int reading = digitalRead(BUTTON_PIN);
  68.  
  69.     // If button state changed, reset debounce timer
  70.     if (reading != lastButtonState) {
  71.         lastDebounceTime = millis();
  72.     }
  73.  
  74.     // See if enough time has passed to debounce
  75.     if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY) {
  76.         if (reading != lastButtonState) {
  77.             lastButtonState = reading;
  78.             // Button pressed (active LOW)
  79.             if (reading == LOW) {
  80.                 ledState = !ledState;
  81.                 digitalWrite(LED_PIN, ledState ? HIGH : LOW);
  82.                 Serial.print("Button pressed. LED is now ");
  83.                 Serial.println(ledState ? "ON" : "OFF");
  84.             }
  85.         }
  86.     }
  87. }
  88.  
  89. /* END CODE */
  90.  
Advertisement
Add Comment
Please, Sign In to add comment