Tutorial for MakeCode sample codes

Have you downloaded the MakeCode sample codes for Valenta Off-Roader? In this tutorial web page, you can learn about how and why these sample codes are programmed. When you understand them better, of if you have a new idea, you could customize it and program your own code!

 



Receiver.hex file

Below are several blocks of codes in the Receiver.hex file, and we will explain each blocks individually.

on start block

This block is called initially at once when we turn on the receiver micro:bit. We will set the radio group to be the same number "1" for the transmitter micro:bit and the receiver micro:bit, so they can send signals each other.

 

By default, the steering is not straight because of the servo included. If you look at the car from above, the steering is too right or too left.

 

If the steering is not straight, please open the Receiver.hex file at makecode.microbit.org and try one of these adjustments before playing:

  • If you think the steering is too right, adjust it to the left by 5 degrees by entering "5" in set adjustLeft block.
  • If you think the steering is too left, adjust it to the right by 5 degrees by entering "5" in set adjustRight block.

In this sample Receiver.hex file, we entered 5 degrees to set adjustLeft block, because we thought that the steering was too right by the servo included.

 

The servo included can be operated from 0 (the most right) to 90 (straight) to 180 (the most left) degrees. Ideally, the servo's neutral position must be exactly 90 degrees for the car to go straight. Practically, the servo's neutral position must be adjusted by the degree in either set adjustLeft or set adjustRight block.

servo function

Whenever this function is called, the servo is being shifted to its neutral position, which is the 90 degrees, adjusted by the degrees you have entered in either set adjustLeft or set adjustRight block within on start block. The steering is supposed to look straight.

 

forward function

Pin 13 and 15 are used to set the motors' direction. When they are set to 0, the car will run forward. Pin 12 and 14 are used to set the motors' speed from 0 to 1023. When they are set to 1023, the car will run at its max speed. If this function is called, the car will run forward at its max speed for 1 second and will stop.

backward function

Pin 12 and 14 are used to set the motors' direction. When they are set to 0, the car will run backward. Pin 13 and 15 are used to set the motors' speed from 0 to 1023. When they are set to 1023, the car will run at its max speed. If this function is called, the car will run backward at its max speed for 1 second and will stop.

LED-ON function

Pin 1 and 2 are used to turn on LEDs. When they are set to 1, the LEDs will turn on.

LED-OFF function

Pin 1 and 2 are used to turn off LEDs. When they are set to 0, the LEDs will turn off.

on radio received receivedString block

This is the event-driven block that responds to the signal from the transmitter micro:bit. When the receiver micro:bit receives a signal from the transmitter micro:bit, this block will call one of these functions.

  • goForward function
  • goBackward function
  • goLeft function
  • goRight function

goForward function

When this function is called, it will further call the following functions together.

  • servo function ... to set the steering straight
  • LED-ON function ... to turn on LEDs
  • forward function ... to let the car run forward for 1 second
  • LED-OFF function ... to turn off LEDs

goBackward function

When this function is called, it will further call the following functions together.

  • servo function ... to set the steering straight
  • LED-ON function ... to turn on LEDs
  • backward function ... to let the car run backward for 1 second
  • LED-OFF function ... to turn off LEDs

goLeft function

When this function is called, it will further call the following functions together.

  • servo write pin 8 to neutral + 10 block ... to turn the steering to the left by 10 degrees
  • LED-ON function ... to turn on LEDs
  • forward function ... to let the car run forward for 1 second
  • LED-OFF function ... to turn off LEDs
  • servo function ... to set the steering straight

goRight function

When this function is called, it will further call the following functions together.

  • servo write pin 8 to neutral - 10 block ... to turn the steering to the right by 10 degrees
  • LED-ON function ... to turn on LEDs
  • forward function ... to let the car run forward for 1 second
  • LED-OFF function ... to turn off LEDs
  • servo function ... to set the steering straight

Transmitter.hex file

Below are several blocks of codes in the Transmitter.hex file, and we will explain each blocks individually.

on start block

This block is called initially at once when we turn on the transmitter micro:bit. We will set the radio group to be the same number "1" for the transmitter micro:bit and the receiver micro:bit, so they can send signals each other.

 

on button A pressed block

This is the event-driven block that is triggered when the button A is pressed on the transmitter micro:bit. The transmitter micro:bit will send the signal goLeft to the receiver micro:bit. The receiver micro:bit will call the goLeft function.

 

on button B pressed block

This is the event-driven block that is triggered when the button B is pressed on the transmitter micro:bit. The transmitter micro:bit will send the signal goRight to the receiver micro:bit. The receiver micro:bit will call the goRight function.

 

on button A+B pressed block

This is the event-driven block that is triggered when the button A and B are pressed together on the transmitter micro:bit. The transmitter micro:bit will send the signal goForward to the receiver micro:bit. The receiver micro:bit will call the goForward function.


BitCommander.hex file

If you have 4Tronix Bit:Commander to be used with the transmitter micro:bit, you can transfer the BitCommander.hex file to the transmitter micro:bit and plug it in to the BitCommander. We will explain the blocks of codes in the BitCommander.hex file individually.

on start block

This block is called initially at once when we turn on the Bit:Commander. We will set the radio group to be the same number "1" for the transmitter micro:bit and the receiver micro:bit, so they can send signals each other.

 

on yellow down block

This is the event-driven block that is triggered when the yellow button is pressed on the Bit:Commander. It will send the signal goLeft to the receiver micro:bit. The receiver micro:bit will call the goLeft function.

 

on blue down block

This is the event-driven block that is triggered when the blue button is pressed on the Bit:Commander. It will send the signal goRight to the receiver micro:bit. The receiver micro:bit will call the goRight function.

 

on red down block

This is the event-driven block that is triggered when the red button is pressed on the Bit:Commander. It will send the signal goForward to the receiver micro:bit. The receiver micro:bit will call the goForward function.

 

on green down block

This is the event-driven block that is triggered when the green button is pressed on the Bit:Commander. It will send the signal goBackward to the receiver micro:bit. The receiver micro:bit will call the goBackward function.