Skip to the page
Year 9 · Summer 1 · Physical Computing

Build, test and refine

What this lesson covers

Topic 13 Programming basics

KS3 Computing · A program stores information in variables, works on it with operators, and controls the order with sequence, selection and iteration. Examples are in Python, with Scratch alongside for Year 7.

Programming Computational ThinkingCambridge Lower Secondary Computing 0860

You will learn to

  • Create the device in stages, testing each stage before adding the next.
  • Write a test table with normal, boundary and erroneous tests for a device.
  • Find one fault from testing, fix it, and run all the tests again.

Success criteria

  • I can show each stage working before I add the next one.
  • I can run my test table and mark each test pass or fail.
  • I can explain one fix I made and show the test that now passes.

Key words

Say each word, then read what it means. Every one is in the glossary too.

Variable
A named place in memory that holds a value.
Assignment
Storing a value in a variable. = in Python, ← in pseudocode.
Casting
Changing a value from one data type to another, like int("13").
Concatenation
Joining strings together: "7" + "7" = "77".
Selection
Choosing a path with if / else.
Iteration
Repeating steps: a loop.
Boolean
A value that is only True or False.
Infinite loop
A loop whose condition never becomes false.

The explanation

Read it here, or open the study page below to go one step at a time with things to try.

Build in stages

Build a little, test it, then build a little more. Follow the plan from last lesson.

  1. Wire one LED with its resistor. Test it with Blink.
  2. Add the other two LEDs. Test that setLights(0, 0, 1) shows green only.
  3. Add the button and its pull-down. Print digitalRead(BUTTON) and press it: you should see 0, then 1.
  4. Add the full sequence. Test the whole crossing.

The Serial Monitor is a box under the code. It shows what Serial.println prints. It is your window into the program.

★Remember

If a stage fails, the fault is almost always in the last thing you added.

A test table for a device

Plan the tests before you run them. Write what should happen first.

TestTypeWhat to doExpected
1NormalPress the button once while the lights are greenAmber 2 s, red 5 s, red and amber 1 s, then green
2BoundaryGive the button the quickest tap you canThe sequence still starts
3BoundaryPress the button again while the lights are redNothing extra happens
4ErroneousHold the button down and do not let goOne full sequence, then cars get a green light

For a device, boundary tests try the edges of normal use: the shortest press, or a press at an awkward moment.

Erroneous tests try misuse: holding a button, pressing it again and again, or doing the unexpected.

Fix one thing

Test 4 fails. The lights go green, then loop() starts again at once. The held button is read straight away.

So the sequence starts again. Cars get green for a split second only.

    setLights(1, 1, 0);   // red and amber together: get ready to go
    delay(1000);
    setLights(0, 0, 1);   // back to green
    delay(5000);          // NEW: cars always get at least 5 seconds of green
  }
}
  1. Change one thing: add delay(5000) after the lights go green.
  2. Run test 4 again. Cars now get 5 seconds of green every time.
  3. Run tests 1 to 3 again. A fix in one place can break another.
★Remember

Record the fail, the fix and the new pass in your test table. That is your evidence.

e.g.Worked example

You press the crossing button and nothing happens. How do you find the fault?

  1. Predict: pressing the button should turn the amber light on.
  2. Check the input first. Add Serial.println(digitalRead(BUTTON)); at the top of loop().
  3. Run it and open the Serial Monitor. Not pressed should show 0. Pressed should show 1.
  4. Always 0? The fault is in the wiring. Check that the button's diagonal leg goes to pin 2.
  5. Jumps between 0 and 1 when nobody presses it? The 10 kΩ pull-down is missing.
  6. Shows 1, but still no amber? The fault is in the code. Check the if uses == and HIGH.
  7. Fix one thing, then run every test again.

Study it

Open the study page and go one step at a time.

Build it

Check yourself

One question at a time. The reason shows after each answer.

Saved on this device.