The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.

Scratch(mBlock) Programming Design - Use the mBot Robot_Tiked

Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by newr_100, 2020-01-14 16:26:29

Scratch(mBlock) Programming Design - Use the mBot Robot_Tiked

Scratch(mBlock) Programming Design - Use the mBot Robot_Tiked

3-3
Making the Robot Move

After understanding the basic principles and related parameter settings of the motor, you can now
start writing an mBlock program to make the robot move, that is; making the robot run forward,
backward, turn left and right, as well as moving fast and slow.

Schematic diagram

Robot is driven by dual motor, running “forward, backward, left and right”.

Implementation 1

Please write an mBlock program allowing the robot motor to stop automatically after running forward
3 seconds.

Illustrated diagram

Run forward from right to left three times

Answer

Flow chart mBlock program

When double-clicking the
“mBot program”

Runs forward 3 seconds
mBot stops

Chapter 3 mBot Starts to Move 51

Implementation 2

Please write an mBlock program allowing the robot motor to run forward 3 seconds before turning
right when the user presses the “button”.

Illustrated diagram

Motor turns right after running forward3 seconds

Answer

Flow chart mBlock program

When double-clicking the
“mBot program”

The judgment
button is pressed

True

Runs forward 3 seconds

Rotates right 0.65 seconds

mBot stops

52 Scratch (mBlock) Program Design — Using mBot Robot

3-4
Moving the Robot in a Square

In the previous section, we learnt how to make an mBot carry out the four major basic actions,
running “forward, backward, left, and right”. Next we will design a program that allows the robot go
along a square.

Implementation 1

Please write an mBlock program with a sequential structure (without using loops) so that when the
user presses the “button”, the robot can move along a square.

Illustrated diagram

Motor runs forward 3 seconds, heads right, repeat 4 times

Answer

Flow chart mBlock program

When double-clicking the
“mBot program”

Runs forward 3 seconds
Rotates right 0.65 seconds

Runs forward 3 seconds
Rotates right 0.65 seconds

Runs forward 3 seconds
Rotates right 0.65 seconds

Runs forward 3 seconds

Rotates right 0.65 seconds
mBot stops

Chapter 3 mBot Starts to Move 53

Implementation 2

Please write an mBlock program using the "Loop circle" structure, so that when the user presses the
“button”, the robot will move along a square.

Answer

Flow chart mBlock program

When double-clicking the
“mBot program”

Set number of times = 0

Forward 3 seconds

Rotates right 0.65 seconds

Number of times
= number of times +1

Ture

The number of times <= 4

False

mBot stops

Note: Refer to Chapter 5 of this book for a detailed description of the "Sequential" and "Loop" structures.

54 Scratch (mBlock) Program Design — Using mBot Robot

3-5
Motor Receiving Other Power Sources

Assuming that we have assembled a wheeled robot, the robot starts slowing down as it moves closer
to an obstacle in front. At this point, we must use the “ultrasonic sensor to detect the distance” as the
source for the speed of the motor.
1. Ultrasonic sensor to control the speed of the motor.
2. Random numbers to control the motor to turn on its own.
3. Light sensor to control the speed of the motor.

3-5.1 Ultrasonic Sensor to Control the Speed of the Motor

Definition
“Ultrasonic sensor” detects distance to control the “speed” of the motor.

Example

The “ultrasonic sensor” emits the distance detected, which then gets transmitted to the rotational speed
of the “motor”.

Answer

When double-clicking the
“mBot program”

Flow chart Distance = 0
Distance= Ultrasonic detection distance

mBot motor speed = Distance / 1.6

mBlock program

Chapter 3 mBot Starts to Move 55

Description

1. The absolute value of the rotational speed of the motor is 255.
2. Ultrasonic sensor detected a distance of about 400cm.Therefore, 400/255 ≒ 1.6.
3. Thus, the rotational speed of the motor can be normalized every time the distance,

detected by the ultrasonic, is divided by 1.6.

3-5.2 Random numbers to control the motor to turn
on its own (to dance)

Example

Send the return value of the “Random Block” to the rotational speed in the “motor”. That is, let the
robot decide on the direction it will move.

Answer

When double-clicking the
“mBot program”

Distance 1 = 0
Distance 2 = 0

Flow chart Distance 1 = Get a random value
Distance 2 = Get a random value

Rotation speed of mBot left motor = Distance1
Rotation speed of mBot right motor = Distance2

Wait for 0.2 seconds

mBlock program
with code blocks

Set the key parameter

-255 to represent running backward, 255 to represent running forward.

56 Scratch (mBlock) Program Design — Using mBot Robot

3-5.3 Light Sensor to Control the Speed of the Motor

Definition
“Ultrasonic sensor” detects distance to control the “speed” of the motor.

Example

The “light sensor” detects the value of light and then transmits it to the “motor” rotational speed.
That is, the higher the “light value” is, the faster the speed. Conversely, the lower the “light value”
is, the slower the speed.

Answer

Flow chart When double-clicking the
“mBot program”

Light value = 0

Light Value
= The light sensor detects the light source

Rotation speed of the mBot = Light Value/4

mBlock program

Description

1. The absolute value of the motor rotational speed is 255.
2. 2. The light value detection by the light sensor is approximately 0 ~ 1023. Therefore,

2013/255 ≒ 4.
3. Thus, the rotation speed of the motor can be normalized every time the light value detected

by the light sensor is divided by 4.

Chapter 3 mBot Starts to Move 57

Chapter

3 Review questions

1. Please list down the basic functions of the mBot. List at least four.
2. Please list down the advanced features of the mBot. List at least four.
3. How would you design the distance moved by the mBot?

58 Scratch (mBlock) Program Design — Using mBot Robot

Chapter 4 CHAPTER OBJECTIVES
Data and
• To let the reader understand the
Computations how to make a variable and the
way it is displayed in the mBlock
development environment.

• To l e t t h e re a d e r u n d e r s t a n d
the arrays of lists and the use
of subprograms in the mBlock
development environment.

CHAPTER OUTLINE

• 4-1 Variables
• 4 - 2 G e n e r a l C o m p u t a t i o n o f

Variable Data
• 4-3 List
• 4-4 General Computation of Lists
• 4-5 Subprogram (Make a Block)


4-1
Variables

Definition
During the process of implementing the program, its “content” will change along with the
implementation of the program.

Concept

Imagine a “variable” as a “container” that is specifically used to “storing data”.

Schematic diagram

Variable Stored data

Purpose Container

1. To configure the appropriate main memory space to the system requirements.
2. Reduce errors in logic.

Example

A=B+1
A, B is a variable, whereby its content can be changed.

Illustrated diagram

Process of implementation Changes in the content of the variable

A=0 : B=1 A 0→2
A=B+1

B1

60 Scratch (mBlock) Program Design — Using mBot Robot

4-1.1 Steps to Make a Variable

You need to make a variable as computed data is often used in mBlock program. The steps are as
follow:

• Step 1: Scripts / Data and Block / Make a variable

• Step 2: Make a variable: Distance

Scripts / Data and Block / make a variable Make a variable: Distance



2



3

2

Description

In Step 2, you understand that the variable falls into two categories:
1. For all sprites: representing “Global variables” as the main one in this book.
2. For this sprite only: representing “Local Variables”.

• Step 3: Showcase the related graphical blocks and contents of the “variable”.

Shows the current content of the “Distance” variable Check

Description

At this point, the content of the current “Distance” variable is displayed on the upper left corner
of the stage.

Chapter 4 Data and Computations 61

4-1.2 The Presence of Variables

Basically, once the variable is completely made, it will automatically appear on the upper left corner
of the stage.

1. Hiding the variable

If you do not wish to display the contents of the variable, you can use the following two methods:

Uncheck Execute the mBlock program “Hide variable”

2. Displaying the variable

If you wish to display the contents of the variable, you can use the following two methods:

Check Execute the mBlock program “Display variable”

3. The three different display modes are commonly seen

Normal size Large size Slider

62 Scratch (mBlock) Program Design — Using mBot Robot

4-1.3 Maintenance of Variables

Basically, when writing a data computing program, we often make a lot of variables. If they were not
given a meaningful name from the start, subsequent maintenance work will be affected. Thus, if you
wish to rename a variable or delete the name of a variable, the method is as follows:

1. Right-click 2. Select the item to be maintained
(Rename variable or delete variable)

Rename the variable Delete a variable


2

Chapter 4 Data and Computations 63

4-2
General Computation of Variable Data

In mBlock program, computing of data can be broadly divided into the following five
categories:

1. Arithmetic 2. Comparison 3. Logic

4. String 5. Mathematical

Description
In the table, the mathematical computation may include various “mathematical functions” and
“transfer functions”.

64 Scratch (mBlock) Program Design — Using mBot Robot

4-2.1 Specific Operation

Definition
Assign the result of the expression on the “right” to the arithmetic operator on the “left” (that is the
variable name).

Method

Start with the right side of the specific operator “=”

Example

Assigned

Arithmetic operator Specific operator Result of the expression
Sum = 0

Illustrated diagram

Block program display method

Description

1. Set the value… of the variable ... to “specific operator”.
2. Assign the number 0 on the right to the variable “Sum” on the left. In other words, the

variable “Sum” is set to zero.

Chapter 4 Data and Computations 65

4-2.2 Arithmetic Operation

Introduction

Just like in mathematics, there are four arithmetic operations in the programming language.

Purpose

It is use to carry out the four arithmetic operations handle the input of “numerical data” by the user.

Diagram of the four arithmetic operations in order of priority

Order Diagram Function Example Result

1 Multiply 40

1 Divide 3.333…
2 Add 42
2 Subtract 14

Implementation 1

When the user presses the “button”, the Count counter’s variable value is automatically added by 1
and executed repeatedly.

Answer mBlock program

Flow chart

When double-clicking the
“mBot program”

count = 0

The judgment button
is pressed

True
Count = Count +1

Wait for 0.3 seconds

66 Scratch (mBlock) Program Design — Using mBot Robot

Implementation 2

It is using the “ultrasonic sensor” to simulate the “relationship between the distance and sound
frequency” in the “automatic braking system”. Assume the “equation of the distance and 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 detection distance +2200

Wait for 0.001 seconds

mBlock program

Chapter 4 Data and Computations 67

4-2.3 Relational Operation

Definition
Refer to a comparison of the size of the expression. It is also thus called “comparison expression”.

Schematic diagram

Comparison of the size of the relationship

Application

“Conditional” within the “Select Structure”.

Purpose

Used to determine if the “conditional” is true.

Block of various types of relational operations

Block Function Conditional expression Result
False
Equals to True
False
Smaller than

Bigger than
Note: The precedence of the relational operators is the same.

68 Scratch (mBlock) Program Design — Using mBot Robot

Implementation 1

When the user presses the “button”, the mBot “ultrasonic sensor” will repeatedly detect if there
is any obstacle in front of it, within a distance of 5 cm. If there is, it will stop; otherwise it will
continue to run forward.

Answer

Flow chart

When double-clicking the
“mBot program”

True The judgment button False
is pressed

True

Ultrasonic detection distance <5

mBot stops mBot runs forward

mBlock program

Chapter 4 Data and Computations 69

Implementation 2

When the user presses “button”, mBot’s “light sensor” will repeatedly detect the current level of
brightness. If the level is more than 500, it will run forward; otherwise it will stop.

Answer

Flow chart

When double-clicking the
“mBot program”

False The judgment button True
is pressed

True

Detected light source value> 500

mBot stops mBot runs forward

mBlock program

Note: The “light sensor” detects the range of light: 0 ~ 1023. The higher the value, the brighter it is.

70 Scratch (mBlock) Program Design — Using mBot Robot

Implementation 3

When the user presses “button”, mBot “line-follower sensor” will repeatedly detect if the floor is a
black or white line. If it is a black line, it will stop; otherwise it will run forward.

Answer

Flow chart

When double-clicking the
“mBot program”

True The judgment button False
is pressed

True

The return value of the line-follower = 0

mBot stops mBot runs forward

mBlock program

Note: The mBot line-follower sensor can only identify the colors black and white. There are four scenarios
regarding the return value. Please refer to Chapter 7 for a detailed description.

Chapter 4 Data and Computations 71

Sensor1 (left) detects color Sensor2 (right) detects color Return value
Black Black 0
Black White 1
White Black 2
White White 3

4-2.4 Logical Operation

Introduction

It is developed by mathematician Boolean, including: AND, OR, NOT...etc.

Definition
It is a more complex type of computing, also known as Boolean operation.

Application

In conditions when there are two (or more) “conditional” in the “selection structure”.

Purpose

To combine "logical operator" and "comparison operator" to enhance the functionality of the program.

Block Program

Block of various types of relational operations

Given A=True, B=False

Block Function Expression Result
A And B False
AND A Or B True
Not A False
OR

NOT

72 Scratch (mBlock) Program Design — Using mBot Robot

Implementation 1

The mBot will stop when the “button” is pressed and the “Line-follower sensor” detects black or
the “light sensor” detects darkness. Otherwise it will run forward.

Answer

Flow chart

When double-clicking the
“mBot program”

The judgment button
is pressed

True

True The return value of the line-follower = 0 False
Or

Light source detected value <500

mBot stops mBot runs forward

mBlock program

Chapter 4 Data and Computations 73

Implementation 2

When the “button” is pressed and the “Line-follower sensor” detects black while the “ultrasonic
sensor” detects an obstacle in front of it, the mBot will stop. Otherwise it will run forward.

Answer

Flow chart

When double-clicking the
“mBot program”

The judgment button
is pressed

True

True The return value of the line-follower = 0 False
Or

Light source detected value <10

mBot stops mBot runs forward

mBlock program

74 Scratch (mBlock) Program Design — Using mBot Robot

Implementation 3

When the “button” is pressed and the “ultrasonic sensor” does not detect any obstacles in front of
it, the mBot will run forward. Otherwise it will stop.

Answer

Flow chart

When double-clicking the
“mBot program”

False The judgment button True
is pressed

True

Detected distance Not <10

mBot stops mBot runs forward

mBlock program

Chapter 4 Data and Computations 75

4-2.5 String Operation

Features
A correlation operation used to concatenate letters or strings of words.

Purpose

To be more flexible in the output of string information.

Block of various types of string operations

Block Function Example

Merge string

Results of
execution

Take out the
first character

Results of
execution

Calculates
the number
of words in
the string

Results of
execution

The number
is converted

to string

Results of
execution

76 Scratch (mBlock) Program Design — Using mBot Robot

Implementation 1

When the “button” is pressed, the “line-follower sensor” detects the return value of either the
black or the white line. It will then display the result using the “merge string” block.

Answer

Flow chart

When double-clicking the
“mBot program”

String = 0

The judgment button
is pressed

True

String
= String combines the line-follower return value

Wait for 1 second

mBlock program

Results

Note: mBot line-follower sensor can only identify the colors black and white. There are four scenarios
regarding the return value. Please refer to Chapter 7 for a detailed description.

Chapter 4 Data and Computations 77

Sensor1 (left) detects color Sensor2 (right) detects color Return value
Black Black 0
Black White 1
White Black 2
White White 3

Implementation 2

When the “button” is pressed, the line-follower sensor detects the return value of the black or
white line. Through the two LED lights, the number of detection is displayed using the “string
length” Block.

Return Value LED2 LED1
0 Unlit Unlit
1 Unlit Lit
2 Lit Unlit
3 Lit Lit

Answer Flow chart

When double-clicking the
“mBot program”

String = 0
Count = 0

The return value True Both LEDs are unlit
of the line-follower = 0 True LED1 lit LED2 unlit
True
False True LED1 unlit
Both LEDs are lit
The return value
of the line-follower = 1

False

The return value
of the line-follower = 2

False

The return value
of the line-follower = 3

False

Combine the line-follower return value
Length is calculated after combining

Wait for 0.5 seconds

78 Scratch (mBlock) Program Design — Using mBot Robot

mBlock program

Results

Chapter 4 Data and Computations 79

4-2.6 Mathematical Operation

Features
Use to deal with a variety of mathematical operations.

Purpose

To allow mBot having the ability of carrying out mathematical operations.

Block of various types of string operations

Block Function Common examples
Random
number Dancing Robots → Refer to e.g. in
Chapter 3.
Take the (Using “random value” to determine
remainder the direction and speed of the motor)

Rounding Find odd or even number
1. Press the “button” the number of

times to control the lighting of left
and right LCD light.
2. Automatic switch. (Odd: On, Even:
Off)

Have the values detected by various
sensor as (integer)

Mathematical
function

80 Scratch (mBlock) Program Design — Using mBot Robot

Implementation 1

Using the “ultrasonic sensor” to detect the front distance, using “rounding” Block as an example to
compare.

Answer

Not using “rounding”

Using “rounding”

Results

Not using “rounding” Using “rounding”

Implementation 2

To control the lighting up of both left and right LCD by pressing the “button” a specific number of
times. (Lit when odd numbers, unlit if it’s even numbers)

Chapter 4 Data and Computations 81

Answer

Flow chart

When double-clicking the
“mBot program”

Count = 0

True The judgment button False
is pressed

True
Count = Count +1

Count Mod 2 = 1
Odd number of judgments

Two LED lights lit Two LED lights unlit

Wait for 0.5 seconds

mBlock program

Results “Even number of times” pressed

“Odd number of times” pressed Both left and right LCD unlit

Both left and right LCD lit up

82 Scratch (mBlock) Program Design — Using mBot Robot

4-3
General Computation of Variable Data

Definition
Refer to a collection of variables with the “same name” and “data type”.

Characteristics

1. Takes up contiguous memory space.
2. One of the ways to represent a sequence.
3. The elements of the data types are the same.
4. Support (Random Access) and (Sequential Access).
5. Inserting or deleting elements is more troublesome as other elements needs to be diverted.

Application

To temporarily store the continuous changes in the value of the environment at each interval or
distance.

Example

Using the temperature sensor to record the temperature once every hour and then storing it in the
list.

Schematic diagram The data type of each element is the same

Continuous memory space

4-3.1 Create a List

If you have to collect data frequently when writing an mBlock block program, you must then first
learn how to make a list of arrays. The following steps are used to explain it.

Chapter 4 Data and Computations 83

• Step 1: Scripts / Data & Instructions / Make a list Make a list named as: Random List
• Step 2: Make a list named: Random List

Scripts / Data & Instructions /Make a List


2
13

2

Description

In Step 2, the variables are understood to be divided into two types:
1. For all sprites: representing “global variables”, as the main one in this book.
2. For this sprite only: representing “local variables”.

• Step 3: Displays the relevant blocks and content of the “List”.

Displays the contents of the current list Check

84 Scratch (mBlock) Program Design — Using mBot Robot

• Step 4: Manually add and remove elements from the “Random List”.

Manually add elements Manually remove elements
to the “Random List” from the “Random List”

Delete
Add

4-3.2 Delete List

In the previous unit, we are able to use mBlock block program to create the required “List”. We are
also able to delete the old list when we do not need it. The following steps are used to explain it.

Answer

1. Right-click 2. Click “Delete List”

Chapter 4 Data and Computations 85

4-4
General Computation of Lists

Upon completion of the previous unit, it will automatically generate the specified “List name” as
well as a list of related blocks. As shown in the following table:

Related blocks to the List

Blocks Functions

List name

“Add” to the list

“Delete” the specified item from the list

“Insert” data to the location specified in the list

“Updates” the contents of the specified
location in the list
“Obtain” the contents of the specified location
in the list

“Calculate” the number of elements in a list

Determines whether an item “contains” in the
list

“Show” contents in the list is in the stage area

“Hide” contents in the list is not in the stage
area

86 Scratch (mBlock) Program Design — Using mBot Robot

Implementation 1

When the user presses the “button”, a random value (0 ~ 100) will be generated every second. It
will be displayed and stored in the list. Assume a total of six is generated.

Answer

Flow chart

When double-clicking the
“mBot program”

Count = 0

The judgment button
is pressed

True

Count <6

True

Random value generated
added to the Random list

Wait for 1 second

Count = Count + 1

mBlock program Result

Chapter 4 Data and Computations 87

Implementation 2

When the user presses the “button”, the “ultrasonic sensor” will for every second, automatically
detect the distance in the front. It will be displayed and stored in the list. Assume a total of six is
generated.

Answer

Flow chart

When double-clicking the
“mBot program”

Count = 0

The judgment button
is pressed

True

Count <6

True

Distance detected
by ultrasonic sensor added to the list

Wait for 1 second
Count = Count + 1

mBlock program Result

88 Scratch (mBlock) Program Design — Using mBot Robot

4-5
Subprogram (Make a Block)

When we write a program, we do not want to repeat writing similar programs. Therefore, the
simplest approach is to single out some of the “duplicate program” and call them Subroutine or
Function but called “Make a Block” in the mBlock.

Definition
It is a program block with independent functions.

Practice Single program

Put together some common and repetitive codes in a single program.

Schematic diagram

Commonly used and repetitive codes

Principle of a subroutine operation

In general, the “original calling program” is called the “main program”, and the “called program” is
called "subroutine". When the main program calls the subroutine, the “actual parameter” is passed to
the “formal parameter” of the subroutine. When the subroutine is done, it returns to the main program
as the “next line of program” to begin again.

Chapter 4 Data and Computations 89

Illustration

Main Sib( ) Sub Name of subroutine (formal parameter)
...... ......
...... End Sub
Call….Name of subroutine (actual parameter)
......
......
......
Call….Name of subroutine (actual parameter)
End Sub

Description

1. Actual parameters: actual parameter 1, actual parameter 2, ..., actual parameter N.
2. Formal parameters: formal parameter 1, formal parameter 2, ..., formal parameter N.

Program with code blocks Subroutine

Main program

Actual parameter Formal parameter

Advantage

1. It is able to make the program more streamlined by modularizing the duplication of the program.
2. Increase readability of the program.
3. To improve maintainability of the program.
4. Saves memory space occupied by the program.
5. Saves time in writing the program repeatedly.

Disadvantages

Reduce the efficiency of the implementation as the program will Call repeatedly.

90 Scratch (mBlock) Program Design — Using mBot Robot

4-5.1 Create a Subroutine

In writing mBlock graphical program, we will want to write a separate function “subroutine” for
ease of carrying out maintenance work later. We will next explain how to create a subroutine.

• Step 1: Scripts / Data & Block / Make a Block

• Step 2: Fill in the subroutine name: My subroutine

Scripts / Data & Instructions /Make a List Make a list named as: Random List




2

2

3 Subroutine

After completion, as shown below:
Scripts / Data & Block / Make a List

Scripts

Subroutine

Chapter 4 Data and Computations 91

4-5.2 Subroutine Call without Parameters

Definition
There is no transfer of parameters to the “subroutine” when the “main program” calls. Nor will the
“subroutine” return the value to the main program after the completion.

Practice

Write “subroutine” and call it from “main program”.

Implementation

Please design a main program to call a subroutine. If successful, display “subroutine test ok!”

Main program Subroutine

Results

4-5.3 Subroutine Call with Parameters

Definition
Several parameters are transferred to the “subroutine” when the “main program” calls. However
the “subroutine” does not return the value to the “main program” after the completion.

Purpose

Improve the practicality and flexibility of the subroutine.

Practice

The “main program” passes parameters to the “subroutine” while calling it at the same time.

92 Scratch (mBlock) Program Design — Using mBot Robot

Subroutine with defined parameters

12

3

Implementation

Please write a main program where the “results of two subjects” are transferred to the subroutine to
have the total score calculated.

Main program Subroutine

Results

Chapter 4 Data and Computations 93

Chapter

4 Review questions

1. When the user presses the “button”, a score (0 ~ 100) will be randomly generated every
second and stored in the list. The contents of the list as well as the calculated total and
average score will be displayed. Assume a total of six courses.

2. When the user presses the “button”, a dice number (1 ~ 6) will be randomly generated
every second and stored in the list. The number generated will also be displayed each
time. Assuming a total of 10 times is thrown.

94 Scratch (mBlock) Program Design — Using mBot Robot

Chapter 5

CHAPTER OBJECTIVES

• To let the reader understand the
structures of the threefold structures
of process control in the design of a
Lego robot program.

• To let the reader understand when
to use loop and switch, as well as
their uses.

CHAPTER OUTLINE

5-1 T h e T h re e f o l d S t r u c t u re o f
Process Control

5-2 Sequential
5-3 Switch
5-4 Loop

Programming
Process Control

5-1
The Threefold Structure of Process Control

Introduction

When we write an mBlock program, we often write according to what is required. We could write
a series of blocks of commands so that when an event happens, it will implement a set of actions based
on the “different situation” as well as constantly checking for changes in the environment. Thus, in
order to be able to complete the above procedures, we must learn the threefold structure in the process
control of the block programming.

Threefold Structure of Process Control

Sequential Switch Loop

Description
mBot program is composed of a combination of the above three basic structures.

1. Sequential It is when the program implements it from top to bottom, one after
another.

Example

When the user presses the “button”, the mBot runs forward 3 seconds before stopping. It then lets
out a “beep” sound.

96 Scratch (mBlock) Program Design — Using mBot Robot

Answer

Flow chart mBlock program

When double-clicking the
“mBot program”

The judgment button
is pressed

True

Run forward 3 seconds

mBot stops

Make a “beep” sound

2. Switch It selects a different execution path according to the “conditional
expression”.

Example

When the user presses the “button” and the “light sensor” has a light value greater than 500, the
mBot will run forward. Otherwise it will only “beep” once.

Answer

Flow chart mBlock program

When double-clicking the
“mBot program”

The judgment button False
is pressed

True
True

Light source value> 500

Run forward 3 seconds mBot stops
Make a “beep” sound

Description

1. Refer to Chapter 8 for a detailed description of “Light Sensor”.
2. If the switch is used alone, it can only detect once. It is not able to execute it repeatedly.

Chapter 5 Programming Process Control 97

Solution

Together with “Loop”, the robot's actions can be operated repeatedly.

3. Loop Refer to a section of the “block” being executed repeatedly.

Example

When the user presses the “button” and the “light sensor” has a light value greater than 500, the
mBot will run forward. Otherwise it will “beep” and repeat this action.

Answer

Flow chart mBlock program

When double-clicking the
“mBot program”

The judgment button
is pressed

True

True Light source value> 500 False

Run forward 3 seconds mBot stops
Make a “beep” sound

Description

Please refer to Chapter 8 for a detailed description of “Light Sensor”.
From the above block program, we now understand that “loop” + “switch” must be used to
“execute repeatedly” a specific “judged event”.

98 Scratch (mBlock) Program Design — Using mBot Robot

5-2
Sequential

Introduction
It is when the program implements it from top to bottom, a series of Block program one after the other,
during which there is no situation for switch and loop.

Common Blocks

1. To continue the preceding 2. To wait for a condition to 3. Stop the specific program

act or conduct be true

Example

When the “button” of the mBot is pressed, it will begin to run forward until the “ultrasonic sensor”
detects a wall in front of it. When that happens, the robot will turn back and then run forward again. It
will stop when the “line-follower sensor” detects a black line.

mBlock program
Answer

Flow chart mBlock program

When double-clicking the Turns back
“mBot program”
Runs forward
The judgment button
is pressed Detects a black line

True True

Runs forward mBot stops

Distance detected <10

True

Chapter 5 Programming Process Control 99

Advantages

1. Very easy to read as it is from top to bottom.
2. The structure is simple and there are no complex changes.

Disadvantages

1. Unable to express the complexity of the conditional structure.
2. Although you can express loop repetitively, but you often have to write a long block program.

Application

1. There is no need to judge situations.
2. There is no repetitive writing.

Case Analysis

Scenario 1: Let the robot’s motor stop automatically after running forward 3 seconds.
Scenario 2: Let the robot’s motor run forward 3 seconds, turn right and then run forward again for

another 3 seconds.
Scenario 3: Let the robot move along a square.

Illustration

Scenario 1 Scenario 2 Scenario 3

In the above figure, although you can use the “Sequential” approach to “scenario three” but the
Block program is longer and not professional enough. Therefore, it is better to use “Loop” instead.

100 Scratch (mBlock) Program Design — Using mBot Robot


Click to View FlipBook Version