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 &&.
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.
WorksheetOpen in PrintDownload Print the student copy; the Teacher edition has the answers.
Students will be able to
I can declare an int and store a reading in it.
I can write an if and else to decide.
I can fix a missing ; and an = that should be a comparison.
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
Predict: ask which kind of thing level holds.
Find the mistake: ask what value level has after line 9.
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 see
A question to ask
The 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
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.
Up: Upload and run, then turn the knob up until the LED comes on.
Above 500 the if is true: digitalWrite(13, HIGH).
Down: Now turn it back down.
500 or less: the else runs and switches it off. Exactly 500 is not greater than 500.
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.
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, >).
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.
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.
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.
Which is true?: level is 300. Which condition is true? level >= 300 >= includes the value itself.
Between: if (level > 300 && level < 700) means… level is between 300 and 700 && means and: both must be true.
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.
knob at zero: Committing to level's value and the LED with the knob at 0 checks students can read a variable, analogRead and an if together before running.
Try: turn the knob: Turning the knob across 500 settles the prediction and shows the if switching both ways.
Worked example: two slips: Reading the sketch line by line models the move; the two slips (a missing ; and = for ==) are planted after the method, with the symptom each really gives.
Vary: change one thing: Changing the comparison, then fixing each slip on the board, gives students the compiler's message and the logic symptom to recognise.
Build it in Tinkercad: Tinkercad's real compiler gives its own error for a missing ; and its own behaviour for =, so students meet both in a second tool.
conditions: Reading conditions with ==, >= and && and predicting the LED.
a safe level light: Combining two comparisons with && to light the LED only in a band, and meeting a brief whose tests sit on both edges, applies the lesson to a new condition and makes students decide what happens at exactly 300 and 700.
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 { }.