Debounce State Machine in C

Medium C FSM Asked at Garmin
Solve this problem

Implement a software debouncer for a mechanical push button sampled at a fixed rate. The output must only change after the raw input has been stable for N consecutive samples.

Constraints

  • Signature: `int debounce_step(int raw)` returning the debounced level.
  • Stability threshold is 5 consecutive identical samples.
  • State is held in file-scope statics; no globals exposed in the header.
  • Initial debounced state is 0.
  • A glitch shorter than 5 samples must never reach the output.
Starter codeC
1#define STABLE_COUNT 52 3int debounce_step(int raw) {4    // TODO: return the debounced level5    return 0;6}