Consider this Arduino code that reads a button and debounces it. What will be printed to the serial monitor if the button is pressed and held steadily?
const int buttonPin = 2; int buttonState = 0; int lastButtonState = 0; unsigned long lastDebounceTime = 0; unsigned long debounceDelay = 50; void setup() { Serial.begin(9600); pinMode(buttonPin, INPUT); } void loop() { int reading = digitalRead(buttonPin); if (reading != lastButtonState) { lastDebounceTime = millis(); } if ((millis() - lastDebounceTime) > debounceDelay) { if (reading != buttonState) { buttonState = reading; if (buttonState == HIGH) { Serial.println("Button pressed"); } } } lastButtonState = reading; }
Think about how the debounce delay prevents multiple triggers while the button state is stable.
The code prints "Button pressed" only once when the button state changes from LOW to HIGH after the debounce delay. Holding the button steady does not cause repeated prints.
When you press a physical button, why do we need to debounce it in software?
Think about what happens inside a button when you press it physically.
Mechanical buttons do not switch cleanly. They rapidly open and close contacts for a few milliseconds causing multiple signals. Software debouncing filters these out.
What is the problem with this Arduino debounce code snippet?
const int buttonPin = 3; int buttonState = 0; int lastButtonState = 0; unsigned long lastDebounceTime = 0; unsigned long debounceDelay = 50; void setup() { pinMode(buttonPin, INPUT); Serial.begin(9600); } void loop() { int reading = digitalRead(buttonPin); if (reading == lastButtonState) { lastDebounceTime = millis(); } if ((millis() - lastDebounceTime) > debounceDelay) { if (reading != buttonState) { buttonState = reading; if (buttonState == HIGH) { Serial.println("Pressed"); } } } lastButtonState = reading; }
Check the condition that resets the debounce timer.
The debounce timer should reset when the reading changes, not when it stays the same. Resetting on same reading prevents stable detection.
Fix the syntax error in this line of Arduino debounce code:
buttonState = reading if (millis() - lastDebounceTime) > debounceDelay;
Remember the correct syntax for an if statement in C++.
The if statement must have parentheses around the condition and a statement or block after it. Assignment uses =, not ==.
Given this debounce code, how many times will the message "Pressed" print if the button is pressed and held steadily for 1 second?
const int buttonPin = 4;
int buttonState = LOW;
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void setup() {
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
Serial.println("Pressed");
}
}
}
lastButtonState = reading;
}Consider how the debounce logic only prints on state change after delay.
The code prints "Pressed" only once when the button state changes from LOW to HIGH after the debounce delay. Holding the button does not cause repeated prints.
