← Secret messages

micro:bit blocks reference

The blocks from micro:bit Foundations, each with what it does. Find them in the MakeCode toolbox; the picture shows the block, the code is the same block as MakeCode JavaScript.

Print a cheat sheet (the whole course or one unit, with a picture of each block)

Show and react

basic.showIcon(IconNames.Heart)
show icon

Shows a ready-made picture. Basic › show icon; click the picture to choose another.

The block

id: show-icon

basic.showLeds(`
    . # . # .
    # # # # #
    # # # # #
    . # # # .
    . . # . .
    `)
show leds

Draw your own picture: click squares to light them (# is on). Basic › show leds.

The block

id: show-leds

basic.showString("Hello")
show string

Scrolls a word or message across the screen. Basic › show string.

The block

id: show-string

basic.showNumber(0)
show number

Shows a number; numbers above 9 scroll. Basic › show number.

The block

id: show-number

basic.showArrow(ArrowNames.West)
show arrow

Shows an arrow pointing a direction. Basic › more › show arrow.

The block

id: show-arrow

basic.clearScreen()
clear screen

Turns every LED off. Basic › more › clear screen.

The block

id: clear-screen

basic.pause(500)
pause

Waits before the next block. 1000 ms is one second. Basic › pause.

The block

id: pause

basic.forever(function () {
    blocks
})
forever

Runs the blocks inside again and again. Blocks in on start run only once.

The block

id: forever

basic.forever(function () {
    basic.showIcon(IconNames.Heart)
    basic.pause(500)
    basic.showIcon(IconNames.SmallHeart)
    basic.pause(500)
})
animation

Pictures inside forever repeat, in order. Each pause holds the picture before it.

The block

id: animation

input.onButtonPressed(Button.A, function () {
    blocks
})
on button pressed

Runs the blocks inside every time the button is pressed. Choose A, B or A+B. Input.

The block

id: on-button-pressed

input.onGesture(Gesture.Shake, function () {
    blocks
})
on shake

Runs the blocks inside every time the micro:bit is shaken (or tilted, if you choose another gesture). Input.

The block

id: on-shake

Remember and sense

let count = 0
set variable

Puts a value in a variable, throwing the old value away. Make the variable in Variables first.

The block

id: set-variable

count += 1
change variable

Adds to a variable's value. Change by -1 counts down. Variables.

The block

id: change-variable

let count = 0
input.onButtonPressed(Button.A, function () {
    count += 1
    basic.showNumber(count)
})
counter

Set the start value once, in on start. change adds to whatever the variable holds now.

The block

id: counter

input.lightLevel()
light level

How bright it is: 0 (dark) to 255 (bright). Input.

The block

id: light-level

input.temperature()
temperature

How warm it is, in degrees Celsius. Input.

The block

id: temperature

input.acceleration(Dimension.X)
acceleration

Tilt: x is left (below 0) and right (above 0), y is forward and back. Input › more.

The block

id: acceleration

led.plotBarGraph(value, 255)
plot bar graph

Lights more LEDs for a bigger value. The second number is the biggest value. Led.

The block

id: plot-bar-graph

Decide

if (condition) {
    blocks
}
if

Runs the blocks inside only when the condition is true; does nothing when it is false. Logic.

The block

id: if

if (condition) {
    blocks
} else {
    blocks
}
if else

Exactly one part runs: the first when the condition is true, else the second. Click + on an if block.

The block

id: if-else

} else if (condition) {
else if

Another question, asked only if the ones above were false. The first true one wins.

The block

id: else-if

Lists and functions

let answers = ["Yes", "No"]
list

Many values in order under one name. Places start at 0. Advanced › Arrays.

The block

id: list

answers[0]
get value at

The item at a place in the list, counted from 0. Arrays.

The block

id: get-value-at

answers.length
length of list

How many items are in the list. The last place is the length − 1. Arrays.

The block

id: length-of-list

randint(0, 10)
pick random

A random whole number from the first to the second, both included. Math.

The block

id: pick-random

basic.showString(list[randint(0, list.length - 1)])
pick from list

A random place from 0 to length − 1 reaches every item, however long the list grows.

The block

id: pick-from-list

function name () {
    blocks
}
function

A named job. Making it does not run it: a call block runs it. Advanced › Functions.

The block

id: function

function showScore (points: number) {
    blocks
}
function with a parameter

A function with an input. Call it with a value: showScore(5). Functions › Add a parameter.

The block

id: function-with-a-parameter

Talk

radio.setGroup(1)
radio set group

Picks a channel (0 to 255). Only micro:bits in the same group hear each other. Radio.

The block

id: radio-set-group

radio.sendNumber(0)
radio send number

Sends a number to every other micro:bit in the group. Radio.

The block

id: radio-send-number

radio.onReceivedNumber(function (receivedNumber) {
    blocks
})
on radio received

Runs each time a number arrives; receivedNumber holds it. Radio.

The block

id: on-radio-received

radio.onReceivedNumber(function (receivedNumber) {
    if (receivedNumber == 1) {
        blocks
    } else if (receivedNumber == 2) {
        blocks
    }
})
which message

One on radio received gets every message; receivedNumber says which one arrived.

The block

id: which-message

let words = ["Hi", "Yes", "No"]
radio.onReceivedNumber(function (receivedNumber) {
    basic.showString(words[receivedNumber])
})
shared code

Send a place number; both micro:bits must hold the same list in the same order.

The block

id: shared-code

radio.sendString("hi")
radio send string

Sends a word to every other micro:bit in the group. Radio.

The block

id: radio-send-string

radio.onReceivedString(function (receivedString) {
    blocks
})
on radio received string

Runs each time a word arrives; receivedString holds it. Radio.

The block

id: on-radio-received-string

Time

input.runningTime()
running time

Milliseconds since the micro:bit switched on. Take one from a later one to time something. Input › more.

The block

id: running-time

start = input.runningTime()
basic.showNumber(input.runningTime() - start)
time it

Store running time at the start; later, running time − start is how long it took (ms).

The block

id: time-it

waiting = true
true or false

A variable can hold true or false, to remember what is happening now. Logic.

The block

id: true-or-false

Talk to the computer

serial.writeValue("light", input.lightLevel())
serial write value

Sends a line such as light:128 down the USB cable. The board graphs it; MakeCode's simulator shows it under Show data. Serial.

The block

id: serial-write-value

serial.writeLine("ok")
serial write line

Sends a line of text down the USB cable. Serial.

The block

id: serial-write-line

serial.onDataReceived(serial.delimiters(Delimiters.NewLine), function () {
    blocks
})
on serial data received

Runs each time a whole line arrives over USB (it ends with a new line). Serial.

The block

id: on-serial-data-received

serial.readLine()
serial read line

The line that arrived, as text, without its new line. Use it inside on serial data received. Serial.

The block

id: serial-read-line

radio.sendValue("bin", 1)
radio send value

Sends a name and a number by radio, such as bin and 1. Radio.

The block

id: radio-send-value

radio.onReceivedValue(function (name, value) {
    blocks
})
on radio received value

Runs each time a name and number arrive by radio. Radio.

The block

id: on-radio-received-value

pins.servoWritePin(AnalogPin.P0, 90)
servo write pin

Turns a servo on that pin to an angle from 0 to 180 (90 is the middle). Pins.

The block

id: servo-write-pin

Letters and codes

for (let index = 0; index <= 4; index++) {
    blocks
}
for index

Runs the blocks inside once for each number from 0 up to the last one, with index set to that number each time. Loops.

The block

id: for-index

message.charAt(0)
letter at

The letter at a place in a text. Places start at 0, as in a list. Text (char from … at).

The block

id: letter-at

alphabet.indexOf(letter)
find in text

The place where something first appears in a text, counted from 0, or -1 if it is not there. Text (find index of … in …).

The block

id: find-in-text

message.length
length of text

How many letters (and spaces) a text has. The last letter is at place length − 1. Text.

The block

id: length-of-text

result = letter + result
join

Joins two texts, one after the other: "AB" + "C" is "ABC". Text (join).

The block

id: join

place % 26
remainder

What is left over after dividing: 27 % 26 is 1. Wraps a count back round to the start. Math (remainder of … ÷ …).

The block

id: remainder

message ^ key
xor with a key

XOR: compares two numbers bit by bit. Doing it again with the same key brings the number back, so one key locks and unlocks. Written ^ in JavaScript and Python.

The block

id: xor-with-a-key