← micro:bit: talk and play

Name a job

Name some blocks once as a function, then call it by name wherever you need it.

A recipe says 'make the sauce' and the sauce is written once, on another page. Programs name their jobs the same way.

When the same blocks are needed in several places, you can name them once as a function, then call it by name wherever you need it. Make one in Advanced › Functions › Make a Function. The call block then appears in the same place.

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
function flashHeart () {
    basic.showIcon(IconNames.Heart)
    basic.pause(200)
    basic.clearScreen()
    basic.pause(200)
}
input.onButtonPressed(Button.A, function () {
    flashHeart()
    flashHeart()
})

function flashHeart holds the blocks for one flash. They only run when something calls flashHeart.

You press A once. How many times does the heart flash?

RunRun it and compare the result with your prediction.

Run

Press A and count the flashes.

The program as blocks
Show as code
lesson.ts
function flashHeart () {
    basic.showIcon(IconNames.Heart)
    basic.pause(200)
    basic.clearScreen()
    basic.pause(200)
}
input.onButtonPressed(Button.A, function () {
    flashHeart()
    flashHeart()
})
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
function flashHeart () {
    basic.showIcon(IconNames.Heart)
    basic.pause(200)
    basic.clearScreen()
    basic.pause(200)
}
input.onButtonPressed(Button.A, function () {
    flashHeart()
    flashHeart()
})

Noah's program:

The program as blocks
Show as code
function flashHeart () {
    basic.showIcon(IconNames.Heart)
    basic.pause(200)
    basic.clearScreen()
    basic.pause(200)
}
input.onButtonPressed(Button.A, function () {
})

MakeCode can show every program in Python. Here is this one:

def flash_heart():
    basic.show_icon(IconNames.HEART)
    basic.pause(200)
    basic.clear_screen()
    basic.pause(200)
def on_button_pressed_a():
    flash_heart()
    flash_heart()
input.on_button_pressed(Button.A, on_button_pressed_a)

Look at what Python did with on button A pressed: the blocks inside became a function, and the micro:bit calls it on each press.

Noah made the flashHeart function. He presses A and nothing happens. Why?

Without a function, the flash blocks are copied into A and into shake. You change the heart to a diamond in A only. What goes wrong?

This is the same program in Python (press Python at the top of the editor). Match each Python line with what it does.

def flash_heart():
flash_heart()
def on_button_pressed_a():
input.on_button_pressed(Button.A, on_button_pressed_a)

ModifyChange the code on purpose and check the result.

Modify

Make B flash the heart three times. Use the function: do not copy the blocks.

Need a hint?

Drag in on button B pressed, then three call flashHeart blocks from Functions.

Make a function flashSad that flashes a sad face. Call it when the micro:bit is shaken.

Need a hint?

Make a Function called flashSad, with show icon sad face, pause, clear screen, pause.

Another hint

Then call it from on shake.

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

Make B flash the heart three times, using the function.

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

Make a second function, flashSad, and call it from a shake.

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

Sol's team picker, from the last course.

The program as blocks
Show as code
lesson.ts
let names = ["Ava", "Ben", "Chloe", "Dev", "Eve"]
input.onButtonPressed(Button.A, function () {
    basic.showString(names[randint(0, 3)])
})
Sol's team picker has five names, but picks from 0 to 3. Whose name never comes up?

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

Make

Make a stick-figure dance. Make two dance moves as functions (for example wave and jump), each with two or more pictures. Then choreograph: A and B each call a move, and a shake calls a routine of both.

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?

Make two functions, such as wave and jump. Each has two or more show leds pictures.

Another hint

A calls one, B calls the other, and shake calls both, one after the other.

A dance: two functions of your own, each a short animation, called from the buttons.

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

ModifyChange the code on purpose and check the result.

Change an alarm

This alarm beeps and flashes three times when A is pressed, using one function. Make the function's beep higher or lower, then make B sound a longer alarm (more calls).

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 name a job as a function and call it from several places, and see in Python that every event block is a function the micro:bit calls. About 45 minutes.

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

Students will be able to

Before class

A function is a named job: some blocks with a name, made once in Advanced › Functions. Making it does not run it. It runs when a call block calls it, once per call. One copy serves every call, so a change to the function reaches every place at once; copied blocks have to be changed in every copy. The Python button at the top of the editor shows the same program as text: every event block (on button A pressed) becomes a function, handed to the micro:bit to call. Students only read the Python. Functions with inputs (parameters) come later, in Secret messages.

Running the lesson

  1. Predict: some students expect one flash per press. Point at the two call blocks.
  2. Run: count together. Each call runs the whole function once.
  3. Investigate: Noah's mistake (making but never calling) and the two copies are this lesson's ideas. Then switch the editor to Python together before the match.
  4. Say it plainly: an event block is a function. You make it; the micro:bit calls it on each press.
  5. Modify: three flashes means three calls, not copied blocks.
  6. Mix it up: a random pick that misses an item, from the last course.
  7. Make: two dance moves as functions, then a routine. Let students perform their dance for the class.

The hard part (on purpose)

Making a function does nothing until it is called. When a press does nothing, ask which block calls the function.

When students get stuck

Stretch and support

Stretch: Make a third move and a routine of all three, called from A+B.

Support: Give students the two dance functions already made with one picture each, and ask them to add a second picture and the calls.

Where it leads

Next unit: radio. on radio received is one more event block, so one more function the micro:bit calls. Class idea: a class dance where each group writes one move as a function.

What each step is for

Now you can
Next lesson2. Send and receiveSend a number by radio, and react on another micro:bit when it arrives.