Programming mBot Neo with the Mobile mBlock 5 App

This page describes how to write an obstacle avoidance program for mBot Neo with the mBlock 5 app on your smart device.

1. Download the mBlock 5 app on the tablet

2. Connect mBot Neo to the mBlock 5 app

3. Start to write the program

4. Upload the program to mBot Neo

5. Execute the program

 

Before you start, make sure that you've got:

mceclip0.png or mceclip1.png

A tablet or smart phone

mceclip2.png

mBot Neo
If you haven't completed the building of mBot Neo, refer to "Building mBot Neo" to build it first.

Note:

The smart device described on this page is a tablet, and you can use the mBlock 5 app on smart phones in the same way.

1. Download the mBlock 5 app on the tablet

(Skip this step if you have already downloaded the mBlock 5 app.)

Use the tablet to scan the following QR code:

qrcode-ios-cn.png
Alternatively, search for mBlock in app stores to download it.

2. Connect mBot Neo to the mBlock 5 app

(1) Enable the Bluetooth function on the tablet.

2-1.png

(2) Power on mBot Neo.

2-2.png

(3) Tab to open the mBlock 5 app.

2-3.png

(4) Tab Coding, and tab + on the page that appears.

2-4.png

(5) In the sprite library, choose Devices > CyberPi and tab .

2-5.png

(6) Tab the CyberPi setting icon and then tab Connect on the device setting page that appears.

2-6.png

(7) Place the tablet close to mBot Neo.

mBlock 5 automatically searches for and connects to mBot Neo.

2-7.png

After the connection is complete, a voice message is played, indicating that the connection is successful.

3. Start to write the program

(1) Tab Back to coding.

3-1.png

(2) Add the extensions required in the obstacle avoidance program.

To avoid obstacles, mBot Neo uses its ultrasonic sensor 2 to detect obstacles in its way and then respond to them by using the motors and wheels connected to mBot2 Shield. Therefore, you need to add the mBot2 and Ultrasonic Sensor 2 extensions.

Add them as follows:

a. Tab + Extension at the bottom of the blocks area, and then tab + Add under mBot2 on the Extension Center page that appears.

3-2.png

b. Tab + Extension at the bottom of the blocks area, and then tab + Add under Ultrasonic Sensor 2 on the Extension Center page that appears.

3-2-2.png

After the extensions are added, you can find the blocks in the blocks area.

3-2-3.png

(3) Set the programming mode to Upload.

3-3.png

Live: In this mode, you can view the program execution effect in real time, which facilitates the debugging of the program. In this mode, you must keep mBot Neo connected to mBlock 5. If they are disconnected, the program cannot be executed.

Upload: In this mode, you need to upload the compiled program to mBot Neo. After being successfully uploaded, the program can still run properly on mBot Neo when it is disconnected from mBlock 5.

(4) Drag the blocks required to the scripts area.

a. First, define the event for triggering the execution of the obstacle avoidance program. For example, trigger the program by pressing button B.

Tab the Events category, drag the block when button () pressed to the scripts area, and tab the drop-down list box to select button B.

3-4.png

b. Define how many times the program is to be executed. For example, keep executing the program after it is triggered.

Tab the Control category and drag the block forever to the scripts area.

3-4-2.png

c. Define the specific conditions for avoidance and how mBot Neo responds to an obstacle.

Define the condition

For example, mBot Neo starts to avoid an obstacle when it is 9cm or less away from mBot Neo; and mBot Neo keeps moving forward when no obstacle is found or an obstacle is more than 9cm away from it.

Tab the Control category and drag the block if () then () else () to the scripts area.
The condition involves the operation of distance.
Tab the Operators category and drag the block () > () to the scripts area and put it into the condition block.

3-4-3.png

Tab the value 50 to change it to 9.

Tab the Ultrasonic Sensor 2 category and drag the block ultrasonic 2 () distance to an object (cm) to the scripts area and put it into the operation block.

3-4-4.png

Define the responses
When an obstacle is more than 9cm away from mBot Neo, mBot Neo keeps moving forward.
Tab the mBot2 Chassis category and drag the block () at () RPM to the scripts area and put it into the condition block.

3-4-5.png

When an obstacle is 9cm or less away from mBot Neo, mBot Neo turns left.
Drag the block () at () RPM for () secs to the scripts area, put it into the condition block, and tab the drop-down list box to select turns left.

3-4-6.png

Now, a simple obstacle avoidance program is completed.

4. Upload the program to mBot Neo

Note: The program you upload will replace the last program you executed on mBot Neo.

Tab the Upload icon, wait till the uploading is complete, and tab OK.

4.png

5. Execute the program

Press button B on CyberPi.

5.png
mBot Neo starts to move forward and detects obstacles. When an obstacle is found and it's 9cm or less away from mBot Neo, mBot Neo turns left for one second and then moves forward.


Tips:

  • You can change the parameter values on the blocks (such as changing the distance threshold or responses) or add more conditions to compile your own obstacle avoidance programs.
  • You can also compile other programs (such as a line following program) in the similar way.
Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.