Approved content
The content below is provided by a partner.
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.
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:
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 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:
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)
})
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))
})
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)
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)
}
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)
})
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)
})
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)
}
})
basic.forever(function () {
if (input.buttonIsPressed(Button.A)) {
sb.setServoPosition(sb.servo(SBServo.ServoA), 0)
} else {
sb.setServoPosition(sb.servo(SBServo.ServoA), 100)
}
})
basic.forever(function () {
if (input.lightLevel() < 50) {
sb.setRgbLedColorRGB(sb.rgbLed(SBRgbLed.RgbLedA), 100, 0, 0)
} else {
}
})
basic.forever(function () {
sb.setServoPosition(sb.servo(SBServo.ServoA), Math.constrain(input.lightLevel(), 0, 100))
})
basic.forever(function () {
sb.setRgbLedColorHSB(sb.rgbLed(SBRgbLed.RgbLedA), Math.randomRange(0, 100), 100, 100)
basic.pause(200)
})
basic.forever(function () {
sb.setServoPosition(sb.servo(SBServo.ServoA), Math.randomRange(0, 100))
})
basic.forever(function () {
for (let index = 0; index <= 100; index++) {
sb.setRgbLedColorHSB(sb.rgbLed(SBRgbLed.RgbLedA), index, 100, 100)
basic.pause(100)
}
})
basic.forever(function () {
for (let index = 0; index <= 100; index++) {
sb.setServoPosition(sb.servo(SBServo.ServoA), index)
basic.pause(100)
}
})
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)
}
})
let list = [0, 50, 100]
basic.forever(function () {
for (let value of list) {
sb.setServoPosition(sb.servo(SBServo.ServoA), value)
basic.pause(1000)
}
})
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
}
})
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
}
})
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)
})
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)
})
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)
}
})
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))
}
})
radio.setGroup(1)
basic.forever(function () {
if (input.buttonIsPressed(Button.A)) {
radio.sendValue("light", 100)
} else {
radio.sendValue("light", 0)
}
})
radio.onReceivedValue(function (name, value) {
if (name == "light") {
sb.setRgbLedColorRGB(sb.rgbLed(SBRgbLed.RgbLedA), 0, value, 0)
}
})
radio.setGroup(1)
let movement = 0
radio.setGroup(1)
basic.forever(function () {
movement = Math.map(input.acceleration(Dimension.X), -1023, 1023, 0, 100)
radio.sendValue("movement", movement)
})
radio.onReceivedValue(function (name, value) {
if (name == "movement") {
sb.setServoPosition(sb.servo(SBServo.ServoA), value)
}
})
radio.setGroup(1)
Sets the position of a servo by specifying a value ranging from 0%
to 100%
.
sb.setServoPosition(sb.servo(SBServo.ServoA), 100)
servo
- Which servo (A
, B
or C
) to set the position to.position
- The position ranging from 0%
to 100%
.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)
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))
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.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))
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)
servo
- Which continuous servo (A
, B
or C
) to set the speed to.speed
- The speed ranging from -100%
to 100%
.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)
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))
servo
- Which servo (A
, B
or C
) to turn off.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))
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)
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%
.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)
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)
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%
.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)
Sets the color of an individual RGB LED by specifying the color by name.
sb.setRgbLedColor(sb.rgbLed(SBRgbLed.RgbLedA), sb.color(SBColor.Red))
rgbLed
- Which RGB LED (A
or B
) to set the color.color
- The name of the color from a list of color labels.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))
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