0
0
Embedded Cprogramming~20 mins

Button debouncing in software in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Debounce Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
A0000011111
B0000001111
C0000011100
D0000000000
Attempts:
2 left
💡 Hint
Look for when the input stays the same for at least 3 times (debounce_counter >= 2 means 3 stable reads).
🧠 Conceptual
intermediate
1: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?
ATo speed up the button press detection for faster response
BTo ignore quick, unintended fluctuations caused by mechanical bounce
CTo reduce power consumption by delaying CPU cycles
DTo allow multiple button presses to be registered simultaneously
Attempts:
2 left
💡 Hint
Think about what happens physically when a button is pressed and released.
🔧 Debug
advanced
2: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;
}
APrints 00000 (never updates stable_state)
BPrints 11111 (stable_state always 1)
CPrints 01010 (flips every input)
DCauses infinite loop
Attempts:
2 left
💡 Hint
Check the condition that increments debounce_counter and how stable_state is updated.
📝 Syntax
advanced
1: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;
    }
}
AReplace if with while to loop until stable: while (inputs[i] == last_state) { debounce_counter++; }
BChange for loop to while loop without initialization
CRemove else block entirely to avoid reset
DAdd a semicolon after debounce_counter++: debounce_counter++;
Attempts:
2 left
💡 Hint
Look carefully at the line with debounce_counter++ and check for missing punctuation.
🚀 Application
expert
2: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;
}
A4
B2
C3
D5
Attempts:
2 left
💡 Hint
Count how many times stable_state changes to 1 after 2 stable reads.