[2020] Lighting Up LEDs for Night Crawling

1.3K51

Intro: [2020] Lighting Up LEDs for Night Crawling

Valenta Off-Roader

Valenta Off-Roader is a Micro:bit powered Off-Road RC car. It is Lego Technic compatible and equipped with two (x2) micro gear motors on the rear wheels and (x1) steering servo based on Roberval arm mechanism. Humming Works LLC and 4Tronix UK are working in partnership for the design of Valenta Off-Roader.

"Night Crawler" version

The new version has come up with the built-in x4 LEDs for night crawling! Using Microsoft MakeCode editor and the Valenta extensions, you could set up various LED flashing modes, duration and RGB colors! Personalize your car and light up the mood!

STEP 1: Copying the MakeCode Sample File

For this project, we have prepared the MakeCode sample file (Receiver.hex) so you can download. Since the sample file is ready to play, you can start playing immediately.

In this step, you can download the sample file to your computer. Connect your computer and the micro:bit via USB cable. Drag and drop the sample file to the micro:bit and use this as "receiver" micro:bit.

Once you've copied the sample file to the micro:bit, disconnect it from your computer. Then, mount the micro:bit on Valenta Off-Roader and turn on the power switch on the motor controller.

STEP 2: Setting Up Micro:bit IOS GamePad App

For installing micro:bit iOS App on your iPhone or iPad, pairing the app to the micro:bit and setting up the Game Pad, please refer to the project "Using IPhone or IPad and Micro:bit Game Pad App to Control an RC Car" throughout the step 2 to 12. In this previous project, we've assigned the Game Pad buttons A through D in the following ways:

  • Pressing button A -> The car going forward
  • Pressing button B -> The car going backward
  • Pressing button C -> The car turning left
  • Pressing button D -> The car turning right

For this project to turn on and off the LEDs, why don't we assign the Game Pad button 1 and 2 in the following ways:

  • Pressing button 1 -> Turning on the LED lights
  • Pressing button 2 -> Turning off the LED lights

STEP 3: Reviewing the MakeCode Sample File

Now let's examine how the block codes would work in this MakeCode sample file.

In the earlier step, you've downloaded the MakeCode sample file to your computer. Connect your computer and the micro:bit via USB cable. Open the MakeCode editor and click Import button. Open Receiver.hex file that you've copied to "receiver" micro:bit.

STEP 4: Adding Valenta Extensions to MakeCode Editor

To light up LEDs in this project, you'll need to add Valenta extensions to the MakeCode editor. They are easy-to-use predefined blocks of codes we've made especially for playing with Valenta Off-Roader.

Click Extensions in the bottom of the menu. Type https://github.com/4Tronix/Valenta within the search box, as shown in the attached photo. Click the Valenta extensions. Now the extensions are added in the menu. Let's use the following extensions in this project.

Board Model blocks
This block group is used primarily when you initially code on start block. In this project to light up LEDs on your Valenta Off-Roader, you'll use Valenta Zero motor controller included in the kit. You'll drag and drop select board model Zero block within on start block, so "receiver" micro:bit can recognize which motor controller will be used for this project.

FireLed blocks

If you look at Valenta Zero motor controller on the car, it has x4 LEDs numbered 0, 1, 2 and 3. This block group will light up these LEDs in many ways. For example, you can pick up your favorite color (or define any RGB color you like), set the lighting time and adjust its brightness based on your preference.

STEP 5: Reusing the "Old" Block Codes We've Made

In the previous steps, you've opened Receiver.hex file copied to "receiver" micro:bit in the MakeCode editor. Now let's look at each block codes in detail.

Please recap the previous project "Using IPhone or IPad and Micro:bit Game Pad App to Control an RC Car" step 16 through 23 for understanding the following block codes that have been used in the same way in this project.

The above block codes are used for driving the car forward, backward, turning left or right, when pressing the button A, B, C and D.

STEP 6: Developing the "New" Block Codes We'll Use

To light up the LEDs in this project, you'll add the following "new" block codes and use them together with the "old" block codes you've reviewed in the last step.

These "new" block codes are needed for this project to light up LEDs.

  • on start
  • function LED
  • on gamepad button 1 down
  • on gamepad button 2 down

Let's take a look at each block in the next few steps.

STEP 7: On Start

on start block

This block is called initially at once when "receiver" micro:bit is turned on.

The block bluetooth io pin service will allow other block codes to work with digital and analog pins via Bluetooth communication.

From the Functions extension, drag and drop call steer block inside on start block. It will call function steer block and the car's steering look straight ahead.

From the Valenta Board_Model extension, drag and drop select board model Zero block within on start block, so your "receiver" micro:bit can recognize that you'll use Valenta Zero motor controller for Valenta Off-Roader.

STEP 8: Function LED

This step is where you can define how LEDs would light up, and that's why it's the most important step of all in this project!

First, let's make a boolean variable light which has the value of 0 or 1. 0 means to turn off LEDs. 1 means to turn on LEDs. You'll use this variable to turn on and off LEDs. From the Variables extension, click Make a Variable button and create the variable light.

function LED block

From the Functions extension, click Make a Function button to create function LED. From the Loops extension, drag and drop while do block within function LED. From the Logic extension, drag and drop 0 = 0 block. From the Variables extension, drag and drop the boolean variable light to the left side of 0 = 0 block, so it looks just as light = 0 block. Also change the right side to 1, so it looks just as light = 1. Then, drag and drop light = 1 block onto the hex part of while do block within function LED. What it does is, when the boolean variable light is set to 1 to turn on LEDs, you'll keep implementing function LED in the loop.

Meanwhile, let's talk about the RGB color! The RGB color is the color to be made by mixing Red, Green, Blue in various ways. Each color can be represented numerically in the range from 0 to 255. You can express RGB color in the triplet (r, g, b) from (0, 0, 0) to (255, 255, 255).

In this project, why don't we let the computer pick up the random number from 0 to 255 for each Red, Green and Blue, put each value into the triplet and choose a totally unexpected RGB color for each LED numbered from 0, 1, 2 and 3 equipped on the motor controller!

In the Variables extension, click Make a Variable button to create a variable n1, n2 and n3 to keep three random numbers for a moment. From the Math extension, drag and drop pick random block and define the range from 0 to 255, so it looks just as pick random 0 to 255 and put this inside set n1 to block from the Variables extension, so it looks just as set n1 to pick random 0 to 255 and put this block within while do block. Repeat this process to create the blocks for n2 and n3.

From the Valenta FireLED extension, drag and drop convert from red 0 green 0 blue 0 block and replace each number with the variable n1, n2 and n3 from the Variables extension, so it looks just as convert from red n1, green n2 blue n3 block. This block can pick up a random number from 0 to 255 for each Red, Green and Blue, and these three colors would be mixed together to determine a totally unknown RGB color!

From the Valenta FireLED extension, drag and drop set LED at 0 to block and combine it with convert from red n1, green n2 blue n3 block, so it looks just as set LED at 0 to convert from red n1 green n2 blue n3 block. There are x4 LEDs numbered from 0, 1, 2 and 3, so repeat this process to create all the blocks for the LED 1, 2 and 3.

Finally, how long will you set up the duration for flashing LEDs? From the Basic extension, drag and drop pause (ms) block. 1000 (ms) is 1 second. Let's set the time as pause (ms) 500, for example. From the Valenta Fire LED extension, drag and drop clear all LEDs block within while do block, so all the LEDs will reset their colors every 0.5 seconds.

The next step is how you will assign which button on the Game Pad in the iOS app to switch the boolean value light from 0 to 1 (or 1 to 0) to turn on (or off) the LEDs.

STEP 9: On Gamepad Button 1, 2 Down

You can customize which button action would light on and off the LEDs and it's up to you totally.

On the Game Pad in the iOS app, how about pressing the button 1 lets the LEDs turn on, and pressing the button 2 lets the LEDs turn off?

Previously, you've made the boolean variable light which has the value of 0 or 1. 0 means to turn off LEDs. 1 means to turn on LEDs. You'll use this variable to turn on and off LEDs.

on gamepad button 1 down block

From the Devices extension, drag and drop on gamepad button 1 down block. From the Variables extension, drag and drop set light to 1, so the function LED would be implemented to turn on LEDs. From the Functions extension, drag and drop call LED block. All the LEDs would light up randomly.

on gamepad button 2 down block

From the Devices extension, drag and drop on gamepad button 2 down block. From the Variables extension, drag and drop set light to 0, so the function LED would be implemented to turn off LEDs. From the Functions extension, drag and drop call LED block. All the LEDs would be turned off.