0
0
Embedded Cprogramming~20 mins

Debouncing as a state machine 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 a simple debounce state machine

What is the output of this debounce state machine code when the input signal toggles rapidly?

Embedded C
#include <stdio.h>

#define DEBOUNCE_TIME 3

typedef enum {IDLE, DEBOUNCE} State;

int main() {
    int input[] = {0,1,0,1,1,1,1,0,0,0};
    int stable_output = 0;
    State state = IDLE;
    int counter = 0;

    for (int i = 0; i < 10; i++) {
        switch(state) {
            case IDLE:
                if (input[i] != stable_output) {
                    state = DEBOUNCE;
                    counter = 1;
                }
                break;
            case DEBOUNCE:
                if (input[i] == stable_output) {
                    state = IDLE;
                } else {
                    counter++;
                    if (counter >= DEBOUNCE_TIME) {
                        stable_output = input[i];
                        state = IDLE;
                    }
                }
                break;
        }
        printf("%d", stable_output);
    }
    return 0;
}
A0001111000
B0000000000
C0111111111
D0001111111
Attempts:
2 left
💡 Hint

Think about how the state machine waits for the input to be stable for 3 cycles before changing output.

🧠 Conceptual
intermediate
1:30remaining
Understanding debounce state transitions

In a debounce state machine, what triggers the transition from the DEBOUNCE state back to the IDLE state without changing the output?

AInput signal changes to a new value different from stable output
BOutput is forced to toggle regardless of input
CDebounce timer reaches zero
DInput signal matches the current stable output before debounce time is reached
Attempts:
2 left
💡 Hint

Think about what happens if the input bounces back to the stable value during debounce.

🔧 Debug
advanced
2:30remaining
Identify the bug in debounce state machine code

What is the bug in this debounce state machine code snippet?

Embedded C
typedef enum {IDLE, DEBOUNCE} State;

int stable_output = 0;
State state = IDLE;
int counter = 0;

void update(int input) {
    switch(state) {
        case IDLE:
            if (input != stable_output) {
                state = DEBOUNCE;
                counter = 0;
            }
            break;
        case DEBOUNCE:
            if (input == stable_output) {
                state = IDLE;
            } else {
                counter++;
                if (counter >= 3) {
                    stable_output = input;
                    state = IDLE;
                }
            }
            break;
    }
}
AThe condition 'counter > 3' should be 'counter >= 3' to debounce correctly
BThe stable_output should be updated before changing state to IDLE
CCounter should start at 1, not 0, to count the first differing input
DThe state should never return to IDLE once in DEBOUNCE
Attempts:
2 left
💡 Hint

Check the debounce time condition carefully.

📝 Syntax
advanced
1:30remaining
Syntax error in debounce state machine code

Which option contains the syntax error in this debounce state machine snippet?

Embedded C
typedef enum {IDLE, DEBOUNCE} State;

int stable_output = 0;
State state = IDLE;
int counter = 0;

void update(int input) {
    switch(state) {
        case IDLE:
            if (input != stable_output) {
                state = DEBOUNCE;
                counter = 1;
            }
            break;
        case DEBOUNCE:
            if (input == stable_output) {
                state = IDLE;
            } else {
                counter++;
                if (counter >= 3) {
                    stable_output = input;
                    state = IDLE;
                }
            }
            break;
    }
}
AIncorrect enum declaration syntax
BMissing braces around if statement in case DEBOUNCE
CMissing semicolon after 'state = DEBOUNCE' in case IDLE
DMissing return type for update function
Attempts:
2 left
💡 Hint

Look carefully at the lines inside the switch cases.

🚀 Application
expert
3:00remaining
Number of stable output changes in debounce state machine

Given this input sequence and debounce time of 2, how many times does the stable output change?

int input[] = {0,1,1,0,0,1,1,1,0,0};
int debounce_time = 2;
A5
B3
C4
D2
Attempts:
2 left
💡 Hint

Count how many times the input stays stable for 2 cycles before output changes.