0% found this document useful (0 votes)
23 views

ENGG1320 - Graphical Programming

Uploaded by

fnr87zdp99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

ENGG1320 - Graphical Programming

Uploaded by

fnr87zdp99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

ENGG1320 – Hands-on Project HH Cheung, IMSE

Department of Industrial and Manufacturing Systems Engineering


Faculty of Engineering
The University of Hong Kong

ENGG1320 Engineers in the Modern World


Group Project – Apps Development

1 Objective
➢ Learn to use graphical programming for apps development
➢ Learn to use mBlock (v5) to develop an app for controlling Arduino-based products

2 Introduction
The mBlock, v5.3.5 used in this document, is a graphical programming environment based on Scratch 3.0
Open Source Project that makes it easy to program Arduino projects and create interactive applications.
The mBlock has the following features:
- scratch-style programming
- rich hardware support, such as Arduino Uno, Arduino Mega 2560, Raspberry Pi, BBC micro:bit, etc.
- real-time code generator
- all-in-one easy-to-use solution
- modular and expandable
- internationalization

Page 1 of 31
ENGG1320 – Hands-on Project HH Cheung, IMSE

The brain in an mBot robot is an easy-to-use mCore control board, which is based on Arduino Uno, and
integrates various onboard sensors, such as a buzzer, light sensor, RGB LED, etc., which provides you with
an easier way to start learning electronics and developing interactive products. It has the following features:

- Come with Arduino and Makeblock Library for easy programming


- Include various featured sensors, such as a buzzer, light sensor, RGB LED, etc.
- Support On-board USB type B connector design for long-time use
- Adopt the resettable fuse to prevent the board from getting burnt
- Adopt four colour-labelled RJ25 connectors for easy wiring and expansion with more sensor
modules

Top view of a mCore board

Wiring of an mBot robot


Page 2 of 31
ENGG1320 – Hands-on Project HH Cheung, IMSE

3 Connect the mBot robot to mBlock


The mBlock software provides three ways to connect your mBot robot:
- USB Serial Port
- Bluetooth
- 2.4G Wireless Serial

3.1 Add an mBot robot to the mBlock software


1. Run the mBlock software.

2. Remove default devices (click on a cross symbol at the top-right corner of the device’s icon) from the
Devices Tab if it is not a “mBot”.

Page 3 of 31
ENGG1320 – Hands-on Project HH Cheung, IMSE

3. Click the “Add” icon to add a new device. Click on “mBot” and highlight the star icon on the top-left
corner. Then, click the “OK” button to save and apply changes.

3.2 For USB Serial Port


1. Connect a USB cable from the mBot robot to your computer.
2. Switch on the power of the mBot robot
3. A USB-Serial port will be listed in Windows Device Manager. The Serial Port number “COM3”, in the
picture below, represents the mBot robot connected to the computer.

Page 4 of 31
ENGG1320 – Hands-on Project HH Cheung, IMSE

4. Click the “Connect” button.

5. Select the “USB” Tab. Select the correct Serial Port number and then click the “Connect” button.

6. A “Connected!” message will be shown.

3.3 For Bluetooth


1. Switch on the power of the mBot robot
2. Click the “Connect” button.

Page 5 of 31
ENGG1320 – Hands-on Project HH Cheung, IMSE

3. Select the “Bluetooth” Tab. Select correct mBot robot and then click the “Connect” button.

4. A “Connected!” message will be shown.

3.4 For 2.4G Serial Port


1. Switch on the power of the mBot robot
2. Click the “Connect” button.

3. Select “2.4G” Tab. Select correct mBot robot and then click the “Connect” button.

4. A “Connected!” message will be shown.

Page 6 of 31
ENGG1320 – Hands-on Project HH Cheung, IMSE

4 Update Firmware to an mBot robot


Three types of program/firmware can be programmed to an mBot robot through the mBlock 5 software.
They are online firmware, factory firmware, and user-created programs.
The online firmware is special software, a request-response communication model, running in an mBot
robot. The firmware will wait for a request sending from the master application like mBlock. It executes
the request and then sends back the result as a response. The firmware itself does not implement full
application logic. The application logic is implemented in master applications like mBlock software.

Request (e.g. move forward)


mBlock 5 mBot
Application
Application Response (e.g. done)
Online
Logic Firmware
ApplicationR

The factory firmware is a standalone firmware running in an mBot robot that has three standalone operation
modes. The operation mode named “Mode A” is a remote control driving mode. A user can use IR remote
controller to move the mBot robot. The operation mode named “Mode B” will move the mBot robot forward
until it detects an obstacle using an ultrasound distance sensor module. The operation mode named “Mode
C” is a simple line follower demonstration program. The mBot robot will move along a black line using a
line-follower sensor module.

4.1 Upload online firmware to an mBot robot


1. Connected to an mBot using USB-Serial port as shown in the section “Connect the mBot robot”
2. Click the “Live” button and then click the “Setting” button

Page 7 of 31
ENGG1320 – Hands-on Project HH Cheung, IMSE

3. Click the “Update Firmware” button

4. Select “Online firmware” and then click the “Updates” button

5. A message “The update is finished” will be shown.

4.2 Upload factory firmware to an mBot robot


1. Connected to an mBot using USB-Serial port as shown in the section “Connect the mBot robot”
2. Click the “Live” button and then click the “Update” button

Page 8 of 31
ENGG1320 – Hands-on Project HH Cheung, IMSE

3. Click the “Update Firmware” button

4. Select “Factory firmware” and then click the “Updates” button

5. A message “The update is finished” will be shown.

4.3 Upload user-created program to an mBot robot


1. Connected to an mBot using USB-Serial port as shown in the section “Connect the mBot robot”
2. Click the “Upload” button and then click the “Upload” button

3. A message “The code has been uploaded.” will be shown.

Page 9 of 31
ENGG1320 – Hands-on Project HH Cheung, IMSE

5 Tutorial 1 – Create your first app


Mouse, the small mouse, likes to eat apples very much. Today it breaks into the mBlock world and there is
a sweet apple just in front of it. Can you help Mouse eat that apple? Come on, help Mouse with the mBot
robot at your hand.

5.1 Learning objectives


- Learn to create an app using basic building blocks of the mBlock
- Learn to control onboard LED lights
- Learn to control the on-board buzzer
- Learn to control a ME 7-segment display module

5.2 Electronic Module


5.2.1 The mCore control board

Page 10 of 31
ENGG1320 – Hands-on Project HH Cheung, IMSE

5.2.2 Me 7-Segment Display

• Me 7-Segment Display-Red module adopts a 4-digit common-anode digital tube for displaying the numbers and
a few special characters

5.3 Knowledge Points


Block Shapes Description / Functions
A Hat Block is a start point of a script. It is placed on top of other blocks.
Each hat block uses its way to activate a script such that different scripts can be
executed at different time points and conditions.

A Stack Block is a rectangular shaped block with a notch at the top and a
bump on the bottom that can fit above or below other blocks. Stack blocks are
used to execute commands.
A Reporter Block is a round-shaped block that stores a value that can be a
numerical value or character string. It can be configured to be accessed among
all scripts or just accessed within a single script. A reporter block must be put
into the round-shaped slot of another block, and therefore cannot be used
independently.
A Boolean Block is a hexagon-shaped block that contains a Boolean value
which can be either "true" or "false". A Boolean block must be put into the
hexagonal slot of another block, and therefore cannot be used independently.
A Cap Block is a rectangular shaped block with a notch at the top. It is used
at the end of a script, and therefore can be put only under all blocks.

Building Blocks Instructions Examples


A hat block executes the
subsequent script when
the green flag is clicked.

Waits a specified time


before executing the
subsequent script. The
number can be an integer
or a decimal number.

Page 11 of 31
ENGG1320 – Hands-on Project HH Cheung, IMSE

If the on-board button is


pressed, it reports a true.
Otherwise, it reports a
false.
Waits until the specified
conditions are met
before executing the
subsequent script.
Lights up the specified
LED/LEDs by the
specified RGB colour
values.
The RGB colour value is
specified by an integer
ranging from 0 to 255.
Set all colours to zero to
turn off LED/LEDs.
The onboard buzzer can
output a note from C2 to
D8.
A beat is the duration of
a note to be played. At
general playing speed,
one beat equals one
second.

5.4 Programming Structure


Programming Structure Thinking Process
Sequence structure description:
The script begins running from the first block, Begin
followed by all the other blocks executing in order.
This is the sequential
Instruction block A
The diagram on the right is a standard sequence
structure. After the program starts, it implements
three blocks in turn and finally ends. The sequential Instruction block B
structure is the basis for the running way of the
program.
Instruction block C

Instruction block D

Page 12 of 31
ENGG1320 – Hands-on Project HH Cheung, IMSE

5.4.1 Example script

5.5 Creating your first app


Now, you create the first mBlock program that a mouse moves and eat an apple. The program shows an
animation of a moving mouse and request mBot robot generates a tone when the mouse eats an apple.
When the program starts, the mBot robot plays a sound, set all LEDs to red colour, and displays “0000” in
the 7-segment display module. A mouse sprite will wait for you to click on it in starting position. Once you
click on the mouse sprite, it moves toward the apple. The mBot robot plays another sound, set all LEDs to
green colour, and displays “1111” in the 7-segment display module when the mouse sprite hits the apple
sprite.

Page 13 of 31
ENGG1320 – Hands-on Project HH Cheung, IMSE

5.5.1 How sprites in mBlock interact with a connected device


In mBlock, the resources of device programs and sprite scripts are separated. A sprite cannot directly access
any resources such as an onboard LED/buzzer in a connected device. A mblock extension named “Upload
Mode Broadcast” provides some blocks allowing sprites to interact with a connected device.
Building Blocks Instructions Examples
A hat block executes the
subsequent script when a
specific broadcast
message is received.
A reporter block reports
the value associated with
a specific broadcast
message.
Broadcast a specific
message without
associated value.

Broadcast a specific
message with an
associated value.

5.5.2 How to add “Upload Mode Broadcast” and “Maker’s Platform” extension
1. Select “Upload” mode

2. Click on the “+ extension” button

3. Click on the “Sprite Extensions” tab. Enter “upload” in the search bar and press the “enter” key. Click the “+
Add” button to add the “upload Mode Broadcast” extension for sprites.

Page 14 of 31
ENGG1320 – Hands-on Project HH Cheung, IMSE

4. Click on the “+ extension” button again

5. Click on the “Device Extensions” tab. Enter “upload” in the search bar and press the “enter” key. Click the “+
Add” button to add the “upload Mode Broadcast” extension for devices.

6. Click on the “+ extension” button again

Page 15 of 31
ENGG1320 – Hands-on Project HH Cheung, IMSE

7. Click on the “Device Extensions” tab. Look for the “Maker’s Platform” extension. Click the “+ Add” button to
add the “Maker’s Platform” extension for devices.

5.5.3 Device program and sprites scripts


An mBot program An mBot robot program handles
broadcast messages

1. When a broadcast message


“mode1” is received, the robot
displays “0000” in the 7-
segment display module, set all
LEDs to red colour, and a note
“C4” will be played for “0.5”
beats.
2. When a broadcast message
“mode2” is received, the robot
displays “1111” in the 7-
segment display module, set all
LEDs to green colour and plays
a note “C4” will be played for
“0.5” beats. Then, the robot
waits for 0.2 seconds. Finally, a
note “F2” will be played for
“0.5” beats.

Make sure the device program is


uploaded to an mBot robot and the
mBot robot is connected in the
mBlock software before running the
sprites script.

Page 16 of 31
ENGG1320 – Hands-on Project HH Cheung, IMSE

A mouse sprite script Let the mouse move towards


Apple’s position

1. Press the space bar to start the


program.
2. Let the mouse moves to the
initial position.
3. Broadcast a message named
“mode1” requests an mBot robot
playing a tone from the onboard
buzzer.
4. Wait until the program ended.
5. Move the mouse sprite toward an
apple when the user clicks the
mouse sprite.

An apple sprite script Make a sound when the mouse eats


the apple

1. Press the space bar to start the


program.
2. Wait for the mouse sprite to
move to the start position.
3. Wait for the mouse sprite
touching the apple.
4. Broadcast a message named
“mode2” requests mBot robot
playing a tone from the onboard
buzzer.
5. Stop all running sprites program.
1.

Page 17 of 31
ENGG1320 – Hands-on Project HH Cheung, IMSE

6 Tutorial 2 – Create an app for picking apples


A mouse found a fruitful apple tree today. Now is the ripe season. Apples on the trees constantly fall when
blowing in the wind. The mouse hopes to find a container to hold these apples. Can you help the mouse
collect more apples?

6.1 Learning objectives


- Learn how to control and apply the ultrasonic sensor

6.2 Electronic Modules


Category Name Function Instructions
An ultrasonic sensor is an input The ultrasonic module is marked
device for detecting distance. It has with a yellow label so you need to
two "eyes", one of which is to emit connect it to the interface with a
ultrasonic waves while the other yellow logo on the motherboard.
one is responsible for receiving the
signal bounced back after detecting
an obstacle. Hence, it realizes the
goal of measuring distance.

Detection range: 3cm-400cm;


Detection angle: 30 °;

6.3 Knowledge Points


Building Block Instructions Examples
Executes the enclosed
scripts for the specified
number of times.

Executes the enclosed


scripts repeatedly until the
report condition is met.

Executes the enclosed


scripts repeatedly.

Page 18 of 31
ENGG1320 – Hands-on Project HH Cheung, IMSE

Reports the distance of


obstacles detected by the
ultrasonic sensor that is
connected to the specified
port.
A hat block executes the
subsequent script when the
mBot robot is powered up. A
standalone mBot program
must use this hat block.

Make sure the standalone


mBot program is uploaded
to the mBot robot before
disconnect it from the
computer.

6.4 Programming Structure


Programming Structure Thinking Process
Loop Structure (Cycle Structure):
Begin
The loop structure is the structure that repeats its
contained execution script. As the right flow chart
shows, instruction blocks A and B are called the loop Instruction block A
body. If the loop condition is false, re-execute the loop,
otherwise, the loop ends. When you need to repeat the
same script, we often use the loop structure in
programming. Instruction block B

N Meet the loop


condition

End

Page 19 of 31
ENGG1320 – Hands-on Project HH Cheung, IMSE

6.5 Creating the app for picking apples


A mouse moves to different positions based on the reported distance from the ultrasonic sensor for picking the
falling apples.

6.5.1 An mBot program


An mBot robot program handles
broadcast messages

1. When a broadcast message “tone” is


received, a note “F4” will be played for
“0.25” beats.
2. When an mBot starts up, it will get a
distance reported from the Ultrasonic
distance sensor and then send a
broadcast message named “range” with
the reported distance as message value.
The reported distance will be displayed
in the 7-segment display module.

Make sure the device program is uploaded


to an mBot robot and the mBot robot is
connected in the mBlock software before
running the sprites script.

Page 20 of 31
ENGG1320 – Hands-on Project HH Cheung, IMSE

6.5.2 A mouse sprite script


The mouse moving according to the
distance reported from the Ultrasonic
distance sensor.

1. When the green flag in mBlock is


clicked, the mouse sprite script will
look for any broadcast message
“range” and then calculate the new
position of the mouse sprite based on
the associated value.
2. The mouse sprite will move to the new
position.

6.5.3 An apple sprite script


Keep apples falling

1. Move the apple sprite to a random position on top


of the screen.
2. Move the apple down
3. Move the apple sprite to a random position on top
of the screen if it passes the bottom of the screen.

Stop the program after the mouse collect 8 apples

1. Wait for the mouse to hit the apple


2. Send a broadcast message named “tone” to request
the connected mBot robot to play a tone.
3. Stop the program after 8 apples are collected.

Page 21 of 31
ENGG1320 – Hands-on Project HH Cheung, IMSE

7 Tutorial 3 – Create an app that controls the moving of your vehicle


7.1 Learning Objectives
- Learn how to upload the control app to the mBot robot in offline mode

7.2 Knowledge Points


Building Blocks Instructions Examples
Report the current value
of build-in up-count
timer in mBot robot.
The unit of value is in
terms of seconds.
Reset the value of the
build-in up-count timer
in the mBot robot to
zero.
The mBot robot will be
moved in the specified
direction at the
specified power.
If the specified button
of the IR remote is
pressed, a true is
reported. Otherwise, a
false is reported.

7.3 Programming Ideas


A robot determines the travelling distance according to the number of buttons pressing after being powered up, so it
must run the offline mode.

How does the app run?

1. When first power up the robot, a variable “count” is initialised to zero and reset the timer.
2. Check how many times the onboard button is being pressed within 10 seconds.
3. When the green light is on, places the robot at the starting point of the runway.
4. Press the “B” button on IR remote controller. The robot will move forward for the number of seconds
according to the value of the variable “count” and then stop.

Page 22 of 31
ENGG1320 – Hands-on Project HH Cheung, IMSE

Begin

The counter gets to zero, play a


tone, and set LEDs to blue colour

If the on-board button is pressed, the counter is


added by 1. Display count value in the 7-segment
display module

N
Does it reach
10 seconds?

Play a tone and set LEDs to


green colour

Wait for the “B” button pressed


in IR remote controller

Move robot forward in full power.

Wait for “variable counter” seconds

Stop robot and turn-off LEDs

Page 23 of 31
ENGG1320 – Hands-on Project HH Cheung, IMSE

7.4 Trying to create an app that controls the moving of the robot
7.4.1 An mBot robot program
Script Script Description

These blocks initialize the “Count” variable and


reset the mBot robot’s build-in up-count timer to a
known state to avoid any unexpected result.

These blocks count how many times the onboard


button is being pressed in 10 seconds.

These blocks wait for the “B” button pressed on IR


remote controller.

These blocks move the robot forward for the


number of seconds stored in the variable “Count”
and then stop.

Page 24 of 31
ENGG1320 – Hands-on Project HH Cheung, IMSE

8 Task – Create a simple app to control a robot using a Bluetooth


controller
8.1 Learning Objectives
- Learn how to use a Bluetooth controller to control the moment of the robot in offline mode

8.2 Knowledge Points


A Bluetooth controller features 15 buttons and 2 thumb joysticks for a more flexible Makeblock robot control.

A Bluetooth controller must be paired with an mBot robot to establish a communication link. The communication
link will be auto-connected if the communication link is dropped.

Follows the step below to pair a Bluetooth controller to an mBot robot.

1. Make sure a Bluetooth module is installed in an mBot Robot.

2. Turn on an mBot robot without connecting a USB cable. The blue colour LED on the Bluetooth module will
blink slowly if it does not connect with a Bluetooth device.
3. Turn on a Bluetooth controller.
Page 25 of 31
ENGG1320 – Hands-on Project HH Cheung, IMSE

4. Place Bluetooth Controller close to mBot robot, press the Bluetooth button on the controller until the indicator
blinks faster, and then release the button.
5. Pairing is automatically performed. The indicator is solid in blue if the pairing is successful.

A mBlock extension “Bluetooth controller” provides some blocks to check the status of buttons on the Bluetooth
controller and the reading of joysticks on the Bluetooth controller.

Building Blocks Instructions Examples


If the specified button of the Bluetooth
controller is pressed, a true is reported.
Otherwise, a false is reported.

Report value of the specified axis of the


joystick.

The range of values is from -255 to 255.


The value at the centre position is zero.

Page 26 of 31
ENGG1320 – Hands-on Project HH Cheung, IMSE

8.3 Programming Ideas


A robot determines the travelling distance according to the number of buttons pressing after being powered up, so it
must run the offline mode.

The Idea Description Thinking Process


You can use a Bluetooth
remote controller to
firstly adjust the power of
motors, which is a default
value of 50, by clicking
the button “+” within 10
seconds and then control
the moving of the robot
by direction buttons.

How does the app run?


1. Initialise a variable
keep track of the power of
motors and then reset the
timer
2. Set LEDs on to red
colour
3. Increase the power of
motors if button “+” is
pressed on the Bluetooth
remote controller until
either time is larger than
10 seconds or the power
is equal to 100
4. Set LEDs to green
colour.
5. Use direction buttons
on the Bluetooth remote
controller to control the
movement of the robot.

Page 27 of 31
ENGG1320 – Hands-on Project HH Cheung, IMSE

8.4 Trying to create the app a simple app to control a robot using a Bluetooth controller
8.4.1 An mBot robot program
Script Script Description

These blocks initialize the “Count” variable and


reset the mBot robot’s build-in up-count timer to a
known state to avoid any unexpected result.

These blocks adjust the power of motors in 10


seconds.

These blocks move robot accounting buttons


pressed on the Bluetooth controller.

Page 28 of 31
ENGG1320 – Hands-on Project HH Cheung, IMSE

9 Introduction to a line-follower senor

A line-follower sensor mounted on the bottom of an mBot robot is used for facilitating the robot to move on a black
line; its detection range is 1 to 2 cm. The line-follower sensor consists of two sensors named as Sensor 1 and 2. Each
sensor consists of an infrared emitter (IR LED) and an infrared receiver (Photodiode). The IR LED continually emits
infrared light when the mBot robot is powered on. If the infrared light is reflected (encountering white or other light
colour surfaces) to the photodiode with enough intensity, the sensor will output a logic value 1 (now you can see the
blue LED on the back of the line-follower sensor is lighted). If the infrared light is absorbed or cannot be reflected,
the sensor will not receive enough intensity and the output will be logic 0 (the blue LED on the back of the line-
follower sensor do not light).

According to the table, it can be known that the value of the line-follower sensor can be four only: 0, 1, 2, and 3.

Whether the receiver Binary to


The The
receives the reflected decimal
value value
infrared signal Represented (the value
returned returned
in binary shown on
by by
the stage by
Sensor1 Sensor2 Sensor 1 Sensor 2
the sensor)

Not Not
Situation 1 0 0 00 0
received received

Not
Situation 2 Received 0 1 01 1
received

Not
Situation 3 Received 1 0 10 2
received

Situation 4 Received Received 1 1 11 3

Remark: When there is no object in front of the line-follower sensor or the object is far away from the sensor,
what value is on the line-follower sensor?
When there is no object or the object is far away from the sensor, the infrared light cannot be reflected or the signal
reflected is poor. It is deemed that Sensors 1 and 2 do not receive an infrared signal and the returned value is 0. So
according to Situation 1 in the above table, the value of the line-follower sensor should be 0.

Page 29 of 31
ENGG1320 – Hands-on Project HH Cheung, IMSE

A simple line-follower program

Script Script Description

An mBot robot can be programmed to move


along a black line if a line-follower senor is
adopted for identifying either a black line on a
light-colour surface or a light-colour line on a
black surface with four values/situations, as
shown in the above table.

A black line on a light-colour surface is taken as


an example to explain how a line-follower sensor
is adopted for aiding the mBot to move along the
black line.

The following introduces four situations where


mBot is relative to the black line, which can be
dealt with using the following strategies:

1. if it is on the black line, it keeps moving


forward;
2. if it deviates from the black line, it turns
either left or right in time to keep itself on the
line;
3. if it is not on the black line, it turns back to
find the black line.

Knowledge point

The line-patrolling sensor is the best to identify pure white or black surface without reflection. Commonly you can
use black electrician tape to make a patrolling line on a light colour surface to achieve the best effects.

Page 30 of 31
ENGG1320 – Hands-on Project HH Cheung, IMSE

10 References
1. Makeblock mBot - Tutorial

http://docs.makeblock.com/mbot/en/

2. Makeblock mBlock 5 – Basics

https://www.mblock.cc/doc/en/part-one-basics/basics.html

3. Makeblock mBlock 5 - Interact with Sprites and device

https://www.mblock.cc/doc/en/hardware-basic/interact-with-sprite.html

4. Makeblock Bluetooth Controller – Quick Start Guide

https://www.mblock.cc/doc/en/hardware-guide/bluetooth-controller/quick-start-guide.html

Page 31 of 31

You might also like