Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Arduino Debounce
- - Source Code compiled for: Arduino Uno
- - Source Code created on: 2025-10-06 21:30:58
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* give example of button. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /*
- Button example on Arduino UNO:
- - Button connected between pin 2 and GND
- - Internal pull-up resistor enabled via INPUT_PULLUP
- - LED on built-in pin 13 toggles on each press
- - Debouncing implemented for reliable presses
- */
- // Pin assignments
- const int BUTTON_PIN = 2;
- const int LED_PIN = 13;
- // Debounce state
- int lastButtonState = HIGH;
- unsigned long lastDebounceTime = 0;
- const unsigned long DEBOUNCE_DELAY = 50; // milliseconds
- // LED state
- int ledState = LOW;
- void setup(void)
- {
- // Initialize serial for debugging (optional)
- Serial.begin(9600);
- // Initialize pins
- pinMode(BUTTON_PIN, INPUT_PULLUP);
- pinMode(LED_PIN, OUTPUT);
- digitalWrite(LED_PIN, ledState);
- Serial.println("Button example started. Press the button to toggle LED.");
- }
- void loop(void)
- {
- int reading = digitalRead(BUTTON_PIN);
- // If button state changed, reset debounce timer
- if (reading != lastButtonState) {
- lastDebounceTime = millis();
- }
- // See if enough time has passed to debounce
- if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY) {
- if (reading != lastButtonState) {
- lastButtonState = reading;
- // Button pressed (active LOW)
- if (reading == LOW) {
- ledState = !ledState;
- digitalWrite(LED_PIN, ledState ? HIGH : LOW);
- Serial.print("Button pressed. LED is now ");
- Serial.println(ledState ? "ON" : "OFF");
- }
- }
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment