← Arduino

A small system

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.

WidthMore contexts, same idea.
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?

The sketch says if (level < 300). The brief says 300 or below. Which test reading would show the mistake?

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.
  1. brief: blink at ≤300\text{brief: blink at } \le 300Find the threshold and which side it belongs to.

Now pick the best set.

Which set of readings tests this brief best?

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

A fan must turn on when the temperature reading is above 600. Which three readings test the threshold?

Every test passes. What can you say?

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.

Open the teacher guide: answers, solutions, slides and a worksheet.

Students will be able to

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

  1. Try: ask whether 5 out of 5 means the alarm meets the brief.
  2. Worked example: write the table live from the brief.
  3. 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

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.

Sources

What each step is for

Now you can
Next lesson8. The same project on a PicoA Raspberry Pi Pico runs MicroPython. The ideas carry straight over (pins as outputs, reading a sensor, deciding, timing), but the words change, and so do two numbers: time.sleep takes seconds, and read_u16 gives 0 to 65535, not 0 to 1023.