===== 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 [[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:lesson6:s1.png |Open mBlock app}}
- Click on the Extension button\\ \\ {{ :mbotneo:lesson6:s2.png |Add new extension}}
- Click the Add button under the mBot2 shield\\ \\ {{ :mbotneo:lesson6:s3.png |Select mBot2 extension}}
- Click on the Extension button\\ \\ {{ :mbotneo:lesson6:s4.png |Add new extension}}
- Click the Add button under the Ultrasonic Sensor 2\\ \\ {{ :mbotneo:lesson6: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:lesson6:s6.png |Select Upload mode}}
- Click the Events selector and drag the "when CyberPi starts up" block to the coding pane\\ \\ {{ :mbotneo:lesson6: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:lesson6:s8.png |Display block}}
- 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\\ \\ {{ :mbotneo:lesson6: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:lesson6:s10.png |Wait until block}}
- Click the Operators selector and drag the "( ) > 50" block to the coding pane and snap it into the "wait until < >" block\\ \\ {{ :mbotneo:lesson6:s11.png |Greater than block}}
- 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 {{ :mbotneo:lesson6:s12.png |Loudness block}}
- Change the "50" to "75" in the "( ) > 50" block\\ \\ {{ :mbotneo:lesson6:s13.png |Set loudness value}}
- 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:lesson6:s14.png |Forever block}}
- Drag the "if < > then else" block to the coding pane and snap it inside the forever block\\ \\ {{ :mbotneo:lesson6:s15.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:lesson6:s16.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:lesson6:s17.png |Ultrasonic sensor block}}
- Change the "50" to "10" in the "( ) > 50" block\\ \\ {{ :mbotneo:lesson6:s18.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:lesson6:s19.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:lesson6:s20.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:lesson6:s21.png |Equals block}}
- Drag the "pick random 1 to 10" block to the coding pane and snap it inside the "( ) = 50" block\\ \\ {{ :mbotneo:lesson6:s22.png |Pick Random block}}
- Change the "50" to "1" in the "( ) = 50" block\\ \\ {{ :mbotneo:lesson6:s23.png |Set random test value}}
- Change the "10" to "2" in the "pick random 1 to 10" block\\ \\ {{ :mbotneo:lesson6:s24.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:lesson6:s25.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:lesson6:s26.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:lesson6:s27.png |Turn right block}}
- Click the Events selector and drag the "when button A pressed" block to an empty area of the coding pane\\ \\ {{ :mbotneo:lesson6:s28.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:lesson6:s29.png |Restart CyberPi block}}
- Change the name of the program in the upper left to "SoundDetect"\\ \\ {{ :mbotneo:lesson6:s30.png |Change Program Name}}
- Slide the power switch on the mBot2 Shield to the ON position and click the Connect button\\ \\ {{ :mbotneo:lesson6:s31.png |Connect to CyberPi}}
- Click the Upload Code button and wait for the upload to complete\\ \\ {{ :mbotneo:lesson6:s32.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
- Make a loud sound (clap) to start the mBot Neo moving
- 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
[[:mbotneo:start|Return to Main mBot Neo page]]