← micro:bit Foundations

If

Make the micro:bit decide what to do, using a sensor and an if block.

Street lights switch on when it gets dark, and a fridge beeps when its door is left open. Each one asks a yes-or-no question about a number, again and again.

Last lesson the micro:bit read sensors and showed numbers. Now it will decide. An if block (in Logic) asks a yes-or-no question, such as is the light level less than 50? If the answer is yes (true), it runs the blocks inside. If no (false), it skips them.

PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

The program as blocks
Show as code
lesson.ts
basic.forever(function () {
    if (input.lightLevel() < 50) {
        basic.showLeds(`
            # # # # #
            # # # # #
            # # # # #
            # # # # #
            # # # # #
            `)
    }
})

This is a night light. < means "less than".

The light level starts high. You drag it down to 20, then back up to 200. What happens?

RunRun it and compare the result with your prediction.

Run

Drag the light slider down to dark, then back up to bright.

The program as blocks
Show as code
lesson.ts
basic.forever(function () {
    if (input.lightLevel() < 50) {
        basic.showLeds(`
            # # # # #
            # # # # #
            # # # # #
            # # # # #
            # # # # #
            `)
    }
})
Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.

InvestigateTrace how each line of code connects to what you see.

Investigate

The program as blocks
Show as code
lesson.ts
basic.forever(function () {
    if (input.lightLevel() < 50) {
        basic.showLeds(`
            # # # # #
            # # # # #
            # # # # #
            # # # # #
            # # # # #
            `)
    }
})

Look at what the program does when it is bright.

Add a second if so the lights go off when it is bright. The comparison block is in Logic: choose ≥ (greater than or equal to).

Need a hint?

clear screen is in Basic (click more...).

Another hint

The second question is the opposite one: light level ≥ 50.

Zoe's program:

The program as blocks
Show as code
basic.forever(function () {
    if (input.lightLevel() > 50) {
        basic.showLeds(`
            # # # # #
            # # # # #
            # # # # #
            # # # # #
            # # # # #
            `)
    }
    if (input.lightLevel() < 50) {
        basic.clearScreen()
    }
})
Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.
Why do the lights stay on when it gets bright again?

Add a second if that clears the screen when it is bright.

This ticks by itself when your blocks do what is asked.

Zoe's night light is on in the daytime and off at night. Which part is wrong?

ModifyChange the code on purpose and check the result.

Modify

Make the night light come on when the light level is below 100. Change both questions.

Need a hint?

Click 50 in each question and type 100.

Add a third if: when the temperature is above 30, show a sad face.

Need a hint?

Drag in a third if from Logic.

Another hint

temperature is in Input. Use > and 30.

Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.

Make the night light come on earlier: switch at 100 instead of 50.

This ticks by itself when your blocks do what is asked.

Add a warning: show a sad face when the temperature is above 30.

This ticks by itself when your blocks do what is asked.

InvestigateTrace how each line of code connects to what you see.

Mix it up

Thermometer 1 is above. Thermometer 2:

The program as blocks
Show as code
basic.forever(function () {
    basic.showNumber(input.temperature())
})
The program as blocks
Show as code
lesson.ts
basic.showNumber(input.temperature())
Thermometer 1 (above) reads the temperature in on start. Thermometer 2 (below) reads it in forever. You drag the temperature slider. Which one follows it?

MakeBuild something new using what you have learned. Expect a few tries; that is normal.

Make

Make a weather warning. Draw your own pictures:

Test it by dragging the temperature slider.

Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.
Need a hint?

You need two if blocks inside forever, one for cold and one for hot.

Another hint

Cold: temperature < 10. Hot: temperature > 30.

Two ifs: a picture when it is cold (below 10) and a different one when it is hot (above 30).

This ticks by itself when your blocks do what is asked.

ModifyChange the code on purpose and check the result.

Change a fridge alarm

A fridge should stay at 5 degrees or less. This alarm flashes a cross when it gets warmer. Make it also flash when the fridge is too cold (below 0), with a different picture.

Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.
For teachers

Students use an if block to make the micro:bit decide, and see that a false if does nothing, so turning something off needs its own question. About 45 minutes.

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

Students will be able to

Before class

An if block asks a yes-or-no question (a condition), such as light level < 50. When the answer is yes (true), it runs the blocks inside; when no (false), it skips them and does nothing. That second half surprises students: in the Predict, the lights come on in the dark and stay on, because nothing tells them to go off. The fix is a second if with the opposite question. The comparison block is in Logic: < less than, > greater than, ≥ greater than or equal to. Next lesson, else does the same job more neatly.

Running the lesson

  1. Predict: most students expect the lights to go off when it is bright. Let them be surprised.
  2. Run: drag dark, then bright. Ask: which block would turn the lights off? (None.)
  3. Investigate: the fix is a second if asking the opposite question. Zoe's night light is the classic mix-up of < and >: read it aloud as 'if the light is less than 50'.
  4. Modify: changing the switch point means changing both questions, or there is a gap where neither is true.
  5. Mix it up: sensors in on start against forever, from last lesson.
  6. Make: two ifs, two drawings. Leave the middle (10 to 30) showing whatever was last: that is fine for now.

The hard part (on purpose)

An if does nothing when false, so turning something off needs its own question. Ask them to read the question aloud for a dark room and a bright one.

When students get stuck

Stretch and support

Stretch: Make the night light dim gradually: one row lit when it is a bit dark, three rows darker, all five in the dark (three ifs).

Support: Give students the second if already in place with an empty question, and ask them to choose the comparison.

Where it leads

Next lesson: else, which turns two ifs into one block with no gap. Class ideas: a classroom 'too noisy' light with the sound level (micro:bit V2); a plant monitor on a windowsill with a real micro:bit.

What each step is for

Now you can
Next lesson7. If and elseUse one if … else block to choose between two things, so exactly one of them happens.