Implementation 2
Please design an mBot whose “line-follower sensor” can handle four different scenarios with a
return value (0 ~ 3) and adjust mBot so that it travels along the black line.
Draw a flow chart
Starts the robot
Detected return value
Return value=0? Return value=1? Return value=2? Return value=3?
True True True Status=1? Status=2?
Detects repeatedly Status=1 Status=2
Runs forward Turns left slightly Turns right slightly Turns right broadly Turns left broadly
mBlock program
Chapter 7 Robotic Tracking Vehicle (Line-Follower Sensor) 151
Implementation 3
Pertaining to the previous question, and add the following two functions:
1. It will only start when the user presses the “button”.
2. It will only stop when the light sensor detects “dark light”.
mBlock program
152 Scratch (mBlock) Program Design — Using mBot Robot
7-7
Robot Stops when Detecting Third Black Line
As described earlier, although the robot's tracking car is able to use the "line-follower sensor" to run
forward along the black line, it is unable to record the journey during the process of walking. Therefore,
this unit will describe that how the mBot uses the "line-follower sensor" to stop when it detects the
third black line. Shown in the below schematic diagram:
mBot stops
Flow chart mBlock program
When double-clicking the
“mBot program”
The judgment button
is pressed
Count=0
Return Value=Line-follower
sensor Return Value
Count<3
False True
mBot stops Runs forward
Return Value=0
True
Count=count+1
Chapter 7 Robotic Tracking Vehicle (Line-Follower Sensor) 153
7-8
Tracking Robot That Plans to Automatically Park
Rule of the black line on-site
As the two “IR sensors” within the line-follower sensor are 1.2 cm apart, therefore the black line in
this topic has a limit of 1cm.
Implementation
The robot is parked in a designated “parking grid”. Assuming the robot wants to stop at the first
“parking grid”. The program then also has to judge if the variable is 6 while tracking and if so, the robot
will have its body “turn right 90 degrees” to enter the lot.
parking grid 1 parking grid 2 parking grid 3 Starting point
parking grid 4 parking grid 5 parking grid 6
Method — Using “four branches” to determine each scenario
1. When the robot's “left and right are both white”. That is, sense of light from both left and right does
not detect any black line, thus “go straight”.
2. When the robot's “left is white, and right is black”. That is, the sense of light on the right detects a
black line (robot leans to the left), thus you must turn right (small adjustment).
3. When the robot's “left is black, and right is white”. That is, the sense of light on the left detects a
black line (robot leans to the right), thus you must turn left (small adjustment).
4. When the robot's “left and right are both black”. That is, sense of light from both left and right
detects a black line so it will only turn black at an intersection or at the destination. You can thus
change the action according to the purpose.
Branch 1 Branch 2 Branch 3 Branch 4
Go straight Assigned action
Turns right Turns left
(small adjustment) (small adjustment)
White White White Black Black White Black Black
Solution
To store together with “variables”, and when encountering an “intersection”, the variable is increased
by one.
154 Scratch (mBlock) Program Design — Using mBot Robot
Chapter
7 Review questions
Short Answer Questions
1. Please design an mBot that, while walking, if it leans to the right, it must turn left
and issue a “Do sound”. On the contrary, if it leans to the left, it must turn right and
issue a “Re sound”. That is, there will be warning sound when the robot’s tracking
car deviates.
2. Pertaining to the above question, please add a start function by adding a “button”.
That is, the tracking will only start when the user presses the “button” on the
“mCore controller”.
3. Pertaining to the above question, please add a “dark light” switch to control mBot
walking. That is, the tracking stops when the user's hand covers the “light sensor”
on the "mCore controller”.
4. This chapter introduces the robot’s tracking car, and at the same time, will go
straight when a “black line” is detected. However, this method may not work well
in a more complicated route map. Therefore, please modify such that it will only go
straight when the “left is black, and right is white” is detected.
Illustration
480cm
120cm End
Starts Scenario
Scenario Sensor1 Sensor2 Return value Adjustment action
(left side) (right side) 0 Turns right(spins)
On the black line Detects Detects color 1 Runs forward
Leans 2 Turns left(spins)
Black Black 3 Turns right
to the right side
Leans Black White
to the left side White Black
Deviates from the
White White
black line
Chapter 7 Robotic Tracking Vehicle (Line-Follower Sensor) 155
156 Scratch (mBlock) Program Design — Using mBot Robot
Chapter 8 CHAPTER OBJECTIVES
Remote-Controlled • To let the reader understand the
Robot principle of the transmitter and
receiver ends of the mBot “IR
(Infrared Sensor) sensor.
• To let the reader understand the
related applications of the mBot “IR
sensor”.
CHAPTER OUTLINE
8-1 Understanding Infrared Sensor
8-2
Determining Value of Infrared
8-3 Sensor
8-4
Infrared Sensor of Wait Module
8-5
8-6 Infrared Sensor of Switch
Module
8-7
Infrared Sensor of Loop Module
Remote-Controlling mBot
Movement
Sending Messages Between 2
mBots
8-1
Understanding Infrared Sensor
Definition
Used to transmit and receive messages to other robots or devices.
Classification
1. IR Transmitting: is used to send an mBot message to another mBot receiver.
2. IR Receiver: is used to receive another mBot’s transmitted message or an “IR remote control”
signal.
Exterior diagram mBot controller (Actual photo)
mBote controller (Flat diagram)
AA Battery 3.7V Lithium RGB LED RJ 25 port
holder interface battery interface
AA Battery 3.7V Lithium
Rehsoeldter interface battery interface
USB coRnenseect tor
USB connector Power switch RGB LED BuRzJz2e5r port
RJ 25 port Power swMitcohtor interface Buzzer
RJ 25 port
RGRBGLBEDLED Motor interface IR Receiver
IR ReLceigivhetrsensor
BuBzzuezrzer RJ 25 port Light sensor
IRIReRceicveriver RJ 25 port
IRIRTrTanrasnmsitmteirtter RGB LEDRGB LED IR TransmitteIrR Transmitter
Button Button
Light senLsiogrht sensor Button Button Motor interface
Motor interface
Purpose Controlling the robot
(Use the IR receiver to receive messages)
mBot remote control
(The front has a transparent small bulb (IR
transmitter))
158 Scratch (mBlock) Program Design — Using mBot Robot
mBot controller - Illustration
The second mode:
Avoid self-propelled car
The rst mode: The third mode:
Remote control car Tracking self-propelled car
Go forward Go right
Go left Go backward
Adjust speed of
self-propelled car
Description
1. The first mode: Use the “arrow keys” on the remote control to control the mBot’s walking
direction, and with the “number keys” to adjust the walking speed.
2. The second mode: The mBot when walking forward uses the “ultrasonic sensor” to detect if
there is an obstacle. If there is, it will automatically avoid the obstacle, if there isn’t, it will run
forward.
3. The third mode: mBot walks along the pre-set “black line or white line” using the "line-follower
sensor".
First mode Second mode Third mode
Application
1. Control an mBot’s action remotely.
2. Two mBot sending messages to each other.
3. Controlling the robots to march as a brigade remotely.
Chapter 8 Remote-Controlled Robot (Infrared Sensor) 159
8-2
Determining Value of Infrared Sensor
Detects messages sent by the “remote control” in the mBlock program development
environment.
mBlock program The type of messages sent by the IR remote control
Test the message sent by the “remote control” Press “B” on the remote control
Press “A” on the remote control
Test Results The message “B” is displayed
The message “A” is displayed
回傳值= A 回傳值= B
The range of messages sent
Basically, the English words “A ~ F” on the “remote control”, the four direction keys and the
numbers 0 ~ 9 are the messages sent.
160 Scratch (mBlock) Program Design — Using mBot Robot
8-2.1 Messages sent by remote control (control 2 LEDs)
In the previous unit, we have learned how to detect the value of the IR sensor. Next, we will learn
how to use the “remote control” to send messages (control 2 LEDs).
Implementation
Please write an mBlock program to control the “traffic lights” remotely. That is, Led1 will light up in
red when the “A” key is pressed, while Led2 will light up in green when the “B” key is pressed.
Flow chart
When double-clicking the
“mBot program”
Press “A” on True “Red” light in
IR remote control True LED1 lights up
False “Green” light in
LED2 lights up
Press “B” on
IR remote control
mBlock program
Chapter 8 Remote-Controlled Robot (Infrared Sensor) 161
8-2.2 Remote control to send messages (control 2 LEDs flashing)
In this unit, we will learn how to use the “remote control” to send messages (control 2 LEDs
flashing).
Implementation
Bearing to the previous unit, please add another function, which is, by pressing the “C” key, Led1
and Led2 will flash 3 times.
Flow chart
When double-clicking the
“mBot program”
Press “A” on True “Red” light in
IR remote control LED1 lights up
False True “Green” light in
Press “B” on LED2 lights up
IR remote control
False
Press “C” on
IR remote control
Repeats 3 times “Red” light in LED1 lights up
“Green” light in LED2 lights up
Wait for 0.5 seconds
LED1 does not light up
LED2 does not light up
Wait for 0.5 seconds
162 Scratch (mBlock) Program Design — Using mBot Robot
mBlock program
Three common methods of using the IR sensor
The IR sensor in mBlock is often used by the following three functional Blocks.
Wait
Loop
Switch
Chapter 8 Remote-Controlled Robot (Infrared Sensor) 163
8-3
Infrared Sensor of Wait Module
Function
It is set to implement the next action only when the “IR sensor” detected value is equivalent to a
certain” message”.
Wait
Detected value Returned message
Example
The robot runs forward and only stops when the “IR sensor” detects a message that is equal to “A”.
Please use Wait. [Please use Wait]
Answer
Flow chart mBlock program
When double-clicking the
“mBot program”
The robot runs forward
False Detects “A”
being pressed?
True
mBot stops
164 Scratch (mBlock) Program Design — Using mBot Robot
8-4
Infrared Sensor of Switch Module
Definition
It is used to determine if the “IR sensor” detected value is equal to a “message”. If “yes”, it implements
the branch “above”, otherwise the branch “below” is implemented.
Switch Detected value Message
1
2
Description
1. When the conditional is “true”, the branch “above” is implemented.
2. When the conditional is “false”, the branch “below” is implemented.
Example
The robot runs forward and only stops when the “IR sensor” detects a message that is equal to “A”.
[Please use Switch].
Answer
Flow chart mBlock program
When double-clicking the
“mBot program”
Message = 0
Detects “A” being pressed?
True
Message = A
Message = A
True False
mBot stops runs forward
Chapter 8 Remote-Controlled Robot (Infrared Sensor) 165
8-5
Infrared Sensor of Loop Module
Definition
It ends Loop when the “IR sensor” detected value is equivalent to a certain “message”.
Loop
Detected value Threshold value
Example
The robot runs forward and only stops when the “IR sensor” detects a message that is equal to “A”.
[Please use Loop].
Answer
Flow chart mBlock program
When double-clicking the
“mBot program”
The robot runs forward
False
Detects “A” being pressed?
True
mBot stops
166 Scratch (mBlock) Program Design — Using mBot Robot
8-6
Remote-Controlling mBot Movement
In fact mBot, in the original publication, and then through the “connection / Reset Default
Program” in the mBlock language, allows children or their parents to operate the robot through the “IR
remote control”, and also to switch to self-propelled vehicles. For example: remote control cars,
obstacles-avoiding cars and tracking cars.
In this unit, I will introduce, amongst them, on how to write an mBlock program to “control mBot
remotely”.
mBot remote control – Illustration
Second mode:
Obstacles-avoiding self-propelled cars
First mode: Third mode:
Remote control cars Self-propelled tracking cars
Faces forward Faces right
Faces left Faces backward
Adjust speed of
self-propelled car
Pertaining to the first mode in the diagram above, the “arrow keys” on the remote control are used
to control the direction of the mBot walking. The “number keys” are used together to adjust its walking
speed.
Chapter 8 Remote-Controlled Robot (Infrared Sensor) 167
Answer Flow chart
When double-clicking the De nition Change
“mBot program” power subroutine
Power = 160
“1” is pressed True Power = 100
on the remote control
“↑” is pressed on True Forward “2” is pressed True Power = 150
the remote control speed Power on the remote control
False True Backward False True Power = 200
“↓” is pressed on speed Power “3” is pressed
the remote control on the remote control
True Left turn
False speed Power False True Power = 255
“←” is pressed on “4” is pressed
the remote control True Right turn on the remote control
speed Power
False mBot stops
“→” is pressed on
the remote control
False
Change power
mBlock program
168 Scratch (mBlock) Program Design — Using mBot Robot
Results of implementation Controlled robot
(Using IR Receiver to receive messages)
mBot remote control
(There’s a transparent bulb at the front (IR
Transmitter))
Chapter 8 Remote-Controlled Robot (Infrared Sensor) 169
8-7
Sending Messages Between 2 mBots
An mBot or the addition of an IR remote control is constantly used in the previous unit.
In this unit, we will use the (IR Transmitter and Receiver) in the mainboard of every mBot for the
transmission of messages.
IR remote control Receives “remote control” and “Receives” messages from
“transmit” messages another mBot
Implementation)
Assume there are two mBots and an IR remote control.
Now you want to use the “IR remote control” to first send a signal to robot A, and after robot A’s
(transmitter) receives the signal from the remote control, will then send to robot B’s (receiver) to
control its actions.
Answer
Flow chart of the “Transmitter”
When double-clicking the
“mBot program”
“↑” is pressed True Send mBot message 1
on the remote control True
False Send mBot message 2
“↓” is pressed True Send mBot message 3
on the remote control True Send mBot message 4
False
“←” is pressed
on the remote control
False
“→” is pressed
on the remote control
170 Scratch (mBlock) Program Design — Using mBot Robot
Flow chart of the “Transmitter”
When double-clicking the
“mBot program”
Send mBot message 1 True Runs forward 2 seconds
True Runs backward 2 seconds
False True Turns left 2 seconds
Send mBot message 2 True
False Turns right 2 seconds
Send mBot message 3
False
Send mBot message 4
Transmitter Receiver
(To upload the program to mBot’s (To upload the program to mBot’s “Receiver”)
“Transmitter”)
Chapter 8 Remote-Controlled Robot (Infrared Sensor) 171
Chapter
8 Review questions
Short Answer Questions
1. Infrared light relay race _ straight line
Please prepare two mBots; one as the “transmitter”, and the other as the
“receiver”. When the robot, who is the “transmitter” gets to about 10cm
close to a robot, who is the “receiver”, whilst walking in a “straight line”, it
will send a signal to the “receiver” through infrared light. When the “receiver”
receives the signal, it will then run forward in a “straight line” so as to achieve
the purpose of the robot relay race.
2. Infrared light relay race_ follows track
Please prepare two mBots; one as the “transmitter”, and the other as the “receiver”.
When the robot, who is the “transmitter” gets to about 10cm close to a robot, who
is the “receiver”, whilst walking in a “circle”, it will send a signal to the “receiver”
through infrared light. When the “receiver” receives the signal, it will then run
forward in a “circle” so as to achieve the purpose of the robot relay race.
172 Scratch (mBlock) Program Design — Using mBot Robot
Chapter 9 CHAPTER OBJECTIVES
Robotic Solar • To let the reader understand the
Powered Vehicle principle of the transmitter and
(Light Sensor) receiver of the mBot’s “light sensor”
• To let the reader understand related
applications of the mBot’s “light
sensor”.
CHAPTER OUTLINE
9-1 Understanding Light Sensor
9-2
Determining Value of Light
9-3 Sensor
9-4
9-5 Light Sensor of Wait Module
9-6
Light Sensor of Switch Module
9-7
Light Sensor of Loop Module
9-8
Light Sensor Controlling Other
9-9 Block Modules
Making a Robotic Solar Powered
Vehicle
Making a Robotic Cockroach
Vehicle
Making a Smart Streetlamp
9-1
Sending Messages Between 2 mBots
Definition
It is used to detect the intensity of light in the environment.
Purpose
You can use the different light values in the surroundings to make the robot perform different
actions.
Exterior diagram
mBote controller (Flat diagram) mBot controller (Actual photo)
AA Battery 3.7V Lithium RGB LED RJ 25 port
holder interface battery interface
RGB LED RJ 25 port
AA Battery 3.7V Lithium Buzzer
holder interface battery interface Buzzer
Reset
Power switch
Reset MotPoor winetresrwfaictceh
USB connector
Motor interface
USB connector
RJ 25 poRrtJ 25 port RJ 2R5J p2o5rptort IR Receiver
RGB LEDRGB LED LLigighhttsseeInnRssRoorreceiver
RGBRGLBEDLED
Buzzer Buzzer IRIRTTrraannssmmiitttteerr MoMtoor tinotreirnfatceerface
IR ReceivIRerReceiver ButtBountton BBuutttotonn
IR TransmIRitTterarnsmitter LighLtigshetnsseonrsor
Exterior
The light sensor is located in the second row of the front part of the mBot.
Application
1. Create a robotic solar car.
2. Create a robotic cockroach car.
3. Create an intelligent streetlight (day (bright) → LED off, night (dark) → LED on).
174 Scratch (mBlock) Program Design — Using mBot Robot
9-2
Determining Value of Light sensor
Detecting “Light Values” in the mBlock program development environment
Test the value of the light source The hand moves up slowly
Detected light value (higher)
Put your hands about 1 cm above the light sensor
Test Results
Detected light value (lower)
Return value= 82 Return value= 994
Range of sensing value
Basically, the light sensors sense value has a range of 0 to 1023.
1. “Indoor” natural light: between 0 and 1000 (the greater the value, the brighter it is).
2. "Outdoor" natural light: exceeds 1000 (also possible if using a flashlight to shine “indoor”)
Three common uses of light sensor
The light sensor in mBlock is often used by the following three functional Blocks.
Wait
Loop
Switch
Chapter 9 Robotic Solar Powered Vehicle (Light Sensor) 175
9-3
Light sensor of Wait Module
Function
It is set to implement the next action only when the “light sensor” detects that the value is less than the
“threshold value”.
Wait
Detected value Threshold value
Example
The robot runs forward and will only stop when the “light sensor” detects a light value less than “500”.
[Please use Wait].
Flow chart mBlock program
When double-clicking the
“mBot program”
The robot runs forward
False
Detected light value<500
True
mBot stops
176 Scratch (mBlock) Program Design — Using mBot Robot
9-4
Light sensor of Switch Module
Definition
It is used to determine whether the reflected light detected by the “light sensor” is less than the
“threshold value”. If “yes”, the branch “above” will be implemented. Otherwise the branch “below” will
be implemented.
Switch
Detected value Threshold value
1
2
Explanation
1. When the conditional is “true”, the branch “above” is implemented.
2. When the conditional expression is “false”, the branch “below” is implemented.
Example
The robot runs forward and will only stop when the “light sensor” detects a light value less than “500”.
[Please use Switch].
Answer
Flow chart mBlock program
When double-clicking the
“mBot program”
Light Value = 0
Light Value = Detected light value
True Detected light value<500 False
mBot stops mBot runs forward
Chapter 9 Robotic Solar Powered Vehicle (Light Sensor) 177
9-5
Light sensor of Loop Module
Definition
Used to end Loop when the “light sensor” detects a light value that is less than the “threshold value”.
Loop
Detected value Threshold value
Example
The robot runs forward and stops when the “light sensor” detects a light value less than “500”. [Please
use Loop].
Flow chart mBlock program
When double-clicking the
“mBot program”
The robot runs forward
False
Detected light value<500
True
mBot stops
178 Scratch (mBlock) Program Design — Using mBot Robot
9-6
Light sensor Controlling Other Puzzle Modules
Assuming we have assembled a robot and want the speed of its movement to go according to the
different light value. At this point, we must use the “light sensor” to detect the “light value” of the
different light sources, and then have the “numerical data of the light value” send to the other rotational
speed of the “motor”.
Example
The light source controls the speed of the motor. After the “light sensor” detects and sends out the
reflected light intensity, it is, through the “division” operator of the “Operators Block” box, divided by
(3.92). The output is then sent to the motor as its “rotational speed” input.
mBlock program
Explanation
1. “Motor rotational speed” ranges from absolute values 0 ~ 255.
2. “Light sensor” ranges from 0 ~ 1023.
3. Therefore, 1023/255 ≒ 4 must be divided when changing the value of the light sensor into the
motor rotational speed.
Chapter 9 Robotic Solar Powered Vehicle (Light Sensor) 179
9-7
Making a Robotic Solar Powered Vehicle
Rules for a solar car
1. The robot begins to run forward in a straight line when light shines at the robot.
2. The robot stops when the light source is removed.
Site Requirements
Use your mobile phone’s “flashlight” or a traditional flashlight.
Flow chart
When double-clicking the
“mBot program”
Light Value = 0
Light Value = Detected light value
True Light Value> 1000 False
mBot stops mBot runs forward
Implementation
Please write an mBlock program to simulate a “solar car”. That is, when there is light shining on it, it
will automatically run forward, otherwise it stops.
mBlock program
180 Scratch (mBlock) Program Design — Using mBot Robot
9-8
Making a Robotic Cockroach Vehicle
Rules for a cockroach car
1. The robot does not move when light shines on the robot.
2. The robot runs forward in a straight line when the light source is removed.
Site requirements
Use your mobile phone's “flashlight” or a traditional flashlight.
Flow chart
When double-clicking the
“mBot program”
Light Value = 0
Light Value = Detected light value
True Light Value> 1000 False
mBot stops mBot runs forward
Implementation
Please write an mBlock program to simulate a “solar car”. That is, when there is light shining on it, it
will automatically run forward, otherwise it stops.
mBlock program
Chapter 9 Robotic Solar Powered Vehicle (Light Sensor) 181
9-9
Making a Smart Streetlamp
Thinking of the theme
Daytime (bright) → LED off, Night time (dark) → LED on
Method
Use two LED lights to simulate the intelligent streetlight.
Flow chart
When double-clicking the
“mBot program”
Light Value = 0
Light Value = Detected light value
True Light Value> 500 False
LED light off LED light on
Implementation
Please write an mBlock program to simulate the “intelligent street light”. That is, when there is light
shining on it, it will automatically turn the street light off. Otherwise it will turn the street light on.
mBlock program
182 Scratch (mBlock) Program Design — Using mBot Robot
Chapter
9 Review questions
Short Answer Questions
1. Please use the intensity of the light source to determine the tone of the buzzer.
Tone of mBlock
Scale C4 D4 E4 F4 G4 A4 B4
Mid tone 262 294 330 349 392 440 494
frequency
Scale C5 D5 E5 F5 G5 A5 B5
High tone 523 587 659 698 784 880 988
frequency
Piano notes Do Re Mi Fa So Ra Si
2. Please use the intensity of the light source to sound the piano notes, Do, Re, ...,
Si.(Seven notes to match the table)
Scale C4 Tone of mBlock
D4 E4 F4 G4 A4 B4
Piano notes Do Re Mi Fa So Ra Si
151~300 301~450 451~600 601~750 751~900 901~1050
Light values 0~150
Chapter 9 Robotic Solar Powered Vehicle (Light Sensor) 183
184 Scratch (mBlock) Program Design — Using mBot Robot
Chapter 10
CHAPTER OBJECTIVES
• To let the reader understand the
functions and principles of the
mBot’s button, buzzer, LED lights
and reset button.
• To let the reader understand the
various applications of the mBot’s
button, buzzer, LED lights and reset
button.
CHAPTER OUTLINE
10-1 Button
10-2 Detecting Incidents with [Button]
10-3 General Usage of Button
10-4 Buzzer
10-5 LED Light
10-6 Reset Button
Robotic Police Car
(Button_ Buzzer _
LED Light)
10-1
Button
Definition
Pressing the button in the mBot will command it to start implementing instructions.
Features
It is generally used to start the mBot program.
Example
When the user presses the “button”, it starts to execute the line-follower.
Exterior diagram
Application
1. Starts the mBot program.
2. Counter.
186 Scratch (mBlock) Program Design — Using mBot Robot
10-2
Detecting Incidents with [Button]
The event of detecting the “button” in the mBlock programming language
Testing the distance Hand “releases” button
Hand “releases” button
“Press” the button with your hand
Testing the distance
“Press” the button with your hand
Return Value Return Value
Testing the distance
The button in mBlock is often used in the following three functional Blocks.
Wait
Loop
Switch
Chapter 10 Robotic Police Car (Button_ Buzzer _LED Light) 187
10-3
General Usage of Button
In this unit, we will introduce using the “button” to design the various uses in your daily life.
Commonly seen as follows:
1. Hand presses the alarm. (Use Wait).
2. Hand presses the counter. (Use Switch).
3. Hand presses the alarm and warning light. (Use Loop).
10-3.1 Hand Presses the Alarm
When the user presses the “button”, then a “beep” sounds. (Use Wait).
Flow chart mBlock program
When double-clicking the
“mBot program”
If the button is pressed
True
“Beep” sounds
When the user presses the “button”, the counter is automatically increased by 1 in the Stage area.
(Use Switch).
10-3.2 Hand Presses the Counter
Flow chart mBlock program
When double-clicking the
“mBot program”
Counter = 0
If the button is pressed
True
Counter = Counter +1
188 Scratch (mBlock) Program Design — Using mBot Robot
10-3.3 Hand Presses the Alarm
When the user presses the “button”, an “alarm” will sound and a “warning light” is lit. (Use Loop).
Flow chart
When double-clicking the
“mBot program”
False If the button is pressed True
“Alarm” is sounded “Warning light”
“Warning light” lights up doesn’t light up
mBlock program
Chapter 10 Robotic Police Car (Button_ Buzzer _LED Light) 189
10-4
Buzzer
Definition
In an mBot, the buzzer can be said to be its “mouth”.
Features
Different frequency sounds are emitted according to the situations.
Example
When we “boot” or “reset”, “three beeps” will sound so that users understand the current situation.
Exterior diagram
Seven notes to match the table
Tone of mBlock
Scale C4 D4 E4 F4 G4 A4 B4
Mid range tone 262 294 330 349 392 440 494
Piano notes Do Re Mi Fa So Ra Si
190 Scratch (mBlock) Program Design — Using mBot Robot
10-4.1 The buzzer sounds the “Little star” music
Once you understand how the seven notes match the table, we can now use it to design a variety of
music.
Implementation
Little star 1155665 4433221 5544332 5544332 1155665 4433221
Explanation
Notation 1 represents Do on the mobile phone screen, 2 represents Re ~ 6 represents Ra, 7
represents Si, space represents stop.
Answer
The first two paragraphs of the code.
Chapter 10 Robotic Police Car (Button_ Buzzer _LED Light) 191
10-4.2 watchdog that barks
Using the “ultrasonic sensor” to simulate “a barking watchdog”.
Assuming “equation of speed of running forward and distance”: speed = (distance (cm) -30) * 10.
Answer
Flow chart
When double-clicking the
“mBot program”
If the button is pressed
True
Distance = Distance detected by ultrasonic
Distance = 30
True
Barking sound
Rotational speed of the motor running forward =
(Distance detected by ultrasonic – 30)* 10
mBlock program
192 Note The barking is represented by a “beep sound”.
Scratch (mBlock) Program Design — Using mBot Robot
10-5
LED Light
Definition
The LED lights in the mBot will show the different colors of the RGB.
Features
As a “Warning” purpose.
Example
When the “button” is pressed, the alarm will begin to sound and the LED will flash.
Exterior diagram
Explanation
There are two RGB LED lights (LED1 and LED2) on the controller.
Chapter 10 Robotic Police Car (Button_ Buzzer _LED Light) 193
10-5.1 Button to Switch the Two LEDs to Exchange the
Displays
In the previous unit, we have learnt the basic concept of the two RGB LED lights. Next, we will use
the button to show how the LED displays are switched.
Implementation
Please use the “button” to switch between the two LED displays.
Answer
Flow chart mBlock program
When double-clicking the
“mBot program”
Counter = 0
If the button is pressed
Count = Count + 1
Status = odd or even value
LED does not light
True Status = 1 False
LED1 lights up LED2 lights up
194 Scratch (mBlock) Program Design — Using mBot Robot
10-5.2 Button to Switch the LED to Sound DoReMi
In the previous unit, we have learnt the basic concept of the two RGB LED lights.
Next, we will use the button again to switch the LED to play DoReMi.
Implementation
Use the “button” to switch between two LED displays and to play DoReMi.
Answer
Chapter 10 Robotic Police Car (Button_ Buzzer _LED Light) 195
10-5.3 Button to Activate the LED to Play the Sound of an
Ambulance
In the previous unit, we have learnt the basic concept of the two RGB LED lights.
Next, we will use the button to activate the LED to start playing the sound of an ambulance siren.
Implementation
Please use the “button” to switch between the two LED displays, and play the sound of an
ambulance siren.
Answer
196 Scratch (mBlock) Program Design — Using mBot Robot
10-6
Reset Button
Features
Used to close the program that is being implemented.
Example
While you use the “button” start a program, you can close it using the “reset button”.
Exterior diagram
Chapter 10 Robotic Police Car (Button_ Buzzer _LED Light) 197
Chapter
10 Review questions
Short Answer Questions
1. 1. Please design the sound of a “fire engine” siren.
2. 2. Please design the sound of a “police car” siren.
198 Scratch (mBlock) Program Design — Using mBot Robot
Chapter 11 CHAPTER OBJECTIVES
Bluetooth • To let the reader understand the
Communications basic concept and application of the
mBot Bluetooth communications
Application module.
• To let the reader understand how
send characters, light up the
mBot LED lights and receive mBot
ultrasonic distance through the
pairing of a mobile phone and mBot
Bluetooth.
CHAPTER OUTLINE
11-1 mBot Bluetooth Communications
11-2 m B l o c k C o m m u n i c a t i o n s
Component
11-3 Pairing mBot Bluetooth and
Phone
11-4 Mutual Transmission between
mBot and Phone
11-5 Lighting mBot LED Light With
Phone
11-6 R e c e i v i n g m B o t U l t r a s o n i c
Distance with Phone
11-1
mBot Bluetooth Communications
As the robot can be fitted with a wide range of sensors, using it can facilitate us in the collection of
information. It therefore allows us to enter the domain of IoT, the current mainstream internetworking
area, easily. Of which Bluetooth communication being the key technology.
Coupled with the fact that mobile phones are now like a part of our arm, resulting in IoT being
realized in our lives in general. Whether it is for schoolwork, work or entertainment purposes, the
mobile phone’s Bluetooth device is an important part in the realization of IoT.
In this chapter, I will introduce the use of the “Bluetooth communications module” on the mBot’s
mainboard. Its purpose is to allow your Android mobile phone to communicate with the mBot
Bluetooth.
Flow chart on the uses of the Bluetooth module
Install the Bluetooth module Compose the program for
the communication
component Write App
Connects PC USB cable Turn the power back
to mBot on for the mBot
Turn on mBot power Mobile phone is connected
to the mBot
Open mBlock software Mobile phone starts
communications with mBot
Select the serial port
200 Scratch (mBlock) Program Design — Using mBot Robot