Year 9 · Summer 1

Physical Computing · Lesson 5 of 5

Build, test and refine

ProjectTopic 13 Programming basics

Year 9 · Physical Computing · Lesson 5 of 5

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.

Year 9 · Physical Computing · Lesson 5 of 5

Success criteria

You have got it when you can say:

  • 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.

Year 9 · Physical Computing · Lesson 5 of 5

Retrieval starter

Questions from before. Write your answers, then show them.

  1. Last lesson

    The Tube map leaves out real distances. That's…

    A Pattern recognitionB AbstractionC Decomposition

    B. Abstraction

  2. Last unit

    A logo must look sharp on a pen and on a billboard. Best stored as…

    A A vectorB A bitmap

    A. A vector

  3. Earlier this year

    A face-recognition model works badly on faces unlike its training photos. This is…

    A Bias from unrepresentative training dataB A syntax errorC Overflow

    A. Bias from unrepresentative training data

Year 9 · Physical Computing · Lesson 5 of 5

Key words (1 of 2)

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".

Year 9 · Physical Computing · Lesson 5 of 5

Key words (2 of 2)

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 · step 1 of 3 · part 1 of 2

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 explanation · step 1 of 3 · part 2 of 2

Build in stages (continued)

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.

The explanation · step 2 of 3 · part 1 of 2

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.

The explanation · step 2 of 3 · part 2 of 2

A test table for a device (continued)

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

The explanation · step 3 of 3 · part 1 of 2

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
  }
}

The explanation · step 3 of 3 · part 2 of 2

Fix one thing (continued)

  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.

Year 9 · Physical Computing · Lesson 5 of 5

Worked example (1 of 2)

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.

Year 9 · Physical Computing · Lesson 5 of 5

Worked example (2 of 2)

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

  1. Jumps between 0 and 1 when nobody presses it? The 10 kΩ pull-down is missing.
  2. Shows 1, but still no amber? The fault is in the code. Check the if uses == and HIGH.
  3. Fix one thing, then run every test again.

Year 9 · Physical Computing · Lesson 5 of 5

Quick quiz · 1 of 3

How many times does for i in range(2, 10, 2): run?

  1. A 8
  2. B 5
  3. C 4

C. 4

2, 4, 6, 8. The next would be 10, which is the stop, so it isn't used. 4 times.

Year 9 · Physical Computing · Lesson 5 of 5

Quick quiz · 2 of 3

score = 45. Code: if score >= 25: print("Bronze") elif score >= 40: print("Gold"). What prints?

  1. A Both
  2. B Gold
  3. C Bronze

C. Bronze

45 >= 25 is True, so the first branch runs and the rest is skipped. The order is wrong: a logic error.

Year 9 · Physical Computing · Lesson 5 of 5

Quick quiz · 3 of 3

A program runs with no message but says the average of 4 and 6 is 7. This is a…

  1. A Syntax error
  2. B Runtime error
  3. C Logic error

C. Logic error

It ran but gave the wrong answer: a logic error. (4 + 6 / 2 = 7; it needs brackets.)

Year 9 · Physical Computing · Lesson 5 of 5

Exit ticket

Answer on paper before you go. Then check.

  1. count = 5, then while count > 0: print(count). What happens?

    A Prints nothingB Prints 5 for everC Prints 5 4 3 2 1

    B. Prints 5 for ever Nothing changes count, so count > 0 stays True: an infinite loop.

  2. What does print("3" + "4") show?

    A 34B An errorC 7

    A. 34 They're strings, so + joins them: 34.

Year 9 · Physical Computing · Lesson 5 of 5

Homework and the challenge

Homework

Lab Tracker: mark the builds you have done; study page 9 to the quiz.

End-of-unit challenge

A live demo plus a code walkthrough of inputs, processing and outputs.