How it works
Software that must not fail is an expert course about the code that sits between a sensor and something that can hurt someone. Six of its seven lessons run on the gallery's water heater bench: a thermistor on A0, a RESET button on pin 2, a solid-state relay on pin 7 switching two 12 V lamps that stand in for the element, and HEATING, OK and FAULT lamps on pins 8, 9 and 10. The redundancy lesson moves to the delta-T monitor for its two DS18B20 thermometers on one wire.
Every lesson opens a working sketch. You run it, attack it (drag the temperature, break the sensor wire from the part's properties, type bad commands, hang the loop), then fix what gives. The checks read the serial monitor, so every line a lesson quotes is one the board really printed.
Safety first. Everything here is low voltage and lives in the simulator. A real heater on mains power needs a qualified electrician, certified parts, and a thermal cutout in the power path that works with no software at all.
The lessons
- Failure modes: a naive thermostat, an FMEA table ranked by risk priority number, a broken wire that reads as -273.1 C and heats forever, and a plausibility check that turns it into a sensor fault.
- Defensive code: setpoints over serial. A line buffer that cannot
overflow,
strtolwith an end pointer, a range check before the cast, and the 65586 that would have wrapped to a polite 50. - Watchdogs: the chip's real watchdog at 250 ms. A hang, a slow job that trips it, the reset cause in MCUSR, and a boot loop stopped by a counter in EEPROM.
- Asserts in production: a
SAFE_ASSERTthat never compiles away. On a broken invariant it cuts the heater, keeps the line number in EEPROM, resets, and stays latched until a person presses RESET. - Redundancy: two probes that must agree within 2 C, the hotter one trusted, probes found by ROM code, and a setpoint stored twice (once inverted) so a flipped bit in RAM is caught.
- Reviews: three planted bugs found with a checklist: a stray
semicolon, a 16-bit
intthat makes the dry-fire cutout impossible, and a wait loop with no upper bound.
The boss: a fail-safe heater
One controller with every layer: a sensor plausibility check, a latched
over-temperature trip that only a person can reset (and only once the tank
has cooled), a guarded setpoint, SAFE_ASSERT, defensive commands and the
watchdog, with a boot that stays off after a hang. You attack it four ways.
Three attacks hold, and the fourth, a shorted sensor, gets through until you
add the missing check.
What the Uno does differently
- The watchdog and the EEPROM are the ATmega328P's own, modeled register for register. EEPROM survives a watchdog reset and is erased at each fresh Run.
- An Uno's bootloader clears the watchdog's flag in MCUSR, so a watchdog reset reaches your sketch with no flag at all. The lessons read it that way.
- An
intis 16 bits on the Uno. Two of the course's bugs live in that fact. - The thermistor and the DS18B20s are set from their properties: drag the temperature, or set the thermistor's r25 to 1000000000000 for a broken wire and to 1 for a short.
Before and after
Firmware architecture builds the habits this course leans on: error codes, a superloop, and a first look at the watchdog. The FMEA, the reviews and the boss work just as well as a starting point for any device that switches power.