A comparison of two methods of a robot going along a square
mBlock program
Method 1 Method 2
Sequential (Not using the loop) Using the Loop
Repeats 1st time Only needs to be written
Repeats 2nd time once
Repeats 3rd time
Repeats 4th time
Description
1. In the above diagram, the first method of Block box was repeated a total of four times
“forward 3 seconds, turn right”. Therefore, you can take a group of “forward 3 seconds,
turn right” out, and add a “Loop circle” 4 times to the outer layer.
2. Please refer to the section at the back for a description of the Loop.
Chapter 5 Programming Process Control 101
5-3
Switch
Definition
It is used to select a different execution path according to the “conditionals”.
Schematic diagram
Turns left
Turns right
Commonly used Block box 2. Double Switch
1. Single Switch
Advantages
You can determine various different situations.
Disadvantages
When there are too much conditionals, the structure becomes more complex, thus it is more
difficult for beginners to understand immediately.
Application
When there are two or more conditionals.
102 Scratch (mBlock) Program Design — Using mBot Robot
5-3.1 Single Switch
Definition
Means “If... then…”. That is, the description will be only be implemented “on condition”.
Classification
1. Single description
Definition
Mean that when the condition is satisfied, the description to be implemented is only called one line.
Program with code blocks
Flow chart
Flow chart Concept flow chart
Start Start
Conditional False Is it a rainy day? False
True True
Single description
Bring an umbrella
End
End
Chapter 5 Programming Process Control 103
Example mBlock program
If the “button” is pressed, LED1 will light up.
Flow chart
When double-clicking the
“mBot program”
The judgment button
is pressed
True
LED1 & LED2 will light up in red
2. Multiple descriptions
Definition
When the condition is satisfied, more than one line of the description to be implemented is called.
Program with code blocks
Flow chart
Flow chart Concept flow chart
Start Start
Conditional False Is it a rainy day? False
True
True
Bring an umbrella
Multiple Wear rain boots
descriptions
End
End
104 Scratch (mBlock) Program Design — Using mBot Robot
Example
If the “button” is pressed, both LED1 and LED2 will light up in red.
Flow chart mBlock program
When double-clicking the
“mBot program”
The judgment button
is pressed
True
LED1 & LED2 will light up in red
Lecture 1
If the “button is pressed, both LED1 and LED2 will light up in red. And if the button is released,
both LED1 and LED2 will not light up.
mBlock program
Flow chart
When double-clicking the
“mBot program”
False
The button is “pressed”? True LED1 & LED2 will light up in red
False True LED1 & LED2 won’t light up
The button is “released”?
mBlock program
Chapter 5 Programming Process Control 105
Description
If Switch is used alone, it can only detect once and not repeatedly. The solution is to match it
with Loop so that it allows you to operate the robot's actions repeatedly.
Lecture 2
Pertaining to the previous question, by adding Loop will allow us to operate the robot’s actions
repeatedly.
mBlock program
Flow chart
When double-clicking the
“mBot program”
The button is “pressed”? True LED1 & LED2 will light up in red
False True LED1 & LED2 won’t light up
The button is “released”?
False
mBlock program
106 Scratch (mBlock) Program Design — Using mBot Robot
Lecture 3
If the “button” is pressed, the robot runs forward. And if the button is released, the robot will
run backward and repeat the action.
mBlock program
Flow chart
When double-clicking the
“mBot program”
The button is “pressed”? True Robot runs forward
True Robot runs backward
False
The button is “released”?
False
mBlock program
Chapter 5 Programming Process Control 107
5-3.2 Double Choice
Definition
Different descriptions are implemented depending on whether the “conditional” is true or not.
Example
Judging if “Forward” and “Backward”, “Left Turn” and “Turn Right”…etc is happening.
Schematic diagram
If…then… (Double choice) Double paths to the structure
Application
When the condition has only two scenarios.
Program with code blocks
Sprites area
Function table
Function table
Flow chart
Flow chart Concept flow chart
Start Start
True Conditional False True Is it a rainy day? False
Description 1 Description 2 Drive to work Ride a motorbike to work
End Start
108 Scratch (mBlock) Program Design — Using mBot Robot
Implementation
If the “button” is pressed, both LED1 and LED2 will light up in red. Otherwise they will not light
up.
Program with code blocks
Flow chart
When double-clicking the
“mBot program”
True The judgment button False
is pressed
LED1 and LED2 LED1 and LED2
will light up in red won’t light up
mBlock program
Description
If Switch is used alone, it can only detect once, and not repeatedly. The solution is to match it
with Loop so that it allows you to operate the robot's actions repeatedly.
Chapter 5 Programming Process Control 109
Lecture 1
Pertaining to the previous question, by adding Loop allows us to operate the robot's action
repeatedly.
mBlock program
Flow chart
When double-clicking the
“mBot program”
True The judgment button False
is pressed
LED1 and LED2 LED1 and LED2
will light up in red won’t light up
mBlock program
110 Scratch (mBlock) Program Design — Using mBot Robot
Lecture 2
Pertaining to the previous question, add a sound in. That is, when the user presses the “button”,
it will issue a warning sound as well as having two LED red lights lit up.
mBlock program
Flow chart
When double-clicking the
“mBot program”
True The judgment button False
is pressed
LED1 and LED2 will light up LED1 and LED2
in red and make warning sound won’t light up
mBlock program
Chapter 5 Programming Process Control 111
5-4
Loop
Introduction
Computers are invented by humans and are designed to help humans deal with repetitive problems.
One of the methods is by using the Loop.
Definition
Refer to the repetitive implementation of a “Block box”.
Commonly used Block boxes. 2. Conditional loop 3. Infinite loop
1. Counting loop
Advantages
Express easily the complexity of the conditional structure.
Disadvantages
The structure is more complex when using looping around like a nest. Thus it is more difficult for
beginners to understand immediately.
Application
Dealing with repetitive actions or actions with rules.
112 Scratch (mBlock) Program Design — Using mBot Robot
5-4.1 Counting Loop
Definition
It is implemented repeatedly in sequence according to the set value of a “counter”.
Application
This loop is best used when the program is known to have a fixed number of and repeated
implementations.
Examples
Alarm clocks and stopwatches.
Classifications
1. Basic loop; 2. Nest loop.
Block program
Basic Loop Nest Loop
1. Basic Loop 113
Definition
It is a single-level loop. It is the most basic loop description in the programming language.
Application
For repetitive changes of a “single variable.”
Typical example
1. 1 + 2 + 3 + ... + 10
2. Timers or counting down
3. The robot walks along a square (refer to Chapter 3)
Chapter 5 Programming Process Control
Example 1
When the user presses the “button”, the dynamics display 1 added up to 10, and which will be
displayed.
mBlock program
Flow chart
When double-clicking the
“mBot program”
i=1
Sum = 0
The judgment button
is pressed
True
i < 10
True
Sum = Sum + i
i=i+1
Wait for 1 second
mBlock program
114 Scratch (mBlock) Program Design — Using mBot Robot
Example 2
The timer starts counting down from 10 when the user presses the “button”. When it reaches 0, a
“beep” is sounded and a display is shown.
mBlock program
Flow chart
When double-clicking the
“mBot program”
Count = 0
The judgment button False
is pressed
True
Count > 0
True
Say: timer 1 second
Count = Count-1
Wait for 1 second
Make a “beep” sound
mBlock program
Chapter 5 Programming Process Control 115
2. Nest Loop
Definition
Refer to having loops within the other loops. It is a multi-level loop.
Concept
It is like a nest, made up of a combination of layers.
Application
For repetitive changes of “two or more variables”.
Example
When the user presses the “button”, the dynamics display the electronic code table values from 1 to
100.
mBlock program
116 Scratch (mBlock) Program Design — Using mBot Robot
5-4.2 Conditional Loop
Definition
It is when the number of loops is not known in advance.
Application
It is best to use this loop if you do not know the number of implementations of the program.
Examples
The robot runs forward and only stops when the ultrasonic sensor detects an obstruction.
mBlock program
Description
When “conditional” is true, it will jump out of the loop. Otherwise it will continue to implement the
“program block” command repeatedly.
Chapter 5 Programming Process Control 117
Implementation 1
When the user presses the “button”, the robot runs forward and only stops when the ultrasonic
sensor detects an obstruction.
Answer
Flow chart
When double-clicking the
“mBot program”
The judgment button True
is pressed
mBot stops
True
Distance detected < 5
False
mBot runs forward
mBlock program
118 Scratch (mBlock) Program Design — Using mBot Robot
Implementation 2
The robot's two LED lights (RGB three colors) will blink continuously, and will only stop when
the “button” is pressed again.
Answer
Flow chart
When double-clicking the
“mBot program”
The judgment button True
is pressed
Both LED lights
False do not light up
Both LED lights (RGB three colors)
blink continuously
mBlock program
Chapter 5 Programming Process Control 119
5-4.3 Infinite Loop
Definition
When any one condition is not met, the loop will be implemented forever.
Application
To have the robot continuously detect an object.
Examples
Using the robot's ultrasonic sensor to continuously detect if there is a “customer” passing through,
in front of it. If there is, the counter automatically increases by 1.
mBlock program
Description
1. The “program block” instruction in the loop will be implemented repeatedly.
2. In general, it will be used with Switch.
Flow chart
Basic flow chart Basic flow chart + combined with Switch
Start
Start
Description block Description block
Conditional
End End
120 Scratch (mBlock) Program Design — Using mBot Robot
Implementation 1
When the user presses the “button”, the robot will use the ultrasonic sensor to detect whether a
“customer” is entering, at every second. If there is, the counter automatically goes up by 1.
Answer
Flow chart
When double-clicking the
“mBot program”
Count = 0
The judgment button
is pressed
True
Detection distance<5?
True
Count = Count+1
Wait for 1 second
Count value is displayed
mBlock program
Chapter 5 Programming Process Control 121
Implementation 2
When the user presses the “button”, the robot will use the ultrasonic sensor to detect whether a
“customer” is entering, at every second. If there is, the counter automatically goes up by 1.
Answer Flow chart Subroutine
Main program LED lights blinking +Beep sound
LED lights up in “red”
When double-clicking the
“mBot program” Beep sound Wait for 0.2 seconds
Count = 0
The judgment button True LED lights LED lights up in “green”
is pressed blinking + Beep sound Wait for 0.2 seconds
True Beep sound LED lights up in “green”
Beep sound Wait for 0.2 seconds
Count > 10
False
Distance detected<5?
True
Count = Count+1
Wait for 1 second
Count value is displayed
mBlock program mBlock program
(Detects and display admission of “customer”) (Define “LED lights flashing+ beep” of subroutine)
122 Scratch (mBlock) Program Design — Using mBot Robot
Chapter
5 Review questions
1. When writing mBlock program, which three types of process control are commonly
used as control structures? Draw up the flow charts.
2. Define and give examples on the three respective structures in “Process Control”.
3. 3. Please list down the advantages and disadvantages of “Sequential” and when
best to use it.
4. Please list down the advantages and disadvantages of “Switch” and when best to
use it.
5. Please list down the advantages and disadvantages of “Loop” and when best to
use it.
6. Toggle between two LED displays by pressing a button.
7. Alternate between activating the LED display and playing a sound (ambulance) by
pressing a button.
Chapter 5 Programming Process Control 123
124 Scratch (mBlock) Program Design — Using mBot Robot
Chapter 6 CHAPTER OBJECTIVES
Robot Navigates • To let the reader understand the
the Maze definition and principle of the input
of mBot “ultrasonic sensor”.
(Ultrasound Sensor)
• To l e t t h e re a d e r u n d e r s t a n d
the various ways of using mBot
“ultrasonic sensor”.
CHAPTER OUTLINE
6-1 Understanding the Ultrasonic
Sensor
6-2 D e t e r m i n i n g t h e V a l u e o f
Ultrasonic Sensor
6-3 Ultrasonic Sensor of the Wait
Module
6-4 Ultrasonic Sensor of the Switch
Module
6-5 Ultrasonic Sensor of the Loop
Module
6-6 mBot Navigates the Maze
6-7 Ultrasonic Sensor Controlling
Other Block Modules
6-8 Watchdog
6-9 Automatic Braking System
6-1
Understanding the Ultrasonic Sensor
Definition
It is able to detect distance, whether it is near or far, just like the human eye.
Purpose
It is able to detect whether there is an “obstacle” or “target object” in front, allowing the robot to
perform different actions accordingly.
Exterior diagram
1. “Information returned” can have the distance
divided into units of cm.
2. Distance Measured: 1 to 400 cm.
Description
The front of the ultrasonic sensor has two parts; namely the “transmitter” and the “receiver”. The
sensor is mainly used to measuring the distance between it and the object detected.
Principle
When the “ultrasonic” is launched, it uses “sonar” technology to hit the surface of the object and
then receives the “reflected wave”. From the time of “transmitting” to “receiving”, it can find out the
“distance” between the “sensor and object”.
Principle through illustration
transmitter ultrasonic waves
receiver reflected waves
distance
Application
1. To detect any wall in front; 2. To detect if anyone is near the robot; 3.To measure distance.
126 Scratch (mBlock) Program Design — Using mBot Robot
6-2
Determining the Value of Ultrasonic Sensor
Detecting the distance of the ultrasonic sensor in mBlock programming language
Testing the distance Move your hands slowly horizontally
Distance detected (farther)
Place your hand in front of the ultrasonic sensor
Test results
Distance detected (closer)
Return Value ≒ 4 Return Value ≒ 20
Three common methods of using the ultrasonic sensor
The ultrasonic sensor in mBlock is often used by the following three functional Blocks.
Wait
Loop
Switch
Chapter 6 Robot Navigates the Maze (Ultrasound Sensor) 127
6-3
Ultrasonic Sensor of the Wait Module
Features
It is set to implement the next action only when the “ultrasonic sensor” detects that the obstacle in
front is less than the “threshold value”.
Wait
Detected value Threshold value
Description
Only when the “conditional” in Wait is true, will the next action be implemented. Otherwise, none
of the following instructions will be implemented.
6-3.1 mBot Automatically Stops When an Obstruction is Detected
In the previous unit, we have understood when the “ultrasonic sensor” is applicable and in detecting
distance. Next, we can begin to write on how to make mBot automatically stop if there is an obstacle
detected in the process of walking.
Implementation
The mBot runs forward until the “ultrasonic sensor” detects an “obstacle” 25 centimeters in front of
it. It then “stops”. Please use Wait.
Schematic diagram Flow chart
WALL When double-clicking the
25 cm “mBot program”
The robot runs forward
False
Obstacles detected?
True
mBot stops
mBlock Program
128 Scratch (mBlock) Program Design — Using mBot Robot
6-3.2 Stops and Sounding an Alarm When an Obstacle is Detected
In the process of learning how to make mBot stop automatically when an obstacle is detected while
walking, add on another function, that is; it will automatically sound an alarm.
Implementation
mBot runs forward until the “ultrasonic sensor” detects an “obstacle” 25 cm in front of it. It
stops and sounds an alarm. Please make use of Wait.
Schematic diagram Flow chart
WALL When double-clicking the
25 cm “mBot program”
The robot runs forward
False
Obstacles detected?
True
1. Sounds an alarm
2. mBot stops
mBlock Program
Chapter 6 Robot Navigates the Maze (Ultrasound Sensor) 129
6-4
Ultrasonic Sensor of the Switch Module
Definition
It is used to determine whether the distance detected by the “ultrasonic sensor” is less than the
“threshold value”. If “yes”, the branch “above” will be implemented. Otherwise the branch “below” will
be implemented.
Switch
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.
Therefore, when your “detected value” less than the “threshold value”, the branch “above” will be
implemented.
6-4.1 Using Switch to Make the Robot Stop
In the previous unit, we use Wait to make mBot, while walking, automatically stop when there is an
obstacle detected. In this unit, we will introduce the use of Switch to achieve this function.
Implementation
mBot runs forward and stops when the “ultrasonic sensor” detects an “obstacle” 25cm in front of it.
Please use Switch.
WALL
25 cm
130 Scratch (mBlock) Program Design — Using mBot Robot
Answer
Flow chart
When double-clicking the
“mBot program”
True Ultrasonic detected False
distance <25
mBot stops mBot runs forward
mBlock Program
Description
In the above program, it can only detect once and not repeatedly if you use Switch on its own.
6-4.2 Using Switch to make the robot stop and sound an alarm
In the previous unit, we use Wait to make mBot automatically stop, if an obstacle is detected, while
walking. In this unit, we will introduce the use of Switch to add on an additional function; to make it
sound an alarm automatically.
Implementation
mBot runs forward until the “ultrasonic sensor” detects an “obstacle”25 cm in front of it. It stops
and sounds an alarm. Please use Switch.
Chapter 6 Robot Navigates the Maze (Ultrasound Sensor) 131
WALL
25 cm
Answer
Flow chart
When double-clicking the
“mBot program”
True Ultrasonic detected False
distance <25
mBot stops
Sound an alarm mBot runs forward
mBlock program
132 Scratch (mBlock) Program Design — Using mBot Robot
6-5
Ultrasonic Sensor of the Loop Module
Definition
It is used to end Loop when the “ultrasonic sensor” detects that the distance is less than the “threshold
value”.
Loop
Example
The robot runs forward until the ultrasonic sensor detects an “obstacle” in front of it, and then Loop
ends.
Analysis
Schematic diagram Flow chart
WALL When double-clicking the
“mBot program”
WALL The robot runs forward
False
Obstacles detected?
True
mBot stops
mBlock program
Chapter 6 Robot Navigates the Maze (Ultrasound Sensor) 133
6-6
Ultrasonic Sensor of the Switch Module
“An mBot navigating a maze” often comes up in the robotics competitions organized by the World
Robot Olympiad. Ultrasonic sensor is used to complete it.
Starting from the entrance Finding the maze path Locate the exit smoothly
Analysis
1. The robot will run forward until its “ultrasonic sensor” detects an “obstacle” in front of it. It will
then turn right or left.
2. If you use Wait on its own, you can only implement it once and not repeatedly.
Solution
The robot's action can be operated repeatedly by using it with Loop, which has no limits.
2 Common Scenarios
First scenario(Exit is on the right) Second scenario(Exit is on the left)
Entrance Exit
Flow chart (Exit is on the right) Exit Entrance
When double-clicking the Flow chart (Exit is on the left)
“mBot program”
When double-clicking the
The robot runs forward “mBot program”
False
The robot runs forward
Obstacles detected?
True False
Obstacles detected?
Turns right (rotates) True
Turns left (rotates)
Scratch (mBlock) Program Design — Using mBot Robot
134
mBlock program mBlock program (Exit is on the left)
mBlock program (Exit is on the right)
Chapter 6 Robot Navigates the Maze (Ultrasound Sensor) 135
6-7
Ultrasonic Sensor Controlling Other Block Modules
Assuming we have finished assembling a robot, and want the robot to detect the distance in order
to decide on the speed of running forward. That is, the closer the robot is to an obstacle, the slower the
speed. At this point, we must use the “ultrasonic sensor” to detect the “distance” of the obstacle in front
and have the “numerical data of the distance” transmitted to the rotational speed of the “motor”.
Example
Detection of distance through ultrasonic means to control the speed of the motor.
The distance detected by the “ultrasonic sensor” is sent to the motor as its “horsepower” input.
Analysis
Schematic diagram Flow chart
WALL When double-clicking the
Walk slower “mBot program”
WALL The robot runs forward
False Detecting obstacles
too close by?
True
mBot stops
mBlock program
Output port Input port
Description
1. The absolute value of the rotational speed of the motor is 255.
2. The length of the distance detected by the ultrasonic sensor is about 400cm. Therefore,
400/255 ≒ 1.6
3. Therefore, the rotational speed of the motor can be normalized every time the length detected
by the ultrasonic is divided by 1.6.
136 Scratch (mBlock) Program Design — Using mBot Robot
6-8
Watchdog
Using the “ultrasonic sensor” to simulate the “watchdog system”.
Assume the “equation of the speed of going forward to distance”: speed = (distance (cm) - 30) * 10
Answer
Flow chart
When double-clicking the
“mBot program”
Motor forward rotational speed =
(ultrasonic detected distance -30) * 10
mBlock program
Chapter 6 Robot Navigates the Maze (Ultrasound Sensor) 137
6-9
Automatic Braking System
Ladies and gentlemen, if you have the experience of driving, you will find that the unit's automatic
braking system is a feature people who drives would wish to have.It will issue different frequencies of
sound according to the distance from the object.It will even automatically stop if it gets too close to the
object.
Implementation
Using the “ultrasonic sensor” to simulate the “relationship between the distance to sound
frequency” of the “automatic braking system”. Assume the “the equation of distance to frequency”:
frequency (Hz) = -50 * distance (cm) +2000
Answer
Flow chart
When double-clicking the
“mBot program”
The judgment button is pressed
True
Tone = -50 * Ultrasonic detected distance +2200
Wait for 0.001 seconds
mBlock program
138 Scratch (mBlock) Program Design — Using mBot Robot
Chapter
6 Review questions
1. Please write an mBlock program for when the user press the “button”, the robot
will repeatedly walk along a square until an obstacle is 4cm in front of the “ultrasonic
sensor”. It will then stop.
2. The robot walks back and forth (A robot that does not dare lay siege).
3. The robot runs forward until the “ultrasonic sensor” detects an obstacle8cmin front
of it, where it then reverses. This program is carried out repeatedly.
Chapter 6 Robot Navigates the Maze (Ultrasound Sensor) 139
140 Scratch (mBlock) Program Design — Using mBot Robot
Chapter 7 CHAPTER OBJECTIVES
Robotic Tracking • To let the reader understand the
Vehicle definition and principle of the input
of an mBot’s of a “line-follower
(Line-Follower sensor”
Sensor)
• To let the reader understand the
application of the mBot “line-
follower sensor”.
CHAPTER OUTLINE
7-1 Understanding the Line-Follower
Sensor
7-2 Determining the Value of the
Line-Follower Sensor
7-3 Line-Follower Sensor of the Wait
Module
7-4 Line-Follower Sensor of the
Switch Module
7-5 Line-Follower Sensor of the Loop
Module
7-6 Robotic Tracking Vehicle
7-7 Robot Stops when Detecting
Third Black Line
7-8 Tracking Robot That Plans to
Automatically Park
7-1
Understanding the Line-Follower Sensor
Definition
It is used to detect different colors (such as: white and black), and to walk following the black or white
line.
Purpose
To allow the robot to perform different actions according to the different colors on the floor.
Diagram
Sensor 1
Sensor 2
Exterior
At the front end of the line-follower sensor, facing the direction that the mBot runs forward, both
its left and right sides have an IR sensor (Sensor1 on its left, Sensor2 on its right).
Application
1. Robot is tracking (walking along black color).
2. Garbage truck (tracking car + ultrasonic sensor).
3. Finding the black line.
142 Scratch (mBlock) Program Design — Using mBot Robot
7-2
Determining the Value of the Line-Follower Sensor
“Return value” detected by the line-follower sensor in the mBlock development
environment
Testing the return value
The mBot line-follower sensor can only determine the colors black and white. The determined
return value has four scenarios.
Sensor1 (left side) detected colors Sensor2 (right side) detected colors Return Value
Black Black 0
Black White 1
White Black 2
White White 3
Description
1. If the return value is “0”, it means that the line-follower sensor is currently completely on a
black line.
2. If the return value is “1”, it means that the “left” of the line-follower sensor is currently on a
“black line”, and the “right” IR sensor is on a “white line”.
3. If the return value is “2”, it means that the “left” of the line-follower sensor is currently on a
“white line”, and the “right” IR sensor is on a “black line”.
4. If the return value is “3”, it means that the line-follower sensor is currently completely on a
white line.
Three common methods of using the Line-Follower Sensor
The line-follower sensor is often used in the following three functional Block.
Wait
Loop
Switch
Chapter 7 Robotic Tracking Vehicle (Line-Follower Sensor) 143
7-3
Line-Follower Sensor of the Wait Module
Function
It is set to implement the next action only when the “line-follower sensor” detects the “threshold value
(black line)”.
Wait Detected value Return value
Example
The robot runs forward and stops when the “line-follower sensor” detects a "black line". [Please use
Wait.]
Answer
Flow chart mBlock Program
When double-clicking the
“mBot program”
The robot runs forward
False
Detects a black line?
True
mBot stops
144 Scratch (mBlock) Program Design — Using mBot Robot
7-4
Line-Follower Sensor of the Switch Module
Definition
It is used to determine whether the “line-follower sensor” detects a “black line”. If “yes”, the branch
“above” is implemented, otherwise, the branch “below” will be implemented.
Switch Detects value Return value
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 1
The robot runs forward and stops when the “line-follower sensor” detects a "black line". [Please use
Switch.]
Chapter 7 Robotic Tracking Vehicle (Line-Follower Sensor) 145
Answer
Flow chart
When double-clicking the
“mBot program”
Set Return Value = 0
True Return Value = return value of the False
line-follower sensor
Return Value = 0
mBot stops mBot runs forward
mBlock program
146 Scratch (mBlock) Program Design — Using mBot Robot
Example 2
The robot walks on the top of a desk and "stops" when the "line-follower sensor" detects the "table
edge".
Steps in analysis
Step First, use the mBot “line-follower sensor” to find the return value of the detected “table
1 edge”.
Step Then, using the return value as a condition to stop the motor from running forward.
2
mBlock program
Note The speed of the mBot walking forward before reaching the "table edge" must not be too fast,
otherwise it may fall off the desk.
Chapter 7 Robotic Tracking Vehicle (Line-Follower Sensor) 147
7-5
Line-Follower Sensor of the Loop Module
Definition
It waits for the “line-follower sensor” to detect whether there is a “black line” and if “yes”, then Loop
ends.
Loop
Detects value Return value
Example
The robot runs forward and "stops" when the "line-follower sensor" detects a black line. [Please use
Loop.]
Schematic diagram Flow chart
When double-clicking the
“mBot program”
The robot runs forward
False
Obstacles detected?
True
mBot stops
mBlock program
148 Scratch (mBlock) Program Design — Using mBot Robot
7-6
Robotic Tracking Vehicle
In the field of robotics, a lot of competitions,
both at home and abroad, require the use of a
“track”. That is, using the “line-follower
sensor” to move along a black line.
Illustration
The robot travels “from left to right”.
Scenario
Description
The distance between the left and right IR sensors is about 1.2 cm, while the width of the black
line is about 2 cm.
To understand the mBot during the process of walking, the above four scenarios may occur as
in order to achieve the results of the mBot walking along a black line, we must make a variety of
adjustments. That is, different adjustment actions are done under different circumstances.
Scenario Sensor1 (Left Sensor2 (Right Return value Adjustment
side) detects side) detects action
On the black line 0
Leans to the left color color 1 Runs forward
side Black Black 2 Turns left
Leans to the right Black White 3 Turns right
side
Deviate from the White Black *note
black line
White White Two scenarios
Note Note If the mBot deviates completely from the black line, must first determine which direction the mBot
last turn towards. If the mBot deviated completely from the black line when it turned right previously,
then adjust the right turn to adjust the mBot’s direction of travel.
Chapter 7 Robotic Tracking Vehicle (Line-Follower Sensor) 149
Implementation 1
Please have three different scenarios where the “line-follower sensor” has a return value (0 ~ 2)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?
Detects repeatedly True True
True Turns left Turns right
Runs forward
mBlock program
150 Scratch (mBlock) Program Design — Using mBot Robot