===== 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 [[https://www.makeblock.com/pages/mbot-neo-coding-robot|mBot Neo kit]] that can be purchased on Amazon. - Assembled mBot Neo from [[:mbotneo:lesson1|Lesson 1]] - USB Cable - Styrofoam Blocks that are at least 5in tall and 2in thick ===== Software Programming ===== - 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 - 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 {{ :mbotneo:lesson2:s1.png |Open mBlock app}} - Click on the Extension button\\ \\ {{ :mbotneo:lesson2:s2.png |Add new extension}} - Click the Add button under the mBot2 shield\\ \\ {{ :mbotneo:lesson2:s3.png |Select mBot2 extension}} - Click on the Extension button\\ \\ {{ :mbotneo:lesson2:s4.png |Add new extension}} - Click the Add button under the Ultrasonic Sensor 2\\ \\ {{ :mbotneo:lesson2:s5.png |Select ultrasonic sensor extension}} - 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 {{ :mbotneo:lesson2:s6.png |Select Upload mode}} - Click the Events selector and drag the "when CyberPi starts up" block to the coding pane\\ \\ {{ :mbotneo:lesson2:s7.png |Startup block}} - 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\\ \\ {{ :mbotneo:lesson2:s8.png |Display block}} - Change the "makeblock" to "Press B to start obstacle avoidance and A to stop" in the "print makeblock and move to a newline" block\\ \\ {{ :mbotneo:lesson2:s9.png |Set display message}} - Click the Control selector and drag the "wait until < >" block to the coding pane and snap it to the bottom of the stack\\ \\ {{ :mbotneo:lesson2:s10.png |Wait until block}} - 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 {{ :mbotneo:lesson2:s11.png |Button A pressed block}} - Change the "A" to "B" in the "when button A pressed" block\\ \\ {{ :mbotneo:lesson2:s12.png |Set B button}} - 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 {{ :mbotneo:lesson2:s13.png |Forever block}} - Drag the "if < > then else" block to the coding pane and snap it inside the forever block\\ \\ {{ :mbotneo:lesson2:s14.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\\ \\ {{ :mbotneo:lesson2:s15.png |Greater than block}} - 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\\ \\ {{ :mbotneo:lesson2:s16.png |Ultrasonic sensor block}} - Change the "50" to "10" in the "( ) > 50" block\\ \\ {{ :mbotneo:lesson2:s17.png |Set distance value}} - 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 {{ :mbotneo:lesson2:s18.png |Move forward block}} - 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\\ \\ {{ :mbotneo:lesson2:s19.png |If-Then-Else block}} - Click the Operators selector and drag the "( ) = 50" block to the coding pane and snap it inside the if-then block\\ \\ {{ :mbotneo:lesson2:s20.png |Equals block}} - Drag the "pick random 1 to 10" block to the coding pane and snap it inside the "( ) = 50" block\\ \\ {{ :mbotneo:lesson2:s21.png |Pick Random block}} - Change the "50" to "1" in the "( ) = 50" block\\ \\ {{ :mbotneo:lesson2:s22.png |Set random test value}} - Change the "10" to "2" in the "pick random 1 to 10" block\\ \\ {{ :mbotneo:lesson2:s23.png |Set random range}} - 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 {{ :mbotneo:lesson2:s24.png |Turn left block}} - 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\\ \\ {{ :mbotneo:lesson2:s25.png |Turn left block}} - 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 {{ :mbotneo:lesson2:s26.png |Set random range}} - Click the Events selector and drag the "when button A pressed" block to an empty area of the coding pane\\ \\ {{ :mbotneo:lesson2:s27.png |Button pressed event block}} - 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 {{ :mbotneo:lesson2:s28.png |Restart CyberPi block}} - Change the name of the program in the upper left to "ObstAvoid"\\ \\ {{ :mbotneo:lesson2:s29.png |Change Program Name}} - Slide the power switch on the mBot2 Shield to the ON position and click the Connect button\\ \\ {{ :mbotneo:lesson2:s30.png |Connect to CyberPi}} - Click the Upload button and wait for the upload to complete\\ \\ {{ :mbotneo:lesson2:s31.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 Neo ===== 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 mBot2 Shield on the mBot Neo to the ON position - Press the B button on the CyberPi to start the mBot Neo moving - When you're finished watching the mBot Neo avoid obstacles, press the A button - 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 [[:mbotneo:start|Return to Main mBot Neo page]]