Challenge - 5 Problems
Debounce Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of simple debounce logic
What is the output of this code snippet simulating a button press with software debounce?
Embedded C
#include <stdio.h> int main() { int button_state = 0; int last_state = 0; int debounce_counter = 0; int stable_state = 0; int inputs[10] = {0, 1, 1, 0, 1, 1, 1, 0, 0, 1}; for (int i = 0; i < 10; i++) { if (inputs[i] == last_state) { debounce_counter++; } else { debounce_counter = 0; } last_state = inputs[i]; if (debounce_counter >= 2) { stable_state = last_state; } printf("%d", stable_state); } return 0; }
Attempts:
2 left
💡 Hint
Look for when the input stays the same for at least 3 times (debounce_counter >= 2 means 3 stable reads).
✗ Incorrect
The debounce logic updates stable_state only after the input remains the same for 3 consecutive reads. The output prints stable_state each iteration, resulting in '0000011111'.
🧠 Conceptual
intermediate1:30remaining
Understanding debounce delay effect
In software button debouncing, what is the main purpose of introducing a delay or counter before accepting a button state change?
Attempts:
2 left
💡 Hint
Think about what happens physically when a button is pressed and released.
✗ Incorrect
Mechanical buttons often produce rapid on/off signals (bounces) when pressed. The delay or counter ensures the software only registers a stable press, ignoring these quick fluctuations.
🔧 Debug
advanced2:00remaining
Identify the bug in debounce implementation
What error does this debounce code cause when run?
Embedded C
#include <stdio.h> int main() { int last_state = 0; int debounce_counter = 0; int stable_state = 0; int inputs[5] = {1, 1, 0, 0, 1}; for (int i = 0; i < 5; i++) { if (inputs[i] != last_state) { debounce_counter++; } else { debounce_counter = 0; } last_state = inputs[i]; if (debounce_counter >= 2) { stable_state = last_state; } printf("%d", stable_state); } return 0; }
Attempts:
2 left
💡 Hint
Check the condition that increments debounce_counter and how stable_state is updated.
✗ Incorrect
The code increments debounce_counter only when input changes, but resets it when input stays the same. This logic never reaches debounce_counter >= 2 for stable input, so stable_state never updates from 0.
📝 Syntax
advanced1:30remaining
Syntax error in debounce code snippet
Which option contains the correct syntax to implement a debounce check inside a loop?
Embedded C
for (int i = 0; i < 10; i++) { if (inputs[i] == last_state) { debounce_counter++ } else { debounce_counter = 0; } }
Attempts:
2 left
💡 Hint
Look carefully at the line with debounce_counter++ and check for missing punctuation.
✗ Incorrect
In C, every statement must end with a semicolon. The missing semicolon after debounce_counter++ causes a syntax error.
🚀 Application
expert2:30remaining
Calculate number of stable button presses detected
Given this debounce code and input sequence, how many times does the stable_state change from 0 to 1?
Embedded C
#include <stdio.h> int main() { int last_state = 0; int debounce_counter = 0; int stable_state = 0; int count_presses = 0; int inputs[12] = {0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1}; for (int i = 0; i < 12; i++) { if (inputs[i] == last_state) { debounce_counter++; } else { debounce_counter = 0; } last_state = inputs[i]; if (debounce_counter >= 1 && stable_state != last_state) { stable_state = last_state; if (stable_state == 1) { count_presses++; } } } printf("%d", count_presses); return 0; }
Attempts:
2 left
💡 Hint
Count how many times stable_state changes to 1 after 2 stable reads.
✗ Incorrect
The stable_state changes to 1 three times after the input stays stable for 2 reads: at indices 2, 7, and 11. So count_presses is 3.