===== Objective =====
This lesson will show you how to program the mBot to avoid obstacles. It will then show you how test your program on the mBot.
===== Parts Required =====
The parts below are required to complete this lesson. Note that all parts except the styrofoam blocks are included in the [[https://www.makeblock.com/pages/mbot-robot-kit|mBot kit]] that can be purchased on Amazon.
- Assembled mBot from [[:mbot:lesson1|Lesson 1]]
- USB Cable
- Styrofoam Blocks that are at least 5in tall and 2in thick
===== Software Programming =====
- Open the mBlock app on your computer\\ \\ You can download the mBlock app from [[https://www.mblock.cc/en/download/|mBlock Download]] if it isn't already installed {{ :mbot:lesson2:s1.png |Open mBlock app}}
- Click on the add button on the Devices tab\\ \\ You can also click the 'X' for the CyberPi device since it won't be used {{ :mbot:lesson2:s2.png |Add new device}}
- Select the mBot and click the OK button\\ \\ {{ :mbot:lesson2:s3.png |Select mBot device}}
- Click the Upload button under the Mode Switch section\\ \\ This allows you to write your program without having the mBot connected to your computer {{ :mbot:lesson2:s4.png |Select Upload mode}}
- Click the Events selector and drag the "when mBot(mCore) starts up" block to the coding pane\\ \\ {{ :mbot:lesson2:s5.png |Startup block}}
- Click the Control selector and drag the "wait until < >" block to the coding pane and snap it to the bottom of the stack\\ \\ {{ :mbot:lesson2:s6.png |Wait until block}}
- Click the Sensing selector and drag the "when on-board button pressed?" block to the coding pane and snap it into the "wait until < >" block\\ \\ This allows you to control when the mBot starts running the obstacle avoidance after you power it on {{ :mbot:lesson2:s7.png |Button pressed block}}
- 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 turn the mBot power off {{ :mbot:lesson2:s8.png |Forever block}}
- Drag the "if < > then else" block to the coding pane and snap it inside the forever block\\ \\ {{ :mbot:lesson2:s9.png |If-Then-Else block}}
- Click the Operators selector and drag the "( ) < 50" block to the coding pane and snap it into the if - then block\\ \\ {{ :mbot:lesson2:s10.png |Less than block}}
- Click the Sensing selector and drag the "ultrasonic sensor port 3 distance (cm)" block to the coding pane and snap it into the "( ) < 50" block\\ \\ {{ :mbot:lesson2:s11.png |Ultrasonic sensor block}}
- Change the "50" to "10" in the "( ) < 50" block\\ \\ {{ :mbot:lesson2:s12.png |Set distance value}}
- Click the Action selector and drag the "turn right at power 50% for 1 secs" block to the coding pane and snap it inside the top section of the if-then-else block\\ \\ This will cause the mBot to turn right whenever it senses an obstacle within 10cm {{ :mbot:lesson2:s13.png |Turn right block}}
- Change the "1" to "0.5" in the "turn right at power 50% for 1 secs" block\\ \\ {{ :mbot:lesson2:s14.png |Set time for turning}}
- Drag the "move forward at power 50%" block to the coding pane and snap it inside the bottom section of the if-then-else block\\ \\ This will cause the mBot to move forward whenever it does not sense an obstacle within 10cm {{ :mbot:lesson2:s15.png |Move forward block}}
- Connect the USB cable to the mCore mainboard on the mBot and plug the cable into your computer
- Slide the power switch on the mCore mainboard on the mBot to the ON position and click the Connect button\\ \\ {{ :mbot:lesson2:s16.png |Connect to mBot}}
- Click the Upload button and wait for the upload to complete\\ \\ {{ :mbot:lesson2:s17.png |Upload program to mBot}}
- Slide the power switch on the mCore mainboard on the mBot to the OFF position and disconnect the USB cable from the mBot
===== Robot Testing =====
Follow the steps below to test out the program you just created
- Place some of the styrofoam blocks on the floor
- Place the mBot on the floor and slide the power switch on the mCore mainboard on the mBot to the ON position
- Press the black button on the front left of the mCore mainboard on the mBot to start the mBot moving
- When you're finished watching the mBot avoid obstacles, pick it up and slide the power switch on the mCore mainboard to the OFF position
What should change in your program to make the mBot 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, repeat steps 16 through 19 above
How could you change the program so that you can tell the mBot to stop moving instead of picking it up when you're done? Make that change and see if it works
How can you make the mBot start the obstacle avoidance using the infrared remote control like it did in [[:mbot:lesson1|Lesson 1]]? Make that change and see if it works
[[:mbot:start|Return to Main mBot page]]