Button debouncing in software in Embedded C - Time & Space Complexity
When we write code to handle button presses, we want to know how long it takes as the input changes.
Here, we ask: how does the time to check and debounce a button grow with the number of checks?
Analyze the time complexity of the following code snippet.
#define DEBOUNCE_TIME 5
int debounceButton(int *buttonState, int (*readPin)(void)) {
int count = 0;
while (count < DEBOUNCE_TIME) {
if (readPin() == *buttonState) {
count++;
} else {
count = 0;
*buttonState = readPin();
}
}
return *buttonState;
}
This code checks a button state repeatedly until it reads the same value several times in a row to confirm the press.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
whileloop that reads the button state repeatedly. - How many times: Up to
DEBOUNCE_TIMEtimes in a row with the same reading.
Each time the button state changes, the code resets the count and starts over.
| Input Size (n) | Approx. Operations |
|---|---|
| 5 (DEBOUNCE_TIME) | 5 reads if stable |
| 10 (twice stable) | 10 reads if stable twice |
| 100 (many changes) | Up to 100 reads if bouncing |
Pattern observation: The time grows linearly with the number of reads needed to confirm stability.
Time Complexity: O(n)
This means the time to debounce grows directly with the number of button state checks performed.
[X] Wrong: "Debouncing always takes constant time regardless of input changes."
[OK] Correct: If the button bounces, the code resets counting and takes longer, so time depends on input stability.
Understanding how input affects code timing shows you can think about real hardware behavior and write reliable software.
"What if we changed the debounce count to depend on a variable input instead of a fixed constant? How would the time complexity change?"
Practice
button debouncing in software?Solution
Step 1: Understand button signal noise
Mechanical buttons often create multiple quick on/off signals when pressed, called noise or bouncing.Step 2: Purpose of debouncing
Debouncing waits for the signal to stabilize before confirming a press, ignoring these unwanted rapid signals.Final Answer:
To ignore rapid, unwanted signals caused by mechanical button noise -> Option AQuick Check:
Debouncing = Ignore noise [OK]
- Thinking debouncing speeds up detection
- Confusing debouncing with hardware voltage changes
- Assuming debouncing disables buttons
Solution
Step 1: Understand debounce delay
A typical debounce delay is around 20-50 milliseconds to wait for signal stabilization.Step 2: Analyze code options
if(button_pressed()) { delay_ms(50); if(button_pressed()) { /* valid press */ } } uses 50 ms delay, which is correct. if(button_pressed()) { delay_ms(0); if(button_pressed()) { /* valid press */ } } uses 0 ms (no delay), C uses negative delay (invalid), D uses 5000 ms (too long).Final Answer:
if(button_pressed()) { delay_ms(50); if(button_pressed()) { /* valid press */ } } -> Option BQuick Check:
Debounce delay ~50ms = if(button_pressed()) { delay_ms(50); if(button_pressed()) { /* valid press */ } } [OK]
- Using zero or negative delay
- Using too long delay causing slow response
- Not checking button state after delay
int debounce_button() {
if (read_button_pin() == 0) {
delay_ms(40);
if (read_button_pin() == 0) {
return 1;
}
}
return 0;
}
What will debounce_button() return if the button is pressed and held steadily?Solution
Step 1: Check initial button state
The function checks if the button pin reads 0 (pressed).Step 2: Wait and re-check
After 40 ms delay, it checks again. If still 0, it returns 1 indicating a valid press.Final Answer:
1 -> Option CQuick Check:
Stable press returns 1 [OK]
- Assuming immediate return without delay
- Confusing 0 and 1 as pressed/released
- Thinking function loops infinitely
if (read_button_pin() == 0) {
delay_ms(10);
if (read_button_pin() == 0) {
button_pressed_flag = 1;
}
}Solution
Step 1: Analyze debounce delay duration
10 ms delay is often too short to filter mechanical bouncing which can last up to 50 ms.Step 2: Effect of short delay
Short delay allows noise to pass, causing multiple press detections.Final Answer:
Delay of 10 ms is too short to filter bouncing -> Option AQuick Check:
Too short delay causes multiple presses [OK]
- Assuming logic inversion causes multiple presses
- Thinking delay after flag needed
- Confusing delay units (ms vs us)
Solution
Step 1: Understand debounce counting logic
To count presses without bounces, confirm stable press with delay, then count once.Step 2: Avoid multiple counts during one press
Waiting until release before next count prevents multiple increments from one press.Final Answer:
Check button state, wait 50ms, check again; if still pressed, increment count and wait until release -> Option DQuick Check:
Stable press + wait release = correct count [OK]
- Counting immediately without debounce
- Using too short delay causing multiple counts
- Counting on release only misses presses
Ready to prove it?
Pass the test and this pattern moves unknown
