Table of Contents

Objective

This lesson will show you how to program the mBot Neo to avoid obstacles. It will then show you how test your program on the mBot Neo.

Parts Required

The parts below are required to complete this lesson. Note that all parts except the styrofoam blocks are included in the mBot Neo kit that can be purchased on Amazon.

  1. Assembled mBot Neo from Lesson 1
  2. USB Cable
  3. Styrofoam Blocks that are at least 5in tall and 2in thick

Software Programming

  1. Connect the USB cable to the CyberPi on the mBot Neo and plug the cable into your computer

    This will charge up your mBot Neo while you create the program

  2. Open the mBlock app on your computer

    You can download the mBlock app from mBlock Download if it isn't already installed

    Open mBlock app

  3. Click on the Extension button

    Add new extension

  4. Click the Add button under the mBot2 shield

    Select mBot2 extension

  5. Click on the Extension button

    Add new extension

  6. Click the Add button under the Ultrasonic Sensor 2

    Select ultrasonic sensor extension

  7. Click the Upload button under the Mode Switch section

    This allows you to write your program without having the mBot Neo connected to your computer

    Select Upload mode

  8. Click the Events selector and drag the “when CyberPi starts up” block to the coding pane

    Startup block

  9. Click the Display selector and drag the “print makeblock and move to a newline” block to the coding pane and snap it to the bottom of the stack

    Display block

  10. Change the “makeblock” to “Press B to start obstacle avoidance and A to stop” in the “print makeblock and move to a newline” block

    Set display message

  11. Click the Control selector and drag the “wait until < >” block to the coding pane and snap it to the bottom of the stack

    Wait until block

  12. Click the Sensing selector and drag the “button A pressed?” block to the coding pane and snap it into the “wait until < >” block

    This allows you to control when the mBot Neo starts running the obstacle avoidance after you power it on

    Button A pressed block

  13. Change the “A” to “B” in the “when button A pressed” block

    Set B button

  14. Click the Control selector and drag the “forever” block to the coding pane and snap it to the bottom of the stack

    This will allow your program to keep running until you press the A button on the CyberPi

    Forever block

  15. Drag the “if < > then else” block to the coding pane and snap it inside the forever block

    If-Then-Else block

  16. Click the Operators selector and drag the “( ) > 50” block to the coding pane and snap it into the if - then block

    Greater than block

  17. Click the Ultrasonic Sensor 2 selector and drag the “ultrasonic 2 1 distance to an object (cm)” block to the coding pane and snap it into the “( ) > 50” block

    Ultrasonic sensor block

  18. Change the “50” to “10” in the “( ) > 50” block

    Set distance value

  19. Click the mBot2 Chassis selector and drag the “moves forward at 50 rpm” block to the coding pane and snap it inside the top section of the if-then-else block

    This will cause the mBot to move forward whenever it does not sense an obstacle within 10cm

    Move forward block

  20. Click the Control selector and drag the “if < > then else” block to the coding pane and snap it inside the bottom section of the if-then-else block

    If-Then-Else block

  21. Click the Operators selector and drag the “( ) = 50” block to the coding pane and snap it inside the if-then block

    Equals block

  22. Drag the “pick random 1 to 10” block to the coding pane and snap it inside the “( ) = 50” block

    Pick Random block

  23. Change the “50” to “1” in the “( ) = 50” block

    Set random test value

  24. Change the “10” to “2” in the “pick random 1 to 10” block

    Set random range

  25. Click the mBot2 Chassis selector and drag the “turns left 90 until done” block to the coding pane and snap it inside the top section of the if-then-else block

    This will cause the mBot Neo to turn left whenever it senses an obstacle within 10cm and the random number is 1

    Turn left block

  26. Drag the “turns left 90 until done” block to the coding pane and snap it inside the bottom section of the if-then-else block

    Turn left block

  27. Change the “left” to “right” in the “turns left 90 until done” block

    This will cause the mBot Neo to turn right whenever it senses an obstacle within 10cm and the random number is not 1

    Set random range

  28. Click the Events selector and drag the “when button A pressed” block to an empty area of the coding pane

    Button pressed event block

  29. Click the Control selector and drag the “restart CyberPi” block to the coding pane and snap it to the bottom of the when button A pressed block

    This will cause the mBot Neo to restart things when when the A button is pressed no matter what is happening in the forever loop

    Restart CyberPi block

  30. Change the name of the program in the upper left to “ObstAvoid”

    Change Program Name

  31. Slide the power switch on the mBot2 Shield to the ON position and click the Connect button

    Connect to CyberPi

  32. Click the Upload button and wait for the upload to complete

    Upload program to mBot

  33. Slide the power switch on the mCore mainboard on the mBot to the OFF position and disconnect the USB cable from the mBot Neo

Robot Testing

Follow the steps below to test out the program you just created

  1. Place some of the styrofoam blocks on the floor
  2. Place the mBot on the floor and slide the power switch on the mBot2 Shield on the mBot Neo to the ON position
  3. Press the B button on the CyberPi to start the mBot Neo moving
  4. When you're finished watching the mBot Neo avoid obstacles, press the A button
  5. Create a maze out of the styrofoam blocks and see if the mBot Neo can find its way out of it

What should change in your program to make the mBot Neo only turn left when it senses an obstacle? Make that change and see if it works

When you're ready to try out changes to your program, connect the USB cable to the CyberPi on the mBot Neo, plug the cable into your computer, and repeat steps 31 through 33 above

Return to Main mBot Neo page

"Therefore, if anyone is in Christ, he is a new creation. The old has passed away; behold, the new has come. - 2 Corinthians 5:17"