← Reference

Robotics challenges: cheat sheet

Robotics challenges · Find each block in the MakeCode toolbox (the category is at the end of each line).

Drive

robot start

Picks which robot you have. Put it first, in on start, before any other robot block. Robot › robot … start.

robot.elecfreaksCuteBot.start()
The block

robot motor stop

Stops both wheels. End every move and every program that should finish with it.

robot.motorStop()
The block

counter

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

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

robot motor tank for a time

The same, then waits that many milliseconds before the next block. Click + on the block to add the time. It does not stop the motors: follow it with robot motor stop.

robot.motorTank(50, -50, 500)
The block

on button pressed

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

input.onButtonPressed(Button.A, function () {
    blocks
})
The block

function

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

function name () {
    blocks
}
The block

Sense

robot motor tank

Sets the left and right wheel speeds, from −100 to 100. Same speeds go straight, opposite speeds spin, different speeds curve. The robot keeps going until another motor block.

robot.motorTank(50, 50)
The block

robot set color

Sets the robot's lights to a colour. Click the colour to choose another; black turns them off.

robot.setColor(0xff0000)
The block

forever

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

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

if

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

if (condition) {
    blocks
}
The block

if else

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

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

robot obstacle distance

How far away the nearest thing in front is, in centimetres (up to 40; 40 means nothing in range). Read it again in forever to follow it.

robot.obstacleDistance()
The block

robot detect line

True when that line sensor (left or right) is over the dark line. Steer towards the side that still sees it.

robot.detectLine(RobotLineDetector.Left)
The block

show icon

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

basic.showIcon(IconNames.Heart)
The block

animation

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

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

else if

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

} else if (condition) {
The block

pick random

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

randint(0, 10)
The block

Modes

robot play tone

Plays a note on the robot's buzzer: a frequency (262 is middle C) for a time in milliseconds.

robot.playTone(262, 500)
The block

on shake

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

input.onGesture(Gesture.Shake, function () {
    blocks
})
The block

on logo pressed

Runs its blocks when you touch the gold logo on the front of the micro:bit (V2 only; on a V1, use on shake). Input › on logo pressed.

input.onLogoEvent(TouchButtonEvent.Pressed, function () {
    
})
The block

show arrow

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

basic.showArrow(ArrowNames.West)
The block

Talk

play melody

Plays notes on the micro:bit: a letter for each note, - for a rest, at a speed in beats per minute. Until done waits for the tune to end; in background moves on at once. Music › play melody.

music.play(music.stringPlayable("C D E F G A B C5", 120), music.PlaybackMode.UntilDone)
The block

show number

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

basic.showNumber(0)
The block

set variable

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

let count = 0
The block

radio set group

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

radio.setGroup(1)
The block

on radio received

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

radio.onReceivedNumber(function (receivedNumber) {
    blocks
})
The block

radio send string

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

radio.sendString("hi")
The block

on radio received string

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

radio.onReceivedString(function (receivedString) {
    blocks
})
The block

play tone

Plays one note on the micro:bit for a number of beats. In background, so the program carries on. Music › play tone.

music.play(music.tonePlayable(262, music.beat(BeatFraction.Whole)), music.PlaybackMode.InBackground)
The block

show string

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

basic.showString("Hello")
The block

pause

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

basic.pause(500)
The block

change variable

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

count += 1
The block

radio send number

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

radio.sendNumber(0)
The block

which message

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

radio.onReceivedNumber(function (receivedNumber) {
    if (receivedNumber == 1) {
        blocks
    } else if (receivedNumber == 2) {
        blocks
    }
})
The block

true or false

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

waiting = true
The block