Race against your friends! In this game, RVR's LEDs will randomly change colors. Your job is to press the button that matches the color of RVR's LEDs. The better you do, the faster RVR will go! If you hit the wrong button or you wait too long to press a button, RVR will slow down, so try to keep up!
This straighforward example should take you about 30 min to complete
If you haven't already set up your micro:bit using your favorite editor, it's probably best to take care of that before jumping in here. Feel free to check out our Getting Started docs on:
If you want to build your own Rainbow Racer, you will need the following parts (most of them are included in this nifty "inventor's" kit from SparkFun):
- BBC micro:bit
- Micro B USB cable (preferably a long one!)
- SparkFun micro:bit Breakout (with Headers)
- 4 Multi-Colored Momentary Push Buttons (we used red, green, blue, and yellow)
- 4 10K Ω Resistors
- 12 Jumper Wires
First you'll need to put together your circuit! Follow the wiring diagram below:
After you've got your circuit set up, you're ready to flash the code to your micro:bit. Let's explore the code a bit and then we'll walk through how to flash the code.
Let's get started with our imports. We'll need
random for the RVR to be able to choose a random color for the user to have to match. From the
microbit, we'll pull in what we need to transfer information (via the needed
pins) and give feedback to the user (using
import random from microbit import display, Image, sleep, pin8, pin12, pin13, pin16 from sphero import RVRDrive, RVRLed, RVRPower
check_input function will read the GPIO pins to figure out which button (if any) was pressed:
def check_input(): input_btn = 0 if pin8.read_digital(): input_btn = 1 # yellow elif pin12.read_digital(): input_btn = 2 # green elif pin13.read_digital(): input_btn = 3 # red elif pin16.read_digital(): input_btn = 4 # blue return input_btn
set_leds function will set RVR's LEDs depending on the randomly generated
def set_leds(): if current_color == 1: RVRLed.set_all_leds(255, 255, 0) # yellow elif current_color == 2: RVRLed.set_all_leds(0, 255, 0) # green elif current_color == 3: RVRLed.set_all_leds(255, 0, 0) # red elif current_color == 4: RVRLed.set_all_leds(0, 0, 255) # blue return
show_correct_feedback function will show a check mark if the
input_btn matched the
def show_correct_feedback(): display.show(Image.YES) sleep(600) display.clear()
show_incorrect_feedback function will show an X if the
input_btn did not match the
def show_incorrect_feedback(): display.show(Image.NO) sleep(600) display.clear()
Now that we've gotten through our function definitions, we can wake up our RVR, in preparation for using these functions:
Next, we'll initialize our variables (and use our
random import 😉):
current_color = random.randint(1, 4) current_speed = 0 penalty_counter = 0 input_btn = 0
Now we get to the good stuff! First, we'll initialize our LED value and then we'll set that we should check the user's input when we use
input_btn and initialize a counter for how many times the user hits an incorrect button.
In our first bit of logic, we'll check if the user takes too long to press a button.
In the second section, we have two pieces of logic that keep
current_speed within the range [0, 60]; we'll use the
current_speed we land on in a
drive command to make the RVR move forward at the
Once we have all that information, we check user input and give proper feedback!
while True: set_leds() input_btn = check_input() penalty_counter += 1 if penalty_counter > 250: show_incorrect_feedback() current_speed -= 6 penalty_counter = 0 current_color = random.randint(1, 4) input_btn = 0 if current_speed > 60: current_speed = 60 if current_speed < 0: current_speed = 0 RVRDrive.drive(current_speed, 0) if input_btn == 0: pass elif input_btn == current_color: show_correct_feedback() current_speed += 6 penalty_counter = 0 current_color = random.randint(1, 4) input_btn = 0 elif input_btn != current_color: show_incorrect_feedback() current_speed -= 6 input_btn = 0
Now, you're ready to play!
Flashing the Code
Follow the below steps:
- Download the rainbow_racer.py and sphero.py files.
rainbow_racer.pyis the file that will run on the micro:bit, and
sphero.pycontains the functions you'll need to be able to interact with RVR using your micro:bit. You'll notice that
RVRLedare imported from
sphero.pyat the top of the
- We're going to use an editor called Mu to edit our Python code and flash it to the micro:bit. You can follow this link if you don't already have Mu downloaded on your computer. This link is useful if you haven't used Mu with micro:bit before.
- Once you've opened Mu, you'll need to open the
rainbow_racer.pyfile in Mu. Do this by clicking the Load button at the top of the screen and navigating to the
rainbow_racer.pyfile (it's probably in your downloads folder).
- You'll also need to put the
sphero.pyfile in a place where Mu can reach it. Find a folder called mu_code; it should have been created automatically when Mu was downloaded. Move
sphero.pyinto the mu_code folder.
- Now we can flash the code onto your micro:bit! Plug your micro:bit into your computer using the Micro B USB cable. After a few seconds, you'll see a message along the bottom of Mu's interface that says "Detected new BBC micro:bit device."
- After that message has appeared, you can click the Flash button at the top of the screen. You'll see the orange light on the back of the micro:bit flash for a few seconds while the code is being flashed to the device.
- Once the flashing stops, you'll see an error message scroll across micro:bit's LED matrix: "Line 3 ImportError no module named 'sphero'". This is because your micro:bit doesn't have access to the
sphero.pyfile. To fix that, we're going to copy the file onto the micro:bit. Click the Files button at the top of the screen. This will open a window at the bottom of the screen showing the files you have on your micro:bit on the left and the files in the mu_code folder on the right. Drag the
sphero.pyfile from the right side to the left side to copy it onto your micro:bit. This should just take a couple seconds.
- Now you're good to go! Just eject the micro:bit from your computer before you unplug the USB cable and you're ready to race!
To start the race all you'll need to do is plug the micro:bit (which should be mounted in the breakout board) into the RVR using the USB cable, and then turn on the RVR. You'll need to walk with the RVR as it moves since it's plugged into your micro:bit - that's why we suggested finding a long cable! If you have a friend that built their own Rainbow Racer, you can race them! Consider putting tape lanes and a finish line on the ground to have an official race track (keep in mind that the RVR will only drive in a straight line). If you're flying solo, run multiple races and keep track of your times. See if you can beat your own high score!
Try to improve your Rainbow Racer! Here are some ideas to get you started:
- Speed things up by playing with some of the values in the
- Need more of a challenge? Add more buttons and colors!
- Add a joystick to your circuit so you can steer your RVR.
- The breadboard circuit can be pretty clunky to race with - try designing a better controller!
If you're adding a lot to your code, you may run into some Memory Errors. That's okay - micro:bit is best for smaller projects. If you feel like you've outgrown your micro:bit, try upgrading to an Arduino or a Raspberry Pi!