Sample code review

Have you enjoyed MakeCode sample codes for playing with Valenta Off-Roader?

 

Now we will take a closer look at how these sample codes are made using MakeCode editor.

 

Once you get used to the sample codes, then start customizing your own codes using Valenta extensions and master the road!



Opening MakeCode editor

Let's open MakeCode editor. Click Import button and select the sample code you've downloaded in your computer.


Receiver.hex

Let's look at Receiver.hex that you've copied to your "receiver" micro:bit on the car. These codes control the car to go forward, backward, right or left whenever "receiver" micro:bit receives the signal (string) from "transmitter" micro:bit.


on start block

This on start block is called initially at once when you turn on "receiver" micro:bit. 

 

First, set radio group ("1" for example) to be the same number for both "transmitter" micro:bit and "receiver" micro:bit that you use as a pair, so they can communicate each other. 

 

Tips

If you want to play with multiple users, set the radio group number "2" for the second user and "3" for the third user and so on, so that each user will not conflict the radio number between micro:bits.

 

Then, drag and drop select the board model zero block from Valenta extensions, because the car uses the motor controller named Valenta Zero.

 

Also, call the function steer to let the car's steering look straight ahead.


function steer block

By default, the car's steering isn't always straight ahead because of the servo included. If you look at the car from above, the steering might be a little right or left. Thus, we have created a function steer to let the car's steering look straight ahead.

 

Can you find the variable centre? This variable is to set the servo's central angle.

 

By the way, the servo in this kit swings at the range 0 to 90 degrees and its central degree is 45 degrees in the middle. The servo in this kit will let the steering look straight ahead at the 45 degrees. Therefore, let's set centre to 45 degrees.

 

Tips

If you want to use your "own" micro servo (not the one in the kit) which has a different central degree, you might need to find its own central degree and set it in centre variable. For example, if your own micro servo swings at the range -90 to 90 degrees and if its central degree is 0 degrees in the middle, then set centre to 0 degrees by yourself.

 

So what if the car's steering is a little right or left? Let's offset the servo angle a little so its steering looks straight ahead.

 

For example, if you look at your car from above and if its steering is a little left, then offset -5 degrees to the right. Because we use Pin 8 (P8) for the servo on the board, define the block such as offset servo 8 by -5 degrees here.

 

If your car's steering is a little right, then offset 5 degrees to the left. Because we use Pin 8 (P8) for the servo on the board, define the block such as offset servo 8 by 5 degrees here.

 

The last block in the function is to set servo 8 to angle centre by which the servo swings to the centre angle you've defined earlier and the car look straight ahead!


on radio received receivedString block

This is the event-driven block that responds to the string sent from "transmitter" micro:bit. For example, when you tilt "transmitter" micro:bit to the right, it will send the string goRight to the car. Whenever "receiver" micro:bit receives this string, this block will call function goRight and the car will turn right.



function goForward block

When this function is called, it will further call function steer to let the car's steering look straight ahead. Then, the car will go forward at speed 100 for 1000 millisecond (1 second) and stop. You can customize the speed (0 to 100 max) and the time (500 milliseconds, for example) here.


function goBackward block

When this function is called, it will further call function steer to let the car's steering look straight ahead. Then, the car will go backward at speed 100 for 1000 millisecond (1 second) and stop. You can customize the speed (0 to 100 max) and the time (500 milliseconds, for example) here.


function goRight block

When this function is called, the car will turn right. Because the servo uses Pin 8 (P8) on the board, set servo 8 to angle centre -10, meaning that the servo swings to its central angle -10 degrees to the right.

 

Then, the car will go forward at speed 100 for 1000 millisecond (1 second) and stop. You can customize the speed (0 to 100 max) and the time (500 milliseconds, for example) here. 

 

Finally, function steer is called to let the car's steering look straight ahead.


function goLeft block

When this function is called, the car will turn left. Because the servo uses Pin 8 (P8) on the board, set servo 8 to angle centre +10, meaning that the servo swings to its central angle +10 degrees to the left.

 

Then, the car will go forward at speed 100 for 1000 millisecond (1 second) and stop. You can customize the speed (0 to 100 max) and the time (500 milliseconds, for example) here. 

 

Finally, function steer is called to let the car's steering look straight ahead.


Transmitter.hex

Let's look at Transmitter.hex that you've copied to "transmitter" micro:bit.

These sample codes send the signal (string) for controlling the car to drive forward, backward, turning right or left. Using "transmitter" micro:bit's Accelerometer features of down, up, right and left, you can send these signals (strings) to "receiver" micro:bit.


on start block

This on start block is called initially at once when you turn on "transmitter" micro:bit. Then, set radio group ("1" for example) to be the same number for both "transmitter" micro:bit and "receiver" micro:bit that you use as a pair, so they can communicate each other. 

 

on logo down block

This is the event-driven block that responds to the action when you tilt down "transmitter" micro:bit. Then, "transmitter" micro:bit sends the string "goForward" over the radio. When "receiver" micro:bit receives this string, the car will go forward.

 

on logo up block

This is the event-driven block that responds to the action when you tilt up "transmitter" micro:bit. Then, "transmitter" micro:bit sends the string "goBackward" over the radio. When "receiver" micro:bit receives this string, the car will go backward.

 

on tilt right block

This is the event-driven block that responds to the action when you tilt "transmitter" micro:bit to the right. Then, "transmitter" micro:bit sends the string "goRight" over the radio. When "receiver" micro:bit receives this string, the car will turn right.

 

on tilt left block

This is the event-driven block that responds to the action when you tilt "transmitter" micro:bit to the left. Then, "transmitter" micro:bit sends the string "goLeft" over the radio. When "receiver" micro:bit receives this string, the car will turn left.