← Teacher guide

Code with ML blocks

Students code with CreateAI's Machine Learning blocks (in the simulator with practice actions, then on their own model) to build an activity tracker.

micro:bit and the computer · AI on the micro:bit · about 45 minutes · Open the student lesson

Classroom resources

Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Predict and Run 8 min · Investigate 8 min · Modify 12 min · Make 12 min · Own model 5 min

Before class

The idea: the model estimates an action several times a second. on ML … start runs when the estimate changes to that action (once per stretch, not once per jump); on ML … stop runs when it changes away, with how long it lasted; unknown is the estimate when no action reaches its recognition point (CreateAI's word for the confidence threshold). is ML … detected and certainty are there for programs that check in forever.

Practice actions: a trained model only exists inside the project CreateAI makes, so this lesson's editor loads the Machine Learning blocks with six practice actions (from this site's own small extension) and the simulator's ML panel lets students choose the action. Real models use the same blocks with the actions students trained. Running the model on a micro:bit needs a V2.

This lesson has not yet been tried in the live MakeCode editor: check the ML panel appears beside the simulator before class.

Running the lesson

  1. Predict: many say 3. Ask: how many times did the estimate change?
  2. Run: choose actions in the ML panel; count aloud.
  3. Investigate: the counting-jumps question is the subtle one; link unknown to Unsure in AI robots.
  4. Make: the tracker reuses serial write value from the first unit.

The hard part (on purpose)

Start and stop events, and a sensible response to unknown. Ask what the program should do when the model isn't sure.

When students get stuck

What you will seeA question to askThe fix
There is no ML panel.Is the editor full screen? Is the simulator running?Open the editor full screen and restart the simulator.
The count goes up only once.Did the action change between jumps?Choose another action (still) in between.
Running time stays 0.Did running stop? The stop event runs when the estimate changes away.Choose running, then still, then press A.

Answers to the checks

  1. Predict: You do three jumps without stopping, and the model estimates jumping the whole time. What does the micro:bit show?
    1. on ML jumping start runs once, when the estimated action changes to jumping. Three jumps in a row are one stretch of jumping.
  2. Counting jumps: To count every jump in a long stretch of jumping, what would you need?
    A model trained on single jumps, with something else (such as still) between them
    The model estimates an action several times a second; the start event fires only when it changes. Counting each jump means each jump must be its own change. is detected in forever would count many times per jump.
  3. Unknown: What does the unknown action mean?
    No action is certain enough (above its recognition point)
    Like Unsure in the AI robots lessons: when nothing is certain enough, the estimate is unknown. Your program can show it, or do nothing.
  4. Stop and duration: on ML running stop gives a duration. What is it?
    How many milliseconds running lasted, just now
    When the estimate changes away from running, the stop event runs and tells you how long running was the estimate. Add the durations up for total running time.

Solutions

One possible solution for each task. Other correct programs are fine.

Modify › Running time

The program as blocks
Show as code
ml.onStart(ml.event.Jumping, function () {
    jumps += 1
    basic.showNumber(jumps)
})
ml.onStart(ml.event.Still, function () {
    basic.showIcon(IconNames.Asleep)
})
ml.onStopDetailed(ml.event.Running, function (duration) {
    running += duration
})
input.onButtonPressed(Button.A, function () {
    basic.showNumber(Math.round(running / 1000))
})
let running = 0
let jumps = 0

Modify › Not sure

The program as blocks
Show as code
ml.onStart(ml.event.Jumping, function () {
    jumps += 1
    basic.showNumber(jumps)
})
ml.onStart(ml.event.Still, function () {
    basic.showIcon(IconNames.Asleep)
})
ml.onStart(ml.event.Unknown, function () {
    basic.showString("?")
})
let jumps = 0

Make

The program as blocks
Show as code
ml.onStart(ml.event.Still, function () {
    basic.showIcon(IconNames.Asleep)
})
ml.onStart(ml.event.Walking, function () {
    basic.showArrow(ArrowNames.East)
})
ml.onStart(ml.event.Running, function () {
    basic.showIcon(IconNames.Rabbit)
})
ml.onStart(ml.event.Jumping, function () {
    jumps += 1
    basic.showNumber(jumps)
})
ml.onStart(ml.event.Unknown, function () {
    basic.clearScreen()
})
ml.onStopDetailed(ml.event.Running, function (duration) {
    running += duration
})
input.onButtonPressed(Button.A, function () {
    serial.writeValue("jumps", jumps)
    serial.writeValue("running", Math.round(running / 1000))
})
let running = 0
let jumps = 0

Stretch and support

Stretch: Use certainty (%) ML jumping in forever to show a bar of how sure the model is.

Support: Give the jump count; students add the other actions.

Exit ticket

Why does on ML jumping start run once during three jumps in a row?

It runs when the estimated action changes to jumping; three jumps in a row are one stretch of jumping.

What each step is for

Where it leads

Next: the capstone, your own AI sorter. Real world: fitness trackers, fall detection, sports coaching sensors.