Bird
Raised Fist0
Embedded Cprogramming~20 mins

Debouncing as a state machine in Embedded C - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1.

What is the main purpose of using debouncing in embedded C button handling?

easy
A. To avoid false triggers caused by mechanical noise
B. To speed up button press detection
C. To reduce power consumption
D. To increase button press sensitivity

Solution

  1. Step 1: Understand mechanical noise in buttons

    Mechanical buttons can create multiple rapid on/off signals when pressed or released, called noise.
  2. Step 2: Purpose of debouncing

    Debouncing filters out these rapid changes to detect a single clean press or release.
  3. Final Answer:

    To avoid false triggers caused by mechanical noise -> Option A
  4. Quick Check:

    Debouncing = Avoid false triggers [OK]
Hint: Debouncing stops noisy signals from causing multiple presses [OK]
Common Mistakes:
  • Thinking debouncing speeds up detection
  • Confusing debouncing with power saving
  • Assuming it increases sensitivity
2.

Which of the following is the correct way to declare a state variable for a debounce state machine in embedded C?

?
easy
A. char state = 0;
B. int state = "IDLE";
C. enum ButtonState {IDLE, DEBOUNCE, PRESSED}; ButtonState state = IDLE;
D. bool state = false;

Solution

  1. Step 1: Identify proper state variable type

    Using an enum clearly names states and improves code readability.
  2. Step 2: Check syntax correctness

    enum ButtonState {IDLE, DEBOUNCE, PRESSED}; ButtonState state = IDLE; correctly declares an enum and initializes the state variable.
  3. Final Answer:

    enum ButtonState {IDLE, DEBOUNCE, PRESSED}; ButtonState state = IDLE; -> Option C
  4. Quick Check:

    Enum for states = enum ButtonState {IDLE, DEBOUNCE, PRESSED}; ButtonState state = IDLE; [OK]
Hint: Use enum for clear, readable state variables [OK]
Common Mistakes:
  • Assigning string to int variable
  • Using bool for multiple states
  • Using char without clear meaning
3.

Given this snippet of a debounce state machine, what will be the value of state after 3 calls if the button is pressed and held?

enum State {IDLE, DEBOUNCE, PRESSED};
State state = IDLE;
int timer = 0;

void update(bool buttonPressed) {
  switch(state) {
    case IDLE:
      if(buttonPressed) {
        state = DEBOUNCE;
        timer = 2;
      }
      break;
    case DEBOUNCE:
      if(timer > 0) timer--;
      else state = PRESSED;
      break;
    case PRESSED:
      if(!buttonPressed) state = IDLE;
      break;
  }
}

// Calls:
update(true);
update(true);
update(true);
medium
A. PRESSED
B. Undefined
C. IDLE
D. DEBOUNCE

Solution

  1. Step 1: Trace state changes through calls

    First call: state IDLE -> DEBOUNCE, timer=2. Second call: DEBOUNCE, timer=2>0, timer-- to 1, still DEBOUNCE. Third call: DEBOUNCE, timer=1>0, timer-- to 0, still DEBOUNCE (else not executed).
  2. Step 2: Confirm final state after 3 calls

    Button is still pressed, state=DEBOUNCE (timer=0 now, changes to PRESSED on next call).
  3. Final Answer:

    DEBOUNCE -> Option D
  4. Quick Check:

    Timer counts down, state changes only if timer==0 at start of DEBOUNCE [OK]
Hint: Count timer ticks before state changes to PRESSED [OK]
Common Mistakes:
  • Thinking PRESSED after exactly 3 calls
  • Not decrementing timer
  • Resetting state incorrectly
4.

Identify the bug in this debounce state machine code snippet:

enum State {IDLE, DEBOUNCE, PRESSED};
State state = IDLE;
int timer = 0;

void update(bool buttonPressed) {
  switch(state) {
    case IDLE:
      if(buttonPressed) {
        state = DEBOUNCE;
        timer = 3;
      }
      break;
    case DEBOUNCE:
      if(timer == 0) state = PRESSED;
      else timer--;
      break;
    case PRESSED:
      if(!buttonPressed) state = IDLE;
      break;
  }
}

What is the problem?

medium
A. State never changes from DEBOUNCE
B. Timer decrements after checking zero, causing one extra cycle
C. Timer is never initialized
D. Button press is ignored in PRESSED state

Solution

  1. Step 1: Analyze timer decrement logic

    The code checks if timer == 0 before decrementing, so timer reaches 0 but state changes only next call.
  2. Step 2: Identify effect on debounce timing

    This causes the debounce to last one extra cycle longer than intended.
  3. Final Answer:

    Timer decrements after checking zero, causing one extra cycle -> Option B
  4. Quick Check:

    Check timer before decrement causes delay [OK]
Hint: Decrement timer before zero check to fix timing [OK]
Common Mistakes:
  • Thinking state never changes
  • Ignoring timer initialization
  • Assuming button ignored in PRESSED
5.

You want to implement a debounce state machine that only confirms a button press if the button is held for at least 5 cycles. Which approach below correctly implements this behavior?

enum State {IDLE, DEBOUNCE, PRESSED};
State state = IDLE;
int timer = 0;

void update(bool buttonPressed) {
  switch(state) {
    case IDLE:
      if(buttonPressed) {
        state = DEBOUNCE;
        timer = 5;
      }
      break;
    case DEBOUNCE:
      if(buttonPressed) {
        if(timer > 0) timer--;
        else state = PRESSED;
      } else {
        state = IDLE;
      }
      break;
    case PRESSED:
      if(!buttonPressed) state = IDLE;
      break;
  }
}

What happens if the button is pressed for 3 cycles then released?

hard
A. State returns to IDLE without reaching PRESSED
B. State changes to PRESSED after 3 cycles
C. State stays in DEBOUNCE indefinitely
D. State immediately changes to PRESSED on press

Solution

  1. Step 1: Trace timer decrement with button held 3 cycles

    Timer starts at 5, decrements each cycle while buttonPressed is true, so after 3 cycles timer = 2.
  2. Step 2: Button released before timer reaches zero

    On release, state immediately returns to IDLE without reaching PRESSED.
  3. Final Answer:

    State returns to IDLE without reaching PRESSED -> Option A
  4. Quick Check:

    Hold less than 5 cycles resets state [OK]
Hint: Hold button full timer cycles to reach PRESSED [OK]
Common Mistakes:
  • Assuming PRESSED reached early
  • Thinking DEBOUNCE lasts indefinitely
  • Believing immediate PRESSED on press
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard