← Arduino

Values and decisions

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.

WidthMore contexts, same idea.
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

The knob is turned all the way down. After level = analogRead(A0); what is level, and is the LED on?

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.
  1. int level = 0;\text{int level = 0;}A whole-number variable called level, starting at 0.

Now: what happens at exactly 500?

The knob reads 500 exactly. Is the LED on?

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

level is 300. Which condition is true?

if (level > 300 && level < 700) means…

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.

Open the teacher guide: answers, solutions, slides and a worksheet.

Students will be able to

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

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

Sources

What each step is for

Now you can
Next lesson4. Talking to the computerSerial.begin(9600) in setup() opens a line to the computer; Serial.println(level) sends a value and starts a new line, and Serial.print stays on the same line. The serial monitor shows what the board sees, which is how you test and debug a sketch. In C++, "words" + a number does not join them.