Program mBot Neo with the mBlock App

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

1. Download and install the mBlock app

2. Connect mBot Neo to the mBlock app

3. Write a program

4. Upload the program to mBot Neo

5. Run the program

 

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

mceclip0.png or mceclip1.png

A tablet or smartphone

mceclip2.png

mBot Neo
If you haven't completed the building of mBot Neo, build it by referring to Assemble mBot Neo/mBot2 first.

 

1. Download and install the mBlock app

Scan the QR code below with your smart device.
Alternatively, you can search for mBlock in the App Store.

2. Connect mBot Neo to the mBlock app

(1) Enable Bluetooth on your smart device.

1280X1280_-_2023-03-13T150600.006.PNG

(2) Power on mBot Neo.

2-2.png

(3) Tap to open the mBlock app.

2-3.png

(4) Tap Coding, and tap + on the page that appears.

2-4.png

(5) In the Sprite Library, choose Devices > CyberPi and tap .

2-5.png

(6) Tap the CyberPi setting icon and then tap Connect on the Device setting page that appears.

2-6.png

(7) Place your smart device close to mBot Neo.

mBlock 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. Write a program

(1) Tap Back to coding.

3-1.png

(2) Add the extensions required for 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 shield and Ultrasonic Sensor 2 extensions.

Add them as follows:

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

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

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 running effect in real time, which facilitates the debugging of the program. In this mode, you must keep mBot Neo connected to mBlock. If they are disconnected, the program can't be run.

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

          Note: Upload mode is not available for mBot Neo in the mBlock app. Use your computer to upload programs.

(4) Drag blocks to the scripts area.

a. Define the time when the program is triggered by selecting an appropriate block from the Events category. For example, select the block when button () pressed.

Tap Events, drag the block when button () pressed to the scripts area, and tap the drop-down list box to select button B, so as to trigger the program by pressing button B.

3-4.png

b. Define the number of times the program is to be run. For example, keep the program running after it is triggered.

Tap Control and drag the block forever () to the scripts area.

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

Define the condition

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

Tap Control and drag the block if () then () else () to the scripts area.
Set the condition with a distance.
Tap Operators and drag the block () > () to the scripts area and put it in the condition block.

Tap the value 50 to change it to 9.

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

3-4-4.png

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

3-4-5.png

When an obstacle is 9 cm or less away from mBot Neo, mBot Neo turns left.
Drag the block () at () RPM for () secs to the scripts area, put it in the condition block, and tap 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 ran on mBot Neo.

Tap the Upload icon, wait till the uploading is complete, and tap OK.

4.png

 

5. Run the program

Press button B on CyberPi to run the program.

5.png
See if it is as expected that mBot Neo starts to move forward and detects obstacles. When an obstacle is detected within 9 cm away from mBot Neo, mBot Neo turns left for one second and then moves forward.

          Note: Keep mBot Neo within the Bluetooth connection range of your smart device.


Tips:

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

Comments

0 comments

Please sign in to leave a comment.