How to Use the micro:bit SDK
MakeCode SDK for micro:bit
Follow the instructions in the micro:bit MakeCode Setup page to install the RVR+/RVR SDK as an extension directly in MakeCode.
Explore the RVR+/RVR blocks that are added to the MakeCode programming environment.
Python SDK for micro:bit
Where to get the code
Clone the Python SDK from the GitHub repo!
Driving - Drive with Heading
from microbit import sleep
from sphero import RVRDrive, RVRPower
# wait for RVR to turn on
RVRPower.wake()
sleep(2000)
# drive forward
count = 0
while count < 15:
RVRDrive.drive(60, 0)
sleep(200)
count += 1
# drive to the right
count = 0
while count < 15:
RVRDrive.drive(60, 90)
sleep(200)
count += 1
# drive back
count = 0
while count < 15:
RVRDrive.drive(60, 180)
sleep(200)
count += 1
# drive to the left
count = 0
while count < 15:
RVRDrive.drive(60, 270)
sleep(200)
count += 1
# stop driving
RVRDrive.stop(270)
Set Raw Motors
from microbit import sleep
from sphero import RVRDrive, RawMotorModes, RVRPower
# wait for RVR to turn on
RVRPower.wake()
sleep(2000)
# drive forward with speed 60
count = 0
while count < 15:
RVRDrive.set_raw_motors(RawMotorModes.FORWARD, 60, RawMotorModes.FORWARD, 60)
sleep(200)
count += 1
# stop and wait 1 second
RVRDrive.set_raw_motors(RawMotorModes.OFF, 0, RawMotorModes.OFF, 0)
sleep(1000)
# drive backward with speed 60
count = 0
while count < 15:
RVRDrive.set_raw_motors(RawMotorModes.BACKWARD, 60, RawMotorModes.BACKWARD, 60)
sleep(200)
count += 1
# stop and wait 1 second
RVRDrive.set_raw_motors(RawMotorModes.OFF, 0, RawMotorModes.OFF, 0)
sleep(1000)
# drive forward with speed 80 on the left motor and speed 40 on the right motor
count = 0
while count < 20:
RVRDrive.set_raw_motors(RawMotorModes.FORWARD, 80, RawMotorModes.FORWARD, 40)
sleep(200)
count += 1
# stop and wait 1 second
RVRDrive.set_raw_motors(RawMotorModes.OFF, 0, RawMotorModes.OFF, 0)
sleep(1000)
# drive forward with speed 40 on the left motor and speed 80 on the right motor
count = 0
while count < 20:
RVRDrive.set_raw_motors(RawMotorModes.FORWARD, 40, RawMotorModes.FORWARD, 80)
sleep(200)
count += 1
# stop
RVRDrive.set_raw_motors(RawMotorModes.OFF, 0, RawMotorModes.OFF, 0)
LEDs - Set LEDs
from microbit import sleep
from sphero import RVRLed, LEDs, RVRPower
# wait for RVR to turn on
RVRPower.wake()
sleep(2000)
# turn all LEDs off and wait 2 seconds
RVRLed.set_all_leds(0, 0, 0)
sleep(2000)
while True:
# turn all LEDS off
RVRLed.set_all_leds(0, 0, 0)
# set headlights to red
RVRLed.set_rgb_led_by_index(LEDs.RIGHT_HEADLIGHT, 255, 0, 0)
RVRLed.set_rgb_led_by_index(LEDs.LEFT_HEADLIGHT, 255, 0, 0)
# wait 1 second
sleep(1000)
# set headlights to yellow
RVRLed.set_rgb_led_by_index(LEDs.RIGHT_HEADLIGHT, 255, 255, 0)
RVRLed.set_rgb_led_by_index(LEDs.LEFT_HEADLIGHT, 255, 255, 0)
# wait 1 second
sleep(1000)
# set headlights to green
RVRLed.set_rgb_led_by_index(LEDs.RIGHT_HEADLIGHT, 0, 255, 0)
RVRLed.set_rgb_led_by_index(LEDs.LEFT_HEADLIGHT, 0, 255, 0)
# wait 1 second
sleep(1000)
# set headlights to blue
RVRLed.set_rgb_led_by_index(LEDs.RIGHT_HEADLIGHT, 0, 0, 255)
RVRLed.set_rgb_led_by_index(LEDs.LEFT_HEADLIGHT, 0, 0, 255)
# wait one second
sleep(1000)
# set all LEDs to white and wait 2 seconds
RVRLed.set_all_leds(255, 255, 255)
sleep(2000)