Remote Control

How About A Wireless Setup??

We are taking away the training wheels! In this example, we'll give the RVR+/RVR its own micro:bit and use a second micro:bit with a gamer:bit as a controller to cut the physical ties between the RVR+/RVR and that which is controlling it.

This example should take about 45 min to go through before you are ready to hit the road!

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:

You won't need to use one of these editors for this example, but you will need to use them to set up your micro:bit initially, before you can complete this example.

Hardware Setup

Parts List


If you want to build your own Remote Control RVR+/RVR, you will need the following parts (most of them are included in this nifty "inventor's" kit from SparkFun):

Controller Code

Since this is a MakeCode example, we'll use the online MakeCode editor to create our program. Add the RVR+/RVR extension to MakeCode and let's get started!

Let's initiate our program by stating that the RVR+/RVR is not moving and is facing forward, and that no buttons are being pressed:


Functions

We need to write some functions to be used when we press the directional buttons on the gamer:bit. Let's start with the function to change the speed of the RVR+/RVR; we start by incrementing the speed by a value delta that is fed in when this function is called. We also set some bounds for speed so that it is not below 0 or above 80. Once we have our value for our speed, we'll pack it into a string to be sent to the RVR+/RVR and send both that string and a "drive" string to the RVR+/RVR:


We'll use the function we just wrote in a function to slow the RVR+/RVR down when there are no buttons being pressed:

Our third function is to change the direction of the RVR+/RVR. We change the direction by whatever value has been input as the argument of our function and check, once the initial "setting" is finished, that the resulting value for heading is between 0 and 359 (since 360 is the same as 0); if the value is below 0, we add it to 360, if the value is above or equal to 360, we subtract 360 from it (again, since 360 is the same as 0).

Once all manipulations of the heading are finished, we will set the value of heading to a "heading" string, to be fed to the RVR+/RVR, pause to allow that action to take place and then send a "drive" string to the RVR+/RVR (which will use the "heading" we sent just a moment before to know what direction to "drive" in):


In the next section, we'll utilize the above functions and connect the pressing and releasing of each directional button to a behavior! We'll handle both when the button is pressed or held and when it is released:

Going Forward

We'll use the up button on on the gamer:bit to make the RVR+/RVR go forward. When the up button is pressed, we are going to check if the speed is less than the "normal" base speed and set the speed to that (10), if it is not. If we do reset the speed, we'll also notify the RVR+/RVR, by resetting the value of the "speed" string and radioing the new (key & value) pair over to the RVR+/RVR.

We'll then change (increase, by 1) the number of buttons that is active. Once activeButtons has been set, we'll set its string counterpart to the same value and radio it over to the RVR+/RVR.

The green section below all of that is a loop that handles the behavior of the button being held down, during which time, we'll continue to increase the speed by 4 with each iteration:


Once the up button is released, we'll change (decrease, by 1) the number of buttons that are being pressed/held at the time and set the "activeButtons" to the same value as activeButtons and radio it over to the RVR+/RVR. We then insert a pause (to allow that action to complete before we attempt another) and call the slowDown function that we wrote above:



Going Backward

When the down is pressed, we will want to:

We, then, have a loop that, as the down button is held, decreases the value of speed (which either slows down a bot that is moving forward OR speeds up a bot that is moving backward), first checking that the speed is not already at the maximum value of 80 (but in reverse, so, -80) (in which case, we do nothing further).

If we are already heading in the backwards direction (the speed is less than 0, but greater than -80), then we decrease (increase in the backward direction) the speed by 4. If the RVR+/RVR is still heading in the forward direction, we slow it down more quickly than that, by decreasing the speed by 8.

Once the value of the speed has been set, we copy it to the "speed" string and radio it to the RVR+/RVR, pause to let that take place, send a "drive" string to the RVR+/RVR and then pause to let that take place.

The above cycle of speed/"speed" adjustment takes place as long as the user is holding down the down button:



When the down button is released, it triggers a new behavior. We'll send the bot a string "command" to dim the break lights, pause to let that take place, decrease the number of active buttons by 1, radio that new value of "activeButtons" to the RVR+/RVR, pause to let that take place, and then begin to slow the RVR+/RVR down, using our slowDown function:

Turning Left

When the left button is pressed, we'll change (increase, by 1) the number of buttons that is active. Once activeButtons has been set, we'll set its string counterpart to the same value and radio it over to the RVR+/RVR. Then, the entire time the left button is being held, each loop through, the heading will be changed by 5 degrees to the left, using our updateHeading function (which, if you'll remember, also radios a "drive" command to the RVR+/RVR):


When the left button is released, we bump the number of active buttons down by 1 again and radio the change in "activeButtons" to the RVR+/RVR. We pause to let that take place, and then we'll call our slowDown function to slow the RVR+/RVR down:

Turning Right

When the right button is pressed, we'll change (increase, by 1) the number of buttons that is active. Once activeButtons has been set, we'll set its string counterpart to the same value and radio it over to the RVR+/RVR. Then, the entire time the right button is being held, each loop through, the heading will be changed by 5 degrees to the right, using our updateHeading function (which, if you'll remember, also radios a "drive" command to the RVR+/RVR):


When the right button is released, we bump the number of active buttons down by 1 again and radio the change in "activeButtons" to the RVR+/RVR. We pause to let that take place, and then we'll call our slowDown function to slow the RVR+/RVR down:

Last, But Not Least

As the final step of writing the MakeCode program for your controller, you can download the code from online MakeCode editor and drag and drop it onto your "controller micro:bit" (feel free to check out our guide on flashing files to your micro:bit, if you have any questions!), and you should be ready to move on to the code for the RVR+/RVR micro:bit 🤓

RVR+/RVR Code

Let's initiate our program by stating that the RVR+/RVR is not moving, is facing forward, and that no buttons are being pressed, as well as blanking out the headlight colors and then turning first the left and then the right headlight to white. Additionally, we'll set the break lights to dim, since we aren't taking any actions right now:


Functions

The fun one! Our drive function simply takes in a direction (heading) and a speed and tells the RVR+/RVR to drive at the given speed in the given direction:



In our onStart function, we called dimBrakelights, which we'll get to in just a moment, but we also have a function that turns the brake lights on. In this function, we simply set the right and left brake lights to red, zeroing out the other two colors:

In order to dim the brake lights, we don't entirely turn them off (we still want to be visible!), we simply keep the green and blue values of the brake lights at 0 and decrease the amount of red to 40 (from 255):

Cracking the Code (Communicating with the Controller)

The functions above tell the RVR+/RVR what to do when we get a command from the controller, but we haven't yet written anything to interpret the commands we receive from the controller. We receive two types of things from the controller: commands to take an action (eg drive) and details for how to execute actions (eg at what speed we should drive). We have created two functions to ingest information from the controller: one for each of those categories.

Our first function takes in the action strings sent by the controller, "drive", "turnBrakelightsOn" and "dimBreaklights" and converts them into actions (and calls them): drive, turnBrakelightsOn and dimBreaklights:


Our second function takes in the detail strings sent by the controller, "speed", "heading" and "activeButtons", each of which comes with a value, and sets the variables with the same names equal to the corresponding values:

Last, But Not Least

As the final step of writing the MakeCode program for your RVR+/RVR, you can download the code from online MakeCode editor and drag and drop it onto your "RVR+/RVR micro:bit" (feel free to check out our guide on flashing files to your micro:bit, if you have any questions!), and you should be ready to plug everything in and play 🤓

Hardware Setup (Plugging Everything In)

Remote Control

To set up the controller for your RVR+/RVR, insert the batteries into the battery holder and plug the battery holder into the JST connection port on the first micro:bit:


Next, just pop the golden edge at the bottom of the micro:bit into the edge pin connector in the center of the gamer:bit:

RVR+/RVR

To set up the RVR+/RVR, plug the micro USB end of the Micro B USB cable into the micro USB port on the second micro:bit and the USB end into the USB port on the RVR+/RVR:


You Did It!!!

Now that you've written your code, loaded it onto your micro:bit and gotten all of the parts and pieces put together, you have an awesome remote control vehicle that YOU made possible.

It's time to get out and have some fun! Take some photos of your adventures and send them our way. Additionally, not being tethered down is a perfect opportunity to make some additions to your RVR+/RVR...

Now What?

Try to improve your Remote Control RVR+/RVR! Here are some ideas to get you started: