MakeCode Documentation

    MakeCode block code examples for the micro:bit.

    MakeCode is a free online learn-to-code platform made by Microsoft where anyone can build games and write programs for devices like the micro:bit. Programming the Strawbees Robotics board with the micro:bit starts on the MakeCode editor. In this editor has visual drag-and-drop blocks, JavaScript, and Python.

    Sensing the World

    Using the micro:bit enables interactive systems to be created. When programming, generally you fit a combination of the block categories. Inputs, outputs, and processors, also known as brains, to make your system do what you want.

    MakeCode Blocks

    Block programming is snapping drag-and-drop blocks together to create scripts. These scripts are composed to create a sequential, step-by-step set of instructions.

    MakeCode blocks is the core function of programming the micro:bit. Using the programming language in the form of blocks, there is a massive library of documentation for all the blocks for your reference from Microsoft.

    The best way to read code documentation is on MakeCode itself. When selecting a block from the dashboard, right-click for the Help menu. Open the window from the right of the screen to reveal the block documentation on its parameters and examples of implementation.

    On Start

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

    Loops

    if (true) {
    
    }

    Logic

    Variables

    basic.showNumber(0)

    Numbers

    Robotic Inventions Extension

    To use the Robotics board for the micro:bit, you will need to install the Strawbees extension in MakeCode. When in the Extensions library, search for Strawbees then click on the Robotics Inventions extension. There is a Strawbees tab in your library with blocks for programming motors and LEDs. The simulator will display 3 servo motors and 2 RGB LEDs for controlling with the Robotics board.

    Strawbees MakeCode Blocks

    Listed in the following sections are the custom Strawbees MakeCode blocks outlined with parameters and examples for you try!

    Set Servo Position

    sb.setServoPosition(sb.servo(SBServo.ServoA), 100)

    Sets the position of a servo by specifying a value ranging from 0% to 100%.

    Parameters

    • servo - Which servo (A, B or C) to set the position to.
    • position - The position ranging from 0% to 100%.

    Example

    Sets the position of a servo connected to the slot A all the way to the end position 100%, wait 1 second (1000 milliseconds), then return the servo to the start position 0%.

    sb.setServoPosition(sb.servo(SBServo.ServoA), 100)
    basic.pause(1000)
    sb.setServoPosition(sb.servo(SBServo.ServoA), 0)
    MakeCode Example

    Transition Servo Position

    sb.transitionServoPosition(sb.servo(SBServo.ServoA), 100, 1, sb.easing(SBEasing.Linear))

    Transitions the position of a servo over a duration of time (in seconds). The “shape” of the transition is specified by choosing one of the easing functions by name.

    Parameters

    • servo - Which servo (A, B or C) to set the position to.
    • position - The position ranging from 0% to 100%.
    • duration - The duration of the transition, in seconds.
    • easing - The “shape” of the transition (linear, sine, quad, cubic, quart, quint, expo, circ, back, elastic, or bounce).

    Example

    Transitions the position of a servo connected to the slot A to the end position 100%, over 2 seconds. Then transition the servo back to the start-position 0% also over 2 seconds. When the servo moves from one position to the other, the movement will start slow and then speed up, achieved by using the quad out easing function.

    sb.transitionServoPosition(sb.servo(SBServo.ServoA), 100, 2, sb.easing(SBEasing.QuadOut))
    sb.transitionServoPosition(sb.servo(SBServo.ServoA), 0, 2, sb.easing(SBEasing.QuadOut))
    MakeCode Example

    Set Continuous Servo Speed

    sb.setContinuousServoSpeed(sb.servo(SBServo.ServoA), 100)

    Sets the speed of a continuous servo in an arbitrary range from -100% to 100%. If the connected servo is not continuous, this will not work as expected.

    Parameters

    • servo - Which continuous servo (A, B, or C) to set the speed to.
    • speed - The speed ranging from -100% to 100%.

    Example

    Sets a continuous servo connected to the socket B to full speed in the clockwise direction 100%, wait 3 seconds (3000 milliseconds), then revert the direction to counter-clockwise, on half speed -50%.

    sb.setContinuousServoSpeed(sb.servo(SBServo.ServoB), 100)
    basic.pause(3000)
    sb.setContinuousServoSpeed(sb.servo(SBServo.ServoB), -50)
    MakeCode Example

    Turn Off Servo

    sb.turnOffServo(sb.servo(SBServo.ServoA))

    Turns a servo off so that no force will be applied, and it can be rotated manually. This saves battery power.

    Parameters

    • servo - Which servo (A, B, or C) to turn off.

    Example

    Sets the position of a servo connected to the slot A all the way to the end position 100%, wait 1 second (1000 milliseconds), then turn off the servo.

    sb.setServoPosition(sb.servo(SBServo.ServoA), 100)
    basic.pause(1000)
    sb.turnOffServo(sb.servo(SBServo.ServoA))
    MakeCode Example

    Set RGB LED RGB Colors

    sb.setRgbLedColorRGB(sb.rgbLed(SBRgbLed.RgbLedA), 100, 100, 100)

    Sets the color of an individual RGB LED by specifying the amount of red, green and blue in the color.

    Parameters

    • rgbLed - Which RGB LED (A or B) to set the color.
    • red - Amount of red in color, ranging from 0% to 100%.
    • green - Amount of green in color, ranging from 0% to 100%.
    • blue - Amount of blue in color, ranging from 0% to 100%.

    Example

    Sets the RGB LED A to red, by specifying the color as percentages of red 100%, green 0%, and blue 0%.

    sb.setRgbLedColorRGB(sb.rgbLed(SBRgbLed.RgbLedA), 100, 0, 0)
    MakeCode Example

    Set RGB LED Hue Saturation and Brightness

    sb.setRgbLedColorHSB(sb.rgbLed(SBRgbLed.RgbLedA), 100, 100, 100)

    Sets the color of an individual RGB LED by specifying the amount of hue, saturation and brightness in the color. The amounts range from 0% to 100%.

    Parameters

    • rgbLed - Which RGB LED (A or B) to set the color.
    • hue - Hue of the color, ranging from 0% to 100%.
    • saturation - Saturation of the color, ranging from 0% to 100%.
    • brightness - Brightness of the color, ranging from 0% to 100%.

    Example

    Sets the RGB LED B to red, by specifying the color as percentages of hue 0%, saturation 100%, and brightness 100%.

    sb.setRgbLedColorHSB(sb.rgbLed(SBRgbLed.RgbLedB), 0, 100, 100)
    MakeCode Example

    Set RGB LED Color

    sb.setRgbLedColor(sb.rgbLed(SBRgbLed.RgbLedA), sb.color(SBColor.Red))

    Sets the color of an individual RGB LED by specifying the color by name.

    Parameters

    • rgbLed - Which RGB LED (A or B) to set the color.
    • color - The name of the color from a list of color labels.

    Example

    Sets RGB LEDs A and B, to yellow and green, respectively, by selecting the colors by name from a pre-defined list.

    sb.setRgbLedColor(sb.rgbLed(SBRgbLed.RgbLedA), sb.color(SBColor.Yellow))
    sb.setRgbLedColor(sb.rgbLed(SBRgbLed.RgbLedB), sb.color(SBColor.Green))
    MakeCode Example