Greenhouse: is the water arriving?
Judge each pump run and tell an empty tank from a broken sensor. A medium embedded C problem (Error handling), run and graded in the browser.
medium · Error handling · faults · diagnostics · product
Product: Smart greenhouse controller (Agriculture)
Requirement. A pump that runs dry burns out, and a controller that trusts a broken sensor floods the bed. After every pump run, judge it from the soil moisture before and after (in percent) and how long it ran:
- Sensor fault if either reading is outside 0 to 100, or the soil got more than 10 points drier while being watered (soil does not dry while you water it).
- Otherwise, tank empty if the run lasted the full 5000 ms or more and the soil rose by less than 3 points.
- Otherwise the run is OK: a short run that stopped early, or a full run that raised the soil.
Starter code
run_result_t check_run(int before, int after, uint32_t run_ms) {
if (after - before < 3) return RUN_TANK_EMPTY;
return RUN_OK;
}Example tests
A good run and a dry run
printf("%d %d\n", check_run(20, 35, 3000), check_run(20, 21, 5000));Expected output:
0 1A short top-up is not a fault
printf("%d\n", check_run(40, 41, 1200));Expected output:
0Learn it first
- Error handling and fault strategy · Error codes, assertions, safe states and keeping evidence of what went wrong.
More Error handling problems
- Parse a byte, report why it failed · Return a status code and write the result only when parsing succeeded.
- Retry only what can succeed · Repeat an operation while it reports a transient error, but stop on a fatal one.