A variable is declared with its type, int level = 0;. analogRead(A0) gives 0 to 1023, and if (level > 500) { … } else { … } decides. == compares; a single = sets the variable, even inside an if. Every statement ends with ;, or the sketch won't upload.
The night light on the micro:bit decided with a threshold. Same idea, new words: what does it look like in C++, and which two tiny slips stop it working?
int level = 0; // a whole-number variable, starting at 0
void loop() {
level = analogRead(A0); // 0 to 1023, as on the micro:bit
if (level > 500) { // compare
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
delay(100);
}
int means a whole number. ><>=<===!= compare. The { } after if holds what to do when it is true; else { } when it isn't.
Predict: Read the problem and decide what you think before you try anything. A wrong guess is fine: it shows you what to look for.
Try: Try it on the board and compare the result with your prediction.
Investigate: Work out why it happens, one step at a time.
Modify: Change the problem on purpose and see what happens.
Practise: A few on your own, to check you have it.
Make: Make your own problem using what you have learned. Expect a few tries; that is normal.
WidthMore contexts, same idea.
Make your prediction first. Help opens once you've locked it in.
PredictRead the problem and decide what you think before you try anything. A wrong guess is fine: it shows you what to look for.
Knob at zero
TryTry it on the board and compare the result with your prediction.
Try: turn the knob
Try it yourselfYour edits are saved in this browser.
Upload and run, then turn the knob up until the LED comes on.
Use the board. This ticks when you get there.
Now turn it back down.
Use the board. This ticks when you get there.
InvestigateWork out why it happens, one step at a time.
Worked example: two slips
Worked exampleBefore each step, say what you think comes next.
int level = 0;A whole-number variable called level, starting at 0.
level = analogRead(A0);Store the knob's reading (0 to 1023) in level.
if (level > 500)Is it greater than 500? True or false.
true: digitalWrite(13, HIGH);The first { } runs: LED on.
false: digitalWrite(13, LOW);The else { } runs: LED off.
Now: what happens at exactly 500?
Sam's LED stays on whatever the knob does. Click the line where Sam's sketch goes wrong.
ModifyChange the problem on purpose and see what happens.
Vary: change one thing
Try it yourselfYour edits are saved in this browser.
Change the sketch so the LED comes on when the knob is below 300 (a night light). Upload and test it.
Use the board. This ticks when you get there.
This sketch won't upload. Read the message, fix it and upload.
Use the board. This ticks when you get there.
This one uploads, but the LED is always on. Fix it so it comes on above 500.
Use the board. This ticks when you get there.
Modify TypicalChange the problem on purpose and see what happens.
Build it in Tinkercad
PractiseA few on your own, to check you have it.
Conditions
MakeMake your own problem using what you have learned. Expect a few tries; that is normal.
A middle band
Try it yourselfYour edits are saved in this browser.
Need a hint?
Two conditions, both true: &&.
Another hint
if (level > 300 && level < 700)
Another hint
Test it: turn the knob through the whole range.
Change the sketch so the LED is on only while the knob reads between 300 and 700. Test it by turning the knob from one end to the other.
Use the board. This ticks when you get there.
For teachers
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 &&. About 45 minutes.
I can fix a missing ; and an = that should be a comparison.
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
A student's LED is always on. Ask: What does a single = do? Fix: Use > or ==.
A student can't find the ; slip. Ask: Which line does the message name? Look at the end of it. Fix: Add the ;
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 ;'.
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 { }.
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 middle band: Combining two comparisons with && to light the LED only in a band applies the lesson to a new condition.
Now you can
Declare an int and store a reading in it.
Write an if and else to decide.
Fix a missing ; and an = that should be a comparison.