pleasedontcode

Arduino Debounce rev_03

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