Table of Contents

Objective

This lesson will show you how to program the mBot Neo to respond to sound. 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 “Make a loud sound (clap) to start and press the A button 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 Operators selector and drag the “( ) > 50” block to the coding pane and snap it into the “wait until < >” block

    Greater than block

  13. Click the Sensing selector and drag the “loudness” block to the coding pane and snap it into the “( ) > 50” block

    This allows you to control when the mBot Neo starts running the obstacle avoidance after you power it on by making a loud sound

    Loudness block

  14. Change the “50” to “75” in the “( ) > 50” block

    Set loudness value

  15. 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

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

    If-Then-Else block

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

    Greater than block

  18. 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

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

    Set distance value

  20. 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

  21. 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

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

    Equals block

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

    Pick Random block

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

    Set random test value

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

    Set random range

  26. 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

  27. 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

  28. 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

    Turn right block

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

    Button pressed event block

  30. 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

  31. Change the name of the program in the upper left to “SoundDetect”

    Change Program Name

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

    Connect to CyberPi

  33. Click the Upload Code button and wait for the upload to complete

    Upload program to mBot

  34. 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. Make a loud sound (clap) to start the mBot Neo moving
  4. When you're finished watching the mBot Neo avoid obstacles, press the A button

What should change in your program to make the mBot Neo respond to quieter sounds? 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"