← Teacher guide

Values and decisions

Students read a sketch that stores a knob's reading in an int and switches an LED with if and else, test it across the threshold, meet the two classic slips (a missing ; that stops the upload, and = in an if that makes it always true), fix both, and light the LED in a band with &&.

Arduino · Writing C++ · about 45 minutes · Open the student lesson

Classroom resources

Open these in Waypoints Prepare with one click. To share a file another way, copy its address and paste it into Load URL, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Predict and Try 6 min · Worked example 10 min · Vary 12 min · Tinkercad 8 min · Practise and Make 9 min

Before class

The board stops the upload for a missing ; (naming the line) like the real compiler, and runs = in an if as an assignment, as C++ does (the real compiler gives at most a warning). An undeclared name is an error too.

The micro:bit night light (Circuits › Sensors that vary) is the same decision.

Running the lesson

  1. Predict: ask which kind of thing level holds.
  2. Find the mistake: ask what value level has after line 9.
  3. Vary, won't upload: insist they read the message before looking.

The hard part (on purpose)

The two slips are invisible at a glance. Teach students to read the error's line number first, and to say '= sets, == compares' aloud.

When students get stuck

What you will seeA question to askThe fix
A student's LED is always on.What does a single = do?Use > or ==.
A student can't find the ; slip.Which line does the message name? Look at the end of it.Add the ;

Answers to the checks

  1. knob at zero: The knob is turned all the way down. After level = analogRead(A0); what is level, and is the LED on?
    level is 0 (analogRead gives 0 to 1023), 0 is not greater than 500, so the else switches the LED off.
  2. Up: Upload and run, then turn the knob up until the LED comes on.

    Above 500 the if is true: digitalWrite(13, HIGH).
  3. Down: Now turn it back down.

    500 or less: the else runs and switches it off. Exactly 500 is not greater than 500.
  4. Read it: The knob reads 500 exactly. Is the LED on?
    No: 500 is not greater than 500
    level > 500 is false for 500, so the else runs.
  5. Find the mistake: Sam's LED stays on whatever the knob does. Click the line where Sam's sketch goes wrong.
    line 9
    Line 9: level = 500 sets level to 500 and the if uses that value, which isn't 0, so it is always true. Comparing needs == (or here, >).
  6. Below 300: Change the sketch so the LED comes on when the knob is below 300 (a night light). Upload and test it.

    if (level < 300) puts the LED on for low readings.
  7. Won't upload: This sketch won't upload. Read the message, fix it and upload.

    A ; was missing after analogRead(A0). The message names the line.
  8. Always on: This one uploads, but the LED is always on. Fix it so it comes on above 500.

    level = 500 assigns. if (level > 500) compares.
  9. Which is true?: level is 300. Which condition is true?
    level >= 300
    >= includes the value itself.
  10. Between: if (level > 300 && level < 700) means…
    level is between 300 and 700
    && means and: both must be true.
  11. a safe level light: Write the decision so all 6 tests pass: the light is on only while the level is above 300 and below 700.

    if (level > 300 && level < 700) lights it from 301 to 699 and leaves it off at both edges and beyond.

Solutions

One possible solution for each task. Other correct programs are fine.

Vary: change one thing › Below 300

Open task: any program that does what the step asks.

Vary: change one thing › Won't upload

Open task: any program that does what the step asks.

Vary: change one thing › Always on

Open task: any program that does what the step asks.

a safe level light

Open task: any program that does what the step asks.

Stretch and support

Stretch: Use two LEDs (pins 13 and 12): one for below 300, one for above 700.

Support: A card: '= sets, == compares, every statement ends with ;'.

Exit ticket

Write an if that switches pin 13 on when level is 800 or more, and off otherwise.

if (level >= 800) { digitalWrite(13, HIGH); } else { digitalWrite(13, LOW); }

What each step is for

Where it leads

Next: Talking to the computer (print values to see what the board sees).

Python: the same if and else; Python uses : and indentation where C++ uses { }.

Sources