Approved content

The content below is provided by a partner.

strawbees/pxt-robotic-inventions 0.1.26 GitHub

Strawbees Robotic Inventions for micro:bit

Strawbees Robotic Inventions for micro:bit

This package allows you to program the micro:bit to use it with our kit Robotic Inventions for micro:bit.

The kit allows you to control up to three servos (standard or continuous) and two built-in RGB LEDs.

If you want to learn how to use this kit in the classroom, please visit our learning platform.


Table of contents


Getting started

We have prepared an onboarding guide that will help you get started with the Robotic Inventions for micro:bit. Make sure to check it before using the kit for the first time. The guide will cover:

Download onboarding PDF


First program

If this is your first time using the kit (or if you are preparing it to use it in the classroom for the first time), we recommend that you start by programming the micro:bit with the example below. It allows you to control the servo with the buttons and is a great way to start exploring the movement possibilities before diving into coding.

let position = 0
basic.forever(function () {
    if (input.buttonIsPressed(Button.A)) {
        position += -1
        sb.setRgbLedColor(sb.rgbLed(SBRgbLed.RgbLedA), sb.color(SBColor.Red))
    } else {
        sb.setRgbLedColor(sb.rgbLed(SBRgbLed.RgbLedA), sb.color(SBColor.Blue))
    }
})
basic.forever(function () {
    if (input.buttonIsPressed(Button.B)) {
        position += 1
        sb.setRgbLedColor(sb.rgbLed(SBRgbLed.RgbLedB), sb.color(SBColor.Red))
    } else {
        sb.setRgbLedColor(sb.rgbLed(SBRgbLed.RgbLedB), sb.color(SBColor.Blue))
    }
})
basic.forever(function () {
    position = Math.constrain(position, 0, 100)
    sb.setServoPosition(sb.servo(SBServo.ServoA), position)
})

Coding Cards

Coding cards are small snippets of code that can be used to explore different concepts.

They are not meant to be used as they are but for you to tweak the numbers and combine the cards to get the expected result.

The cards are grouped by the hardware in use:

Hardware icons

Blink

basic.forever(function () {
    sb.setRgbLedColor(sb.rgbLed(SBRgbLed.RgbLedA), sb.color(SBColor.White))
    basic.pause(1000)
    sb.setRgbLedColor(sb.rgbLed(SBRgbLed.RgbLedA), sb.color(SBColor.Black))
    basic.pause(1000)
})

Back and forth

Back and forth

basic.forever(function () {
    sb.transitionServoPosition(sb.servo(SBServo.ServoA), 0, 1, sb.easing(SBEasing.Linear))
    sb.transitionServoPosition(sb.servo(SBServo.ServoA), 100, 1, sb.easing(SBEasing.Linear))
})

Change color 10 times

Change color 10 times

for (let index = 0; index < 10; index++) {
    sb.setRgbLedColorRGB(sb.rgbLed(SBRgbLed.RgbLedA), 100, 0, 0)
    basic.pause(500)
    sb.setRgbLedColorRGB(sb.rgbLed(SBRgbLed.RgbLedA), 0, 0, 100)
    basic.pause(500)
}
sb.setRgbLedColorRGB(sb.rgbLed(SBRgbLed.RgbLedA), 0, 0, 0)

Wave 10 times

Wave 10 times

for (let index = 0; index < 10; index++) {
    sb.setServoPosition(sb.servo(SBServo.ServoA), 20)
    basic.pause(1000)
    sb.setServoPosition(sb.servo(SBServo.ServoA), 80)
    basic.pause(1000)
}

Gradually change brightness

Gradually change brightness

input.onButtonPressed(Button.A, function () {
    brightness = Math.constrain(brightness + 4, 0, 100)
})
let brightness = 0
brightness = 0
basic.forever(function () {
    sb.setRgbLedColorHSB(sb.rgbLed(SBRgbLed.RgbLedA), 0, 100, brightness)
})

Gradually change position

Gradually change position

input.onButtonPressed(Button.A, function () {
    position = Math.constrain(position + 4, 0, 100)
})
let position = 0
position = 0
basic.forever(function () {
    sb.setServoPosition(sb.servo(SBServo.ServoA), position)
})

Change color while pressing button

Change color while pressing button

basic.forever(function () {
    if (input.buttonIsPressed(Button.A)) {
        sb.setRgbLedColorRGB(sb.rgbLed(SBRgbLed.RgbLedA), 100, 0, 0)
    } else {
        sb.setRgbLedColorRGB(sb.rgbLed(SBRgbLed.RgbLedA), 0, 0, 100)
    }
})

Change position while pressing button

Change position while pressing button

basic.forever(function () {
    if (input.buttonIsPressed(Button.A)) {
        sb.setServoPosition(sb.servo(SBServo.ServoA), 0)
    } else {
        sb.setServoPosition(sb.servo(SBServo.ServoA), 100)
    }
})

Shine in the dark

Shine in the dark

basic.forever(function () {
    if (input.lightLevel() < 50) {
        sb.setRgbLedColorRGB(sb.rgbLed(SBRgbLed.RgbLedA), 100, 0, 0)
    } else {

    }
})

Move in the dark

Move in the dark

basic.forever(function () {
    sb.setServoPosition(sb.servo(SBServo.ServoA), Math.constrain(input.lightLevel(), 0, 100))
})

Color party

Color party

basic.forever(function () {
    sb.setRgbLedColorHSB(sb.rgbLed(SBRgbLed.RgbLedA), Math.randomRange(0, 100), 100, 100)
    basic.pause(200)
})

Shake

Shake

basic.forever(function () {
    sb.setServoPosition(sb.servo(SBServo.ServoA), Math.randomRange(0, 100))
})

Rainbow

Rainbow

basic.forever(function () {
    for (let index = 0; index <= 100; index++) {
        sb.setRgbLedColorHSB(sb.rgbLed(SBRgbLed.RgbLedA), index, 100, 100)
        basic.pause(100)
    }
})

Sweep motor

Sweep motor

basic.forever(function () {
    for (let index = 0; index <= 100; index++) {
        sb.setServoPosition(sb.servo(SBServo.ServoA), index)
        basic.pause(100)
    }
})

Loop over a list of colors

Loop over a list of colors

let list = [sb.colorLabel(SBColor.Red), sb.colorLabel(SBColor.Green), sb.colorLabel(SBColor.Blue)]
basic.forever(function () {
    for (let value of list) {
        sb.setRgbLedColor(sb.rgbLed(SBRgbLed.RgbLedA), value)
        basic.pause(1000)
    }
})

Loop over a list of positions

Loop over a list of positions

let list = [0, 50, 100]
basic.forever(function () {
    for (let value of list) {
        sb.setServoPosition(sb.servo(SBServo.ServoA), value)
        basic.pause(1000)
    }
})

Light switch

Light switch

let toggle = 0
input.onButtonPressed(Button.A, function () {
    if (toggle == 0) {
        sb.setRgbLedColorRGB(sb.rgbLed(SBRgbLed.RgbLedA), 100, 0, 0)
        toggle = 1
    } else {
        sb.setRgbLedColorRGB(sb.rgbLed(SBRgbLed.RgbLedA), 0, 0, 0)
        toggle = 0
    }
})

Press button to toggle position

Press button to toggle position

let toggle = 0
input.onButtonPressed(Button.A, function () {
    if (toggle == 0) {
        sb.setServoPosition(sb.servo(SBServo.ServoA), 20)
        toggle = 1
    } else {
        sb.setServoPosition(sb.servo(SBServo.ServoA), 80)
        toggle = 0
    }
})

Tilt to change color

Tilt to change color

let movement = 0
let hue = 0
basic.forever(function () {
    movement = input.acceleration(Dimension.X)
    hue = Math.map(movement, -1023, 1023, 0, 100)
    sb.setRgbLedColorHSB(sb.rgbLed(SBRgbLed.RgbLedA), hue, 100, 100)
})

Tilt to move

Tilt to move

let movement = 0
let position = 0
basic.forever(function () {
    movement = input.acceleration(Dimension.X)
    position = Math.map(movement, -1023, 1023, 0, 100)
    sb.setServoPosition(sb.servo(SBServo.ServoA), position)
})

Light alarm

Light alarm

basic.forever(function () {
    sb.setRgbLedColorRGB(sb.rgbLed(SBRgbLed.RgbLedA), 0, 0, 0)
    if (input.acceleration(Dimension.Strength) > 1100) {
        sb.setRgbLedColorRGB(sb.rgbLed(SBRgbLed.RgbLedA), 100, 0, 0)
        basic.pause(4000)
    }
})

Move when moved

Move when moved

basic.forever(function () {
    if (input.acceleration(Dimension.Strength) > 1200) {
        sb.transitionServoPosition(sb.servo(SBServo.ServoA), 100, 1, sb.easing(SBEasing.Linear))
        sb.transitionServoPosition(sb.servo(SBServo.ServoA), 0, 1, sb.easing(SBEasing.Linear))
    }
})

Send/Receive color: Sender

Send/Receive color: Sender

radio.setGroup(1)
basic.forever(function () {
    if (input.buttonIsPressed(Button.A)) {
        radio.sendValue("light", 100)
    } else {
        radio.sendValue("light", 0)
    }
})

Send/Receive color: Receiver

Send/Receive color: Receiver

radio.onReceivedValue(function (name, value) {
    if (name == "light") {
        sb.setRgbLedColorRGB(sb.rgbLed(SBRgbLed.RgbLedA), 0, value, 0)
    }
})
radio.setGroup(1)

Remote control motor: Sender

Remote control motor: Sender

let movement = 0
radio.setGroup(1)
basic.forever(function () {
    movement = Math.map(input.acceleration(Dimension.X), -1023, 1023, 0, 100)
    radio.sendValue("movement", movement)
})

Remote control motor: Receiver

Remote control motor: Receiver

radio.onReceivedValue(function (name, value) {
    if (name == "movement") {
        sb.setServoPosition(sb.servo(SBServo.ServoA), value)
    }
})
radio.setGroup(1)

Documentation

sb.setServoPosition

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

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

Parameters

Example

Sets the position of a servo connected to the socket A all the way to the end-position (100%), wait one second, 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)

sb.transitionServoPosition

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.

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

Parameters

Example

Transitions the position a servo connected to the socket 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))

sb.setContinuousServoSpeed

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

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

Parameters

Example

Sets a continuous servo connected to the socket B to full speed in the clockwise direction (100%), wait 3 seconds, 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)

sb.turnOffServo

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

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

Parameters

Example

Sets the position of a servo connected to the socket A all the way to the end-position (100%), wait one second, then turn off the servo.

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

sb.setRgbLedColorRGB

Sets the color of an individual RGB LED by specifying the amount of red, green and blue in the color. The amounts range from 0% to 100%.

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

Parameters

Example

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

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

sb.setRgbLedColorHSB

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%.

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

Parameters

Example

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

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

sb.setRgbLedColor

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

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

Parameters

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))

Supported targets

License

MIT

makeCodeRender(“{{ site.makecode.home_url }}”, “{{ site.github.owner_name }}/{{ site.github.repository_name }}”);

Strawbees Robotic Inventions=github:strawbees/pxt-robotic-inventions#v0.1.26