This page describes how to write an obstacle avoidance program for mBot Neo with the mBlock 5 app on your smart device.
Before you start, make sure that you've got:
A tablet or smart phone
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:
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) Power on mBot Neo.
(3) Tab to open the mBlock 5 app.
(4) Tab Coding, and tab + on the page that appears.
(5) In the sprite library, choose Devices > CyberPi and tab √.
(6) Tab the CyberPi setting icon and then tab Connect on the device setting page that appears.
(7) Place the tablet close to mBot Neo.
mBlock 5 automatically searches for and connects to mBot Neo.
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.
(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.
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.
After the extensions are added, you can find the blocks in the blocks area.
(3) Set the programming mode to Upload.
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.
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.
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.
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.
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.
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.
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.
5. Execute the program
Press button B on CyberPi.
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.
- 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.