A system reads a sensor, decides, and drives an output, and a brief says what it must do. Test it against a table of cases: each side of every threshold, the threshold itself, and the extremes. Tests that pass far from the threshold can hide a mistake right at it.
Your plant-water alarm works on your desk: it blinks when you twist the knob down. Does it meet the brief? How would you know, without watching it for a week?
The brief: a plant-water alarm. A soil moisture sensor on A0 reads low when the soil is dry (the knob stands in for it). The LED on pin 13 must blink when the reading is 300 or below, and be off otherwise.
A test case sets the sensor to one reading and says what the LED should do. Run the tests runs the sketch once for each case and ticks or crosses it.
Predict: Read the problem and decide what you think before you try anything. A wrong guess is fine: it shows you what to look for.
Try: Try it on the board and compare the result with your prediction.
Investigate: Work out why it happens, one step at a time.
Modify: Change the problem on purpose and see what happens.
Practise: A few on your own, to check you have it.
Make: Make your own problem using what you have learned. Expect a few tries; that is normal.
WidthMore contexts, same idea.
Make your prediction first. Help opens once you've locked it in.
PredictRead the problem and decide what you think before you try anything. A wrong guess is fine: it shows you what to look for.
Which test finds it?
TryTry it on the board and compare the result with your prediction.
Try: run the tests
Passing every test only means the sketch does what the tests ask.
Try it yourselfYour edits are saved in this browser.
Run the tests. How many pass? Then look at the readings tested.
Use the board. This ticks when you get there.
InvestigateWork out why it happens, one step at a time.
Worked example: choose the tests
Worked exampleBefore each step, say what you think comes next.
brief: blink at ≤300Find the threshold and which side it belongs to.
299,300→blinkJust below, and the threshold itself (the brief includes it).
301→offJust above.
0→blink,1023→offThe two ends of the range.
5 casesEach one could catch a different slip.
Now pick the best set.
Sam's alarm passed both tests, but stays off at a reading of 300. Click the line where Sam's plan first goes wrong.
ModifyChange the problem on purpose and see what happens.
Vary: change the brief
Try it yourselfYour edits are saved in this browser.
These tests include 300. Run them, then fix the sketch so all 5 pass.
Use the board. This ticks when you get there.
A new brief: steady on below 100 (very dry), blink from 100 to 299, off from 300. Change the sketch until all 6 tests pass.
Use the board. This ticks when you get there.
Modify TypicalChange the problem on purpose and see what happens.
Build it for real
A real sensor's reading flickers by a few numbers, so a reading near the threshold can make the alarm flicker too. The fix (a gap between switching on and off) is a good stretch.
PractiseA few on your own, to check you have it.
Tests
MakeMake your own problem using what you have learned. Expect a few tries; that is normal.
A fridge door alarm
Try it yourselfYour edits are saved in this browser.
Need a hint?
A light sensor in the fridge: bright means the door is open. Off below 400, steady on from 400 to 699, blink from 700.
Another hint
Test the biggest band first: if (level >= 700) { blink } else if (level >= 400) { on } else { off }
Another hint
Run the tests after each change, and read which case fails.
Write the decisions so all 6 tests pass: off below 400, steady on from 400 to 699, blink from 700 up.
Use the board. This ticks when you get there.
For teachers
Students run a plant-water alarm against a test table that passes but misses the threshold, learn to choose test cases (either side of the threshold, the threshold itself, the ends), fix an off-by-one, add a steady state, build it on real hardware, and meet a three-band fridge alarm brief against six tests. About 50 minutes.
I can choose test cases for a brief, including the boundary.
I can fix a sketch until every test passes.
I can explain what passing tests does and doesn't show.
Before class
The board's Run the tests runs the sketch once per case with no waiting (3 s of board time each) and records what the first LED did: off (never on), on (came on once and stayed on) or blink (came on two or more times).
This lesson is the Years 9–10 test-case idea (AC9TDI10P06, validate against test cases) met in Years 8–9 hardware; Code's console bridge has the same table-of-inputs idea in Python.
Running the lesson
Try: ask whether 5 out of 5 means the alarm meets the brief.
Worked example: write the table live from the brief.
Vary, three states: ask which case to test first in the if, and why.
The hard part (on purpose)
Students treat 'it worked when I tried it' as done. Ask which reading they tried, and which reading the brief is about.
When students get stuck
A student's steady-on case fails as blink. Ask: Does your steady branch switch the LED off anywhere? Fix: Steady on: HIGH only, no LOW in that branch.
A student changes the tests' expectations in their head. Ask: What does the brief say for 300? Fix: The brief decides, not the sketch.
Stretch and support
Stretch: Stop the flicker near the threshold: switch on at 300 or below, and only switch off again above 320.
Support: A blank test table with columns reading, should do, did, ✓/✗.
Where it leads
Next (optional): the same project on a Raspberry Pi Pico. Then Power and motors.
which test finds it?: Committing to which reading exposes an off-by-one at the threshold tests the new move: the boundary is where the slips are.
Try: run the tests: Every test in this table passes, yet the sketch misses the brief: the table has no case at the threshold. That sets up the method.
Worked example: choose the tests: Building a test table from the brief models the move: one case each side of the threshold, the threshold itself, and the extremes. Sam's two far-away tests are planted after the method.
Vary: change the brief: Each task changes the brief and its tests; students change the sketch until every case passes, including a new steady state.
Build it for real: On real hardware the readings aren't neat: printing them and testing at the edges shows how a real sensor behaves.
tests: Choosing test cases for new briefs, including an inclusive and an exclusive threshold.
a fridge door alarm: Meeting a three-state brief against a full test table brings the unit together: read, decide in bands, act, test.
Now you can
Choose test cases for a brief, including the boundary.