Simvik

Thermostat with hysteresis

Decide a heater state with two thresholds so it does not chatter. An easy embedded C problem (Fixed-point and signal processing), run and graded in the browser.

easy · Fixed-point and signal processing · hysteresis · control

Switching a heater on and off at a single threshold makes the relay chatter when the temperature hovers there. Two thresholds fix that.

Given the current heater state, return the new one: turn on when temp is below on_below, turn off when it is above off_above, and otherwise keep the current state.

Starter code

bool thermostat(bool heating, int16_t temp, int16_t on_below, int16_t off_above) {
  // Between the two thresholds, nothing changes.
  return heating;
}

Example tests

Too cold

printf("%d\n", thermostat(false, 190, 200, 220));

Expected output:

1

Too warm

printf("%d\n", thermostat(true, 225, 200, 220));

Expected output:

0

Learn it first

More Fixed-point and signal processing problems