13-10
Using Face Panel to Display Bomb Explosion Countdown 10 Seconds
We have learnt how to use the “IR remote control” to control the mBot in Chapter 8 of this book.
However, after using Lego with a face Panel, don’t you think the face Panel should be able to have more
applications? Therefore, in this unit, “direction arrows” will be added to show the current direction of
walking.
Implementation
Please use the “IR remote control” to operate the mBot, as well as use the face Panel to display the
direction.
Answer
mBlock Program
Chapter 13 Innovative Applications When Combining mBot with Lego 251
13-11
Infrared Controlling mBot Face Panel Displays Direction
We have learnt how to use the “IR remote control” to control the mBot in Chapter 8 of this book.
However, after using Lego with a face Panel, don’t you think the face Panel should be able to have more
applications? Therefore, in this unit, “direction arrows” will be added to show the current direction of
walking.
Implementation
Please use the “IR remote control” to operate the mBot, as well as use the face Panel to display the
direction.
Answer
mBlock Program
252 Scratch (mBlock) Program Design — Using mBot Robot
Chapter
13 Review questions
Short Answer Questions
1. IR to control the mBot (face Panel displays direction) _ direction lights.
2. Line-follower with expressions.
3. Please design a marquee, with the content “I love mBot!”, and have it displayed
from left to right.
Chapter 13 Innovative Applications When Combining mBot with Lego 253
Chapter 14
CHAPTER OBJECTIVES
• To let the reader understand the
convenience and use of the Me
UNO Shield extension board.
• To let the reader understand how to
use the Me UNO Shield extension
board to plan the IoT.
CHAPTER OUTLINE
14-1 Arduino and Shield Extension
Board
14-2 Shield Extension Board External
Power Source
14-3 Sensor Usage
14-4 Bluetooth Transmission
14-5 IoT Home Care Planning
Combing Arduino
with Me UNO Shield
Extension Board
14-1
Arduino and Shield Extension Board
This chapter describes the extension board of Arduino UNO. We usually have a breadboard to
connect all sorts of electrical parts when we practice the circuit board.
It always looks messy once connected, as there are plenty of electrical wires. If one of the wires is
not connected well, it is necessary to check each wire carefully. You can see the benefits of using the
extension board in the following two pictures.
Arduino Mainboard +Breadboard Arduino Mainboard+Shield Extension board
You will find that it is neater, as well as easier to connect the sensors, when you use the Shield
extension board pictured on the right.
Arduino Me UNO Shield Overlapping
Me UNO Shield (above)
Arduino (below)
Description
The left picture shows the Arduinouno mainboard, the middle picture shows the Arduino Uno
extension board made by Makeblock Company and the right picture shows how the two boards
look when they are connected together.
256 Scratch (mBlock) Program Design — Using mBot Robot
“Me UNO Shield” has to be selected on the mBlock mainboard in order to use the Shield extension
board. As shown in the diagram below:
Chapter 14 Combing Arduino with Me UNO Shield Extension Board 257
14-2
Shield Extension Board External Power Source
After we have burned the program to the mainboard, we need to be connected to an external power
supply if we are not using the USB power source. Usually a power supply of only 4 No. 3 6V batteries is
fine. However I find that the ultrasonic sensor does not work from a power supply of 6V after testing it.
Instead of 2 14500 lithium battery with a power supply of 8 ~ 8.4V will be used. As shown in the
diagram below:
No. 1 Power hole No. 2 Power hole 14500 lithium battery
Description
The power hole is of male head specifications 2.1, which can be connected to the power hole of
the Shield extension board (No. 1 power hole in the picture on the left).
The switch on the left side of the power hole needs to be turn on before it will be powered on.
If you connect the Arduino board power hole (power hole No. 2 in the picture on the right),
you need not to turn on the switch on the Shield extension board. Please purchase the battery
holder and 2.1 male head from an electronic store.
258 Scratch (mBlock) Program Design — Using mBot Robot
14-3
Sensor Usage
Shield extension board is used exactly the same way as the mBot. It is the same with the mBlock
program. This unit will use the two sensors to implement it. Perhaps you are wondering why this unit
is so simple, but it means that you are already familiar with the connecting of the mBot sensor and its
program design.
Implementation
• Sensor : human body IR sensor, LED colored lights.
• Function : When the infrared ray senses someone, the LED lamp will turn on for 20 seconds.
Description
We see streetlights that are sensor activated, lighting up when someone walks by them,
everywhere in our lives. It can also be used in our homes. Example, when the elderly gets up at
night to go to the toilet and fall because they are too lazy to turn the light on, thinking they know
their own homes very well. In this case, a sensor-activated light can be installed.
Connecting of the sensor is shown in the picture below:
Human body IR sensor LED color light
Description
The human body IR sensor connects to port No. 8 and the LED color light sensor connects to
port No. 3. Perhaps you will ask, “how do I know which port to connect to the sensor”, and
that is the same as matching the color on the mBot. This is also a specific feature of the Shield
extension board. The sensor module products of the Makeblock’s creative company can be
easily used on the Arduinouno.
Chapter 14 Combing Arduino with Me UNO Shield Extension Board 259
Draw a flow chart
Robot starts
IR Senses Loops Loop
human body
LED displays white light LED turns off
for 20 seconds
mBlock program
File: Ch14 sensor applications.sb2
Description
The program is very simple. The readers can carry on adding their own electrical control. During
the summer, the fan will turn on automatically when a person is detected in the living room.
260 Scratch (mBlock) Program Design — Using mBot Robot
14-4
Bluetooth Transmission
An additional Bluetooth module needs to be added to the Shield extension board in order to use
Bluetooth communications. The program is written exactly the same as the mBot’s except that it will
not always store the last received character code after receiving the Bluetooth value, while the mBot
will. For example, mBot will always retain the character C after receiving ABC, while the Shield
extension board will not.
The mobile phone app will not be explained here. You can use ch11 \ ch11_Ex1.apk in Chapter 11
to transmit characters.
Implementation
• Sensor : Bluetooth module, LED color lights.
• Function : Mobile phone App lights up LED lights.
Description
This unit is very similar to Chapter 11. It is almost exactly the same, except for the hardware
device.
Connects to sensor as shown in the picture below:
LED color lights
Bluetooth Module
Description 261
Color LED light sensor connected to port 3, Bluetooth Module connected port 5.Please note
that you do not need to interrupt the connection between the Shield extension board and the
computer after you connect the mobile phone and Bluetooth module, which is not the same as
with the mBot.
Chapter 14 Combing Arduino with Me UNO Shield Extension Board
Draw a flow chart
Robot starts
Waiting for
Bluetooth connection
Received Bluetooth value Status of the LED
ABCDG A = LED 3
B = LED 1
C = LED 2
D = LED 4
G = all LEDs turned off
mBlock program
File: Ch14 sensor applications.sb2
Description
If you have already practiced chapters 11 and 12, I believe you won’t be unfamiliar with the
program code above. The program code is similar, not only can mBlock program be designed
for Makeblock’s products, it can also be designed for the Arduino mainboard.
262 Scratch (mBlock) Program Design — Using mBot Robot
14-5 263
IoT Home Care Planning
In the world of IT, terms such as cloud computing, large data, Web2.0, 3.0 are churned out every few
years. Regardless of the kind of lingo appearing, as long as you master the technology of programming,
you will not be afraid of the emergence of any new lingos as at the end of the day, software is what
is required. IoT’s entity and the perception of its hardware require to be written in the C language.
mBlock program is written using the C language so when you pull a block out, it will produce a code
relative to the C language code. Thus do not think that the block type of programming is very weak.
Once you have learnt mBlock program, you can move on to learn C, C + + language. C language will
be the focus of programming language for IoT devices.
IoT encompasses a great range, because objects have to communicate with each other as well as
have the ability to go on to the Internet. Future objects will have networking capabilities, and this
network includes the use of Bluetooth with mobile phones, or using wifi to connect directly to the
cloud. Once you are on Cloud, there is a much larger use with large data analysis being one of them.
This unit does not have a program to implement but to just write a simple system plan. To
implement all of IoT systems there must be a combination of multiple technologies. This not only
includes the software, but also includes many hardware technologies, and where software includes Web
programs, DB Planning, mobile phone tablet program, MCU and Sensor program etc.
We are planning a structure for the IoT system, which will contain the Web, mobile phones, MCU
and Sensor. Though it is not large but it is enough to show the technology of linking objects.
System Plan
• Plan: IoT Smart Home Care.
• Devices : cloud host, mobile tablet, Shield extension board and sensor.
• Sensor : Bluetooth module, human body IR sensor, gas sensors.
Function
1. Infrared ray can monitor there is any activity in the house where the elderly lives alone. If after a
certain period of time the infrared ray does not detect anything, it could mean that there could a
problem with the elderly.
2. The gas sensor can detect if the elderly has forgotten to turn the gas off. The elderly usually has a
failing memory and thus, modern technology like this can help to monitor it.
Description
The monitored data above can be uploaded to Cloud, watched perhaps by government
departments, or perhaps by an individual watching the situation at home.
Sensors can be used based on specific needs. The mainboard can also be connected directly
to Cloud, then you won’t have to use your mobile phone to transmit the data to the database in
Cloud.
Chapter 14 Combing Arduino with Me UNO Shield Extension Board
Draw a system structure diagram
IoT system structure diagram
Cloud
House mobile phone / Family / management unit /
tablet social service organization
(N families)
N sensors
Description
This is a simple structure diagram. 1 ~ N sensor components are installed at the house.
After the data is processed, it is uploaded to Cloud through an Android device so the family or
management unit can see the situation at the house. Or when a gas leak is detected, you can
immediately send a message to the family member’s mobile phone. The sensor’s line is directly
connected to Cloud, means that the sensor can connect itself to the Internet and directly upload
the data to Cloud.
Programming technology
• Cloud Server: HTML5 CSS3, JQuery, PHP, MariaDB.
• Mobile tablet: (Android Studio) Java, App Inventor 2.
• Hardware MCU: C, C ++, mBlock.
This unit is an initial structure of IoT planning. It requires very rigorous planning and analysis of
the system, which involves software engineering, if you want to implement it. IoT is the future, and not
just with things but of cars and medicine, etc.
It is almost as if the world of technology wants all objects to interconnect with each other. Perhaps
you wish to plan your own networking of things after seeing this. I believe you can also make your own
IoT after you have finished the contents of this book.
264 Scratch (mBlock) Program Design — Using mBot Robot
Chapter 15
CHAPTER OBJECTIVES
• To let the reader understand the
basic principles and use of the
Arduino mainboard and extension
board.
• To let the reader know how to
produce a self-propelled car using
Arduino mainboard and extension
board.
CHAPTER OUTLINE
15-1 Foreword
15-2 Mainboard Introduction
15-3 Assembly Process
15-4 Programming
DIY Arduino Self
Propelled Vehicle
15-1
Foreword
All the parts used from Chapter 1 to Chapter 13 are from the mBot kits and they have a fixed way
of being assembled. Which creative and innovative students or players may find limiting. Therefore,
in this chapter, you will use the board that was ordered from the internet, as a base, along with the
Shield and ArduinoUNO mainboards as described in Chapter 14, to allow users to create their own is a
personal car. As shown in the picture below:
266 Scratch (mBlock) Program Design — Using mBot Robot
15-2
Mainboard Introduction
Arduino is an open-source, single-chip microcontroller that can be connected to a wide variety of
sensors. Examples are ultrasound sensors, IR sensors, LED lights etc.
At present, most of them are applied to biped robots, automatic tracking robot, four-axis flying
machine, IOT networking applications etc. A common Arduino mainboard is as shown below:
Source: http://www.arduino.cc/en/Main/ArduinoBoardUno
The basic introduction to the Arduino mainboard is shown in the table below:
Microcontroller ATMEGA328
Operating Voltage 5V
Input voltage (recommended) 7-12V
Input voltage (limit) 6-20V
Digital I / O pins 14 (PWM provided by 6 outputs)
Analog input pin 6 pieces
EEPROM 1 KB (ATMEGA328)
Oscillation speed 16 MHz
Source: http://www.arduino.cc/en/Main/ArduinoBoardUno
The Shield extension board is based on the extension
board developed by the Arduino mainboard, where the
Arduino mainboard, is originally required together with
the breadboard to do the jumper, is made compatible with
the Makeblock module. A variety of sensors can then be
easily used just by using a RJ25 connector as well as with the
mBlock library to write program codes easily. At present,
Shield is the most often used extension board. This is as
shown in the picture on the right:
Chapter 15 DIY Arduino Self Propelled Vehicle 267
15-3
Assembly Process
It is necessary to understand what materials are needed if you wish to make a personalized self-
propelled vehicle. In this unit, I will list down the minimum parts for making a self-propelled vehicle
(handheld remote control). Please refer to it.
List of materials
mainboard structure x1
Arduino Uno mainboard x 1
Shield extension board x 1
Battery holder x 1 and battery x 2
Bluetooth module x 1
268 Scratch (mBlock) Program Design — Using mBot Robot
Driving motor module x 1
DC motor x 2
Wheel x 2 and tire x 2
Caster wheel x 1
Chapter 15 DIY Arduino Self Propelled Vehicle 269
15-4
Programming
We can start writing an mBlock control program to control the actions of the robot after assembling
a self-propelled vehicle. In this unit, I will implement the “ultrasonic sensor” and “line-follower sensor”
in the mBot kit, as well as the mobile phone App to control the actions of the vehicle.
15-4.1 Sensor Applications
In this unit, you will implement the “ultrasonic sensor” and “line-follower sensor” in the mBot kit.
Implementation 1
Let the robot run move forward. It stops when the “ultrasonic sensor” detects an “obstacle” 25 cm
in front of it.
Schematic diagram Flow chart
WALL When double-clicking the
25 cm “mBot program”
The robot runs forward
False
Detects an obstacle?
True
mBot stops
mBlock program
270 Scratch (mBlock) Program Design — Using mBot Robot
Implementation 2
Let the robot run forward. It stops when the “line-follower sensor” detects a “black line”.
Schematic diagram
Answer
Flow chart mBlock program
When double-clicking the
“mBot program”
The robot runs forward
False
Detects a black line?
True
mBot stops
15-4.2 Mobile Phone App to Control the Robot 271
In this unit, we will use the mobile phone App to control the robot. The program is divided into
“mobile phone App” and “robot” of the program.
Mobile phone App remote control program
Use the App Inventor 2 to write
Program in the Supplement CD: Ch15_car.aia
Robot program
The program below is written using a subroutine.
Chapter 15 DIY Arduino Self Propelled Vehicle
Robot program
1
2
3
4
5
6
7
8
272 Scratch (mBlock) Program Design — Using mBot Robot
Robot program
9
10
11
12
13
Chapter 15 DIY Arduino Self Propelled Vehicle 273
Description
Line 01: Start receiving message.
Line 02: Call Switch and transmit the received message.
Line 03: Switch subroutine used to determine the received value; if it is equal to A, start both
motors to run forward. B runs backward, C turns left, D turns right, G stops.
Line 04: If the incoming value is equal to A, the robot runs forward.
Line 05: If the incoming value is equal to B, the robot runs backward.
Line 06: If the incoming value is equal to C, the robot turns left.
Line 07: If the incoming value is equal to D, the robot turns right.
Line 08: If the incoming value is equal to G, the robot stops.
Line 09: Forward subroutine. The motors are started up with both having positive values, and
the robot runs forward.
Line 10: Backward subroutine. The motors are started up with both having negative values, the
robot runs backward.
Line 11: Left turn subroutine. The motors are started up with one positive and one negative
value, the robot turns left.
Line 12: Right turn subroutine. The motors are started up with one negative and one positive
value, robot turns right.
Line 13: Stop subroutine. The motors are started up with both values as 0, the robot stops.
274 Scratch (mBlock) Program Design — Using mBot Robot
Chapter 16
CHAPTER OBJECTIVES
• To understand the rules of the
competition and the program
design of the competition.
CHAPTER OUTLINE
16-1 Substitution Competition
Rules
16-2 Substitution Competition App
Program
16-3 iPoe Cup mBot Relay
Competition Rules
16-4 iPoe Cup mBot Relay
Competition Program
mBot Substitution
Competition and
iPoe Cup mBot Relay
Competition
16-1
Substitution Competition Rules
1 Robot regulations
1. The robot must be able to be controlled remotely using the radio frequency or infrared ray
(including the use of computer wireless remote control).
2. There can be several bandwidths for the robot’s remote control to switch from. However it will be
responsible for itself should there be any interference from other radio signals.
3. The robot shall not be fitted or use any equipment that will damage or contaminate the venue.
4. The robot must be able to be manipulated by hand using a remote control as well as being self-
reliant, to have the cargoes provided by the convention moved from the stacking area to the
unloading area. This cargo is a semi-circular styrofoam ball about 2 cm in diameter. As shown in
Figure 2.
5. The robot is limited to the use of mBot + ICCI extension mechanism group of shovel trucks, ICCI
extension mechanism group benefits the public product model: A03-M-ICCImBot.
Figure 1 Figure 2
mBot substitution competition reference work Diagram of a 2 cm Semi-circular styrofoam ball
MBot Examples of Remote Cargo Handling Website: https://www.youtube.com/watch?v=wYNL8LQcW4Q
276 Scratch (mBlock) Program Design — Using mBot Robot
2 Competition Venue
1. As shown in Figure 3, the floor of the competition venue is wood that is painted white, and so may
be uneven to some extent. It is divided into 3 areas; 1 for self-propelled, with a black line that is 3
cm wide, and 2 for remote controls etc. 3 styrofoam balls are placed in positions that are marked
with colored circular stickers about 2 cm in diameter. The left and right styrofoam balls are about
15cm away from the styrofoam balls in the middle.
2. The dimensions and colors described or marked on this site regarding the venue are approximate
values; the actual size and color shall stand at the competition venue.
Remote Control Area Self-propelled Area Remote Control Area
Stacking Area
Unloading Area
Cargo Position
(Semi-circular of manipulator
styrofoam ball)
Venue map of mBot Substitution Competition
This gure is not marked according to scale.
The con gurations at the competition venue on the actual day will stand.
Figure 3
3 Competition Regulations
1. Each team is limited to one robot, one to manipulate by hand and another as a player on the
competition floor.
2. The manipulator in the participating teams will draw lots to decide on the order of the game.
3. Before the start of the competition, all participating robots must be placed in the convention’s
designated area. When it’s the team’s turn to play in the next game, the hand manipulator has to
bring his own robot, under the referee’s signal, to the competition floor.
Chapter 16 mBot Substitution Competition and iPoe Cup mBot Relay Competition 277
4. Before the start of the competition, three cargoes (semi-circular styrofoam balls),using the center
line of the stacking area as a benchmark, are neatly placed, about 15 cm apart on both sides from
the middle, on the circular stickers.
5. When the game is ready, the robot is positioned near the stacking area. The manipulator is situated
at the Position of manipulator ready to remotely control the robot to carry the goods.
6. The referee whistles as a command to start the competition. The manipulator controls the robot to
carry the goods from the stacking area. (When transporting goods, the goods must be lifted from
the ground and cannot be moved by either pushing or pulling. There is no limit to the amount of
cargo carried.)
7. When the robot enters the self-propelled area, the other team members can replenish the cargoes
at the three fixed locations. If the car falls off the stage or becomes scattered while under remote
control, they can be placed back into the stacking area. However, if the cargo falls off in the self-
propelled or the unloading area, they shall not be re-shoveled nor cleared away.
8. As soon as the robot enters the self-propelled area, the manipulator must stop remote controlling
the robot (the manipulator must place the remote control on the ground), and the robot will walk
along the black line.
9. When the robot is out of the self-propelled area and entering the remote control area at the other
end, the manipulator can remote control the robot to place the cargo into the unloading area.
10. After the robot place the goods into the unloading area, the manipulator can use the remote
control to direct the robot back to the vicinity of the stacking area (return journey is done by
remote control). The manipulator uses the remote control to have the robot shovels the cargo, and
then without having to wait for the referee's command, the manipulator remotely controls the
robot into the self-propelled area. The competition is in a repeated cycle according to this order
and rules.
11. Each competition lasts 3 minutes.
12. The results of the competition are based on the number of cargoes placed in the unloading area by
the respective teams. Cargoes not placed in the unloading area shall not be counted. If more than
the one team has the same result, the winning team will be the one who dropped the least cargoes
(including those fallen on or outside the board).
13. During the competition, each robot is controlled by a pre-selected remote control. In case of a
sudden interference, the bandwidth of the remote control can be changed after getting approval
from the referee. The competition time is still running when having the bandwidth changed.
14. After the start of each race, there shall be no adjustment or replacement of any components of the
robot (including programs, batteries and circuit boards, etc.). There shall also be no request to
pause the race.
15. The competition venue’s lighting, temperature, humidity, etc. shall be of the environment’s
ordinary levels and the teams shall not request for any change.
16. Any matters not mentioned in these regulations shall be decided by the referee on the spot
according to the actual situation.
4 Reward
• To obtain the rankings and as the excellent team according to the methods of the competition. A
certificate will be given to the instructor and the players.
278 Scratch (mBlock) Program Design — Using mBot Robot
16-2
Substitution Competition Rules
The Substitution Competition places more emphasis on the agility of the controller controlling
the mBot. This unit uses the IR remote control provided by the mBot to remotely control the mBot.
The program design within the IR remote control cannot be ignored even though human is the one
controlling the mBot remotely in the competition. There are 3 important points about the Substitution
Competition, as follows:
1. The speed of its movements can be changed.
2. Automatic line-follower.
3. Shovel control.
The third point, shovel control, is the most important. This chapter will illustrate using a program
implementation. There will be 2 different instructions for the program code for the shovel module; one
does not allow for the shovel to move slowly, the other one does.
Readers, for your own benefit, please look up the science and technology website regarding the
assembly of the machine.
This is a machine done by the author’s team for the readers to examine.
You can use a wire to tie the sides of the shovel together to
prevent the shovel from moving up excessively.
Shovel is in a at state.
You can use a tape to stick at the top to prevent the shovel from throwing the 279
styrofoam ball to the back when lifting it up.
Note The machine has to be in accordance with the regulations of the competition; machines with additional
work done to them are not allowed to take part in certain competitions.
Chapter 16 mBot Substitution Competition and iPoe Cup mBot Relay Competition
This is the position after the shovel goes up. You can see that the wire stops the shovel from
continuing to move up.
Shovel in a state of going up.
This is the position after the shovel goes up. You can see that the wire stops the
shovel from continuing to move up.
Draw a flow chart
Robot starts
IR received value Both wheels run
Up, down, left, right
Shovels lifts up
IR received value and down
A‘C
IR received value Line-follower
5,6
IR received value Changes speed
280 Scratch (mBlock) Program Design — Using mBot Robot
Remote Control Instructions
Make variables and module subroutines:
A: Shovel goes down.
C: Shovel goes up.
Up, down: run forward and run backward.
Left, right: Turns left and turns right.
1: Running speed is low.
2: Running speed is medium.
3: Running speed is high.
5: Starts line-following.
6: Ends line-following.
mBlock program
Make variables and module subroutines:
File: Ch16-1 substitution (infrared version). SB2
Description
The left block is a variable and the right block is a modular subroutine. It is best to have modular
design structure when we design the program so that looking at the main program will be
clearer.
Chapter 16 mBot Substitution Competition and iPoe Cup mBot Relay Competition 281
mBlock program
Main program:
File: Ch16-1 substitution (infrared version). SB2
01
02
03
Description
Line 01: Set the value of the initial speed to 150.
Line 02: Loops Loop, so that the program will implement the Loop in the program continuously.
Line 03: Modular subroutine, the thing to do inside the module is as the Chinese name written
on the module.
mBlock program
Moving subroutine
File: Ch16-1 substitution (infrared version). SB2
01
02
03
282 Scratch (mBlock) Program Design — Using mBot Robot
Description
Line 01: If the status is not in line-following, the variable will be equal to 0. So if it is line-
following, mBot will have no reaction when pressing up, down, left and right on the
remote control.
Line 02: If pressing the arrow keys, up, down, left and right on the remote control will result in
the corresponding actions, when they are not the keys, the left and right wheels stop
completely. Why do we need if-else multi-nested? It is because when we release the
arrow keys after pressing them, the wheels will stop rotating. Thus we use the if-else
multi-nest design in order to achieve a function like that. Of course the readers can
design other methods themselves to achieve that.
mBlock program
The shovel subroutine
File: Ch16-1 substitution (infrared version). SB2
01
02
03
04
Description
Line 01: The shovel goes up when the remote control C button is pressed. By default the line-
follower sensor disconnected port No. 2 so the shovel connects to port 1. No. 1 pin is
at the position of pin11 and 12.The voltage at pin position 11 has to be high in order
for the shovel to have power. The high power voltage at pin position 12 indicates the
shovel goes up and the low voltage indicates it going down.
Line 02: The shovel goes down when the remote control A button is pressed.
Line 03: Pause a thousandth of a second.
Line 04: Pin position 11 gives low power voltage. That is, it does not give motor power to the
shovel, thus the shovel stops moving.
Chapter 16 mBot Substitution Competition and iPoe Cup mBot Relay Competition 283
mBlock program
Line-follower subroutine
File: Ch16-1 substitution (infrared version). SB2
01
02
03
04
05
06
Description
Line 01: When the key, number 5, is pressed on the remote control, the (Start line-following)
variable is set to 1. It means to begin line-following.
Line 02: When the key, number 6, is pressed on the remote control, the (Start line-following)
variable is set to 0. It means to stop line-following.
Line 03: If the status is already at line-following, then the following section of the programs
implemented.
Line 04: When the line-follower sensor detects black line on its left and right, it runs forward.
Line 05: When the line-follower sensor detects black on its left and white on its right, it turns left.
Line 06: When the line-follower sensor detects whiten its left and black on its right, it turns right.
284 Scratch (mBlock) Program Design — Using mBot Robot
mBlock program
Changing speed subroutine
File: Ch16-1 substitution (infrared version). SB2
01
02
03
Description
Line 01:When the key1is pressed on the remote control, the variable (speed) is changed to 100.
Line 02:When the key1is pressed on the remote control, the variable (speed) is changed to 180.
Line 03:When the key1is pressed on the remote control, the variable (speed) is changed to 255.
The Shovel Moves Slowly
You may chance upon something when you imagine and try all sorts of ways to design a program
design. It is best not to be spoon-fed when learning program design as that will limit the scope of your
thoughts. When you come across a problem or a function during the learning stage, first think about it
instead of looking it up on Google. Doing this through time will strengthen your thought process. The
best to learn is to figure it out by you, however it will be more painful for those who do not like to think
or cannot figure it out. In that case, it will beery helpful to look up the internet for references.
The following program is a non-conformist method of design, so the shovel will have similar slow
moving function.
Chapter 16 mBot Substitution Competition and iPoe Cup mBot Relay Competition 285
mBlock program
Only the differences are listed
File: Ch16-1 substitution (infrared version). SB2
01
02
03
This slow moving shovel is very useful in the competition. Another way to move the shovel slowly
is to connect the shovel to a PWM pin. A mobile phone App, using Bluetooth to control the mBot, will
be better than using a remote control.
286 Scratch (mBlock) Program Design — Using mBot Robot
16-3
iPoe Cup mBot Relay Competition Rules
1 Introduction to the rules of the Aibao Cup Team Tournament
The competition uses three robots in a relay race to complete the competition. The robots must be
able to be started either automatically or manually. There are a total of three hurdles with each robot
having to complete one hurdle, in the competition. The winning team is the one completing all the
hurdles, the fastest, in the competition. If a robot is not able to complete the hurdle or fail to do so, the
next robot can be manually started after a wait of 30 seconds.
The first robot is placed in the starting circle and then it walks to into the stop area. Once the
first robot is inside the stop area, the second robot
automatically starts to move forward. The first robot
must stop at the green dotted line in the stop area.
Robots using Makeblock Company's mBots, have an
ultrasonic sensor and a line-follower sensor included in
them. As shown in the picture attached.
2 Competition Venue
Description 287
Map size is 200cm * 150cm, with background color black, and with white lines. The starting
point is the white circle (10 cm in diameter) at the top right of the map. The line used by the
robot to follow is a 2cm wide white line and the robot’s stop area is a green dashed line with a
single cell specification of 15cm * 15cm. The red dot in the obstacle-avoiding area represents
the obstacle, with another smaller red dot 40cm away from it. The length of the last dashed line
is2cm.
Chapter 16 mBot Substitution Competition and iPoe Cup mBot Relay Competition
3 Introduction to the different hurdles
The first hurdle is the line-follower area. The robot must move along the white line quickly. Once
the robot reaches the stop area, it must stop and send a signal to have the second robot start moving
forward.
Hint
• This level has a semi-circular area. The key is how to make the robot turn and move forward
quickly in the semi-circular at the same time.
The second hurdle is the obstacle-avoiding area. The robot must move along the white line while
avoiding the obstacle. The obstacle is a 600 ml bottle. The robot has to be no farther than40cm from the
bottle after avoiding it, otherwise it is considered to have failed.
Hint
• The bottle must not be touched in this hurdle, thus speed is not the focus here. The focus is how
to let the robot go around the bottle and return to the line while staying within the distance.
The third hurdle is at the dashed line area. There are four dashed lines, the line in the black area is
2cm long, and 10cm long in the white area. The time stops when the robot reaches the end circle.
Hint
• This hurdle is a combination of the first two hurdles. The robot not only needs to maintain its
speed but also its position on the white line, which is only 10cm. The main focus is how to have
the robot ignore the black line while correcting the line-follower.
288 Scratch (mBlock) Program Design — Using mBot Robot
16-4
iPoe Cup mBot Relay Competition Program
Teamwork is the main point of a team competition. The robot’s program not only needs to complete
its own hurdle, it also needs to have the like in a relay race, “passing the baton” function. mBot with
IR transmitter and receiver module and 2.4G module or Bluetooth module can achieve the “passing
the baton” function. The following method using infrared ray illustrates the “passing the baton”
function.
mBlock program
“Passing the baton” function_Giving the baton
01
02
03
04
Description
Line 01: Use the variable Status to record the current status.
Line 02: The robot runs forward in a straight line.
Line 03: If it almost runs into another party, it quickly stops and updates its status.
Line 04: Use infrared ray to send signal A.
Chapter 16 mBot Substitution Competition and iPoe Cup mBot Relay Competition 289
mBlock program
“Passing the baton” function_Receiving the baton
01
Description
Line 01: When the signal A is received, it runs forward in a straight line.
Robots in a team competition must have a line-follower function. There are a variety
of algorithms for a line-follower. The following is written by the author, which is easy to
understand and is also calculated very fast. Please note that the default line to follow in this
program is the black line, which cannot be used for the competition.
The line-follower uses the pre-installed line-follower sensor on the mBot. The sensor has
an IR transmitter and receiver. The line-follower sensor will transmit infrared rays to the floor,
which are then reflected back to the receiver. If the floor is black, most of the infrared rays
will be absorbed, while the white floor will reflect most of the infrared rays back. The line-
follower sensor will then sense if the current floor is white or black according to the amount
of reflected rays.
290 Scratch (mBlock) Program Design — Using mBot Robot
mBlock program
Basic line-follower function
File: Basic line-follower .SB2
01
02
03
04
Description
Line 01: Wait for the button to be pressed before entering the main program to avoid any
explosion after the robot boots up.
Line 02: The line-follower sensor runs forward if it is on the black line. The current state is
recorded as 0.
Line 03: The robot veers to the right if the line-follower sensor has black on its left and white on
its right. Thus the speed of the left wheel is adjusted to be slower and the right wheel
to be faster, and its current state is recorded as veering to the right. On the other hand,
if its left is white and its right is black, then the speed of the left wheel is adjusted to be
faster and the right wheel to be slower, and its current state is recorded as veering to
the left.
Line 04: If the line-follower sensor has deviated from the black line, and both its left and right
are white, then it will start rotating fast according to the recorded status in line 03.If it
veers to the left side, then the left wheel “rotates forward” and the right wheel “rotates
backward”.
The author’s method for the dotted line; add a 0.25-second delay to a part of the program
line 04 so that the robot will delay its reaction when it encounters a white area.
Chapter 16 mBot Substitution Competition and iPoe Cup mBot Relay Competition 291
mBlock program
Basic line-follower function_Dotted lines
File: Basic line-follower function_Dotted lines .SB2
01
Description
Line 01: Add a Wait command, Delay 0.25 seconds and then rotates quickly back to the line
when the line-follower sensor reads double white.
292 Scratch (mBlock) Program Design — Using mBot Robot
mBlock program
Basic obstacle-avoiding
File: Basic line-follower function.SB2
01
02
03
Description
Line 01: Determine whether there really is an obstacle in front. Use cumulative variable to
confirm it has really reached the front of an obstacle.
Line 02: If the Check variable is greater than 6, it means there really is an obstacle in front of it.
Begin to avoid the obstacle.
Line 03: After avoiding the obstacle, it must return to the line using the do-while conditional.
Chapter 16 mBot Substitution Competition and iPoe Cup mBot Relay Competition 293