ist/enlighterjs.min Arduino Button Debounce
Type Here to Get Search Results !

Arduino Button Debounce

DE bouncing is removing unwanted input noise from buttons, switches or other user input



 





Example Code


const int buttonPin = 2;            // the number of the pushbutton pin
const int ledPin = 13;               // the number of the LED pin

int ledState = HIGH;                // the current state of the output pin
int buttonState;                      // the current reading from the input pin
int lastButtonState = LOW;      // the previous reading from the input pin

unsigned long lastDebounceTime = 0;   // the last time the output pin was toggled
unsigned long debounceDelay = 50;     // the debounce time

void setup() {
   pinMode(buttonPin, INPUT);
   pinMode(ledPin, OUTPUT);
   digitalWrite(ledPin, ledState);               // set initial LED state
}

void loop() {
  int reading = digitalRead(buttonPin);     // read the state
  if (reading != lastButtonState) {           // switch changed
    lastDebounceTime = millis();              // reset the debouncing timer
  }

if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {             // if the button state has changed
      buttonState = reading;
      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }

  digitalWrite(ledPin, ledState);      // set the LED
  lastButtonState = reading;         // save the reading
}





Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.