

LOWER SECONDARY

COMPUTER SCIENCE

mBot

40MINS
Hardware requirements:
mBot/mCore Control Panel
Implementation:
Upload the program to Arduino
Effect demo
Example programs
Script description
![]() |
The ranges of the random numbers are controlled by setting its upper and lower limits. |
![]() |
By replacing the color value with a random number, you can make the on-board LED randomly change its color. |
![]() |
The color changing frequency of the LEDs is controlled by the waiting time. |
Knowledge Points
Use of the random number block
The random number means that the number generated each time is uncertain and has no relationship with the one generated last time. For example, the result of rolling a dice can be considered as random since the outcome is between integer 1-6.
Several tips in using the random number block
1) Define the range of the random number.
Define the two ends of the range of the random number. The range can start from a smaller number to a larger number, or vice versa. Namely,
is equivalent to
.
- Double-click on
to enter the range directly, e.g.,
,Other than using integers, a decimal or negative number is also acceptable.
2)Display the random value. By clicking on the block with the mouse, the random value will be shown at the upper right corner. For example, 、
3)Random integer and random decimal. When the two both contain integers, an integer will be selected randomly, but if there’s a decimal in at least one
, a random decimal will be generated. For example,
In this sample, random numbers are selected as the values of the three-primary colors, red, green and blue. In this way, red, green, and blue colors are combined together randomly, producing a random color for the LED light.
Since LEDs do not accept decimals as its brightness, only integers are used as the random numbers in this sample.
Extended tasks
Task 1
Do not change the green and blue values, choose a random number for red, and observe the light effect.
Task 2
Change the script to allow the left and right LEDs on board to show different colors.
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.