===== Objective ===== This lesson will show you how to program the mBot Neo to recognize colors while following a line. 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 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 - Line following paper ===== 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:lesson5:s1.png |Open mBlock app}} - Click on the Extension button\\ \\ {{ :mbotneo:lesson5:s2.png |Add new extension}} - Click the Add button under the mBot2 shield\\ \\ {{ :mbotneo:lesson5:s3.png |Select mBot2 extension}} - Click on the Extension button\\ \\ {{ :mbotneo:lesson5:s4.png |Add new extension}} - Click the Add button under the Quad RGB Sensor\\ \\ {{ :mbotneo:lesson5:s5.png |Select quad RGB 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:lesson5:s6.png |Select Upload mode}} - Click the Events selector and drag the "when CyberPi starts up" block to the coding pane\\ \\ {{ :mbotneo:lesson5: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:lesson5:s8.png |Display block}} - Change the "makeblock" to "Press B to start color line following and A to stop" in the "print makeblock and move to a newline" block\\ \\ {{ :mbotneo:lesson5: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:lesson5: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 color line following after you power it on {{ :mbotneo:lesson5:s11.png |Button A pressed block}} - Change the "A" to "B" in the "when button A pressed" block\\ \\ {{ :mbotneo:lesson5:s12.png |Set B button}} - Click the Variables selector and click the Make a Variable button to create the following variables for all sprites\\ \\ This will allow your program to store information that it needs and change it as it runs {{ :mbotneo:lesson5:s13.png |Create Variables}} - base_power\\ \\ {{ :mbotneo:lesson5:s14.png |Create base_power variable}} - kp\\ \\ {{ :mbotneo:lesson5:s15.png |Create kp variable}} - left_power\\ \\ {{ :mbotneo:lesson5:s16.png |Create left_power variable}} - right_power\\ \\ {{ :mbotneo:lesson5:s17.png |Create right_power variable}} - Drag the "set base_power to 0" block to the coding pane and snap it to the bottom of the stack\\ \\ {{ :mbotneo:lesson5:s18.png |Set variable block}} - Change the "0" to "40" in the "set base_power to 0" block\\ \\ {{ :mbotneo:lesson5:s19.png |Set base_power value}} - Drag the "set base_power to 0" block to the coding pane and snap it to the bottom of the stack\\ \\ {{ :mbotneo:lesson5:s20.png |Set variable block}} - Change the "base_power" to "kp" and the "0" to "0.5" in the "set base_power to 0" block\\ \\ {{ :mbotneo:lesson5:s21.png |Set kp value}} - Click the LED selector and drag the "display" block to the coding pane and snap it to the bottom of the stack\\ \\ This will allow your mBot Neo to display colors on its LEDs {{ :mbotneo:lesson5:s22.png |LED 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 press the A button on the CyberPi {{ :mbotneo:lesson5:s23.png |Forever block}} - Click the My Blocks selector and click the Make a Block button to create the following blocks\\ \\ This will allow you to make your program neater {{ :mbotneo:lesson5:s24.png |Create Blocks}} - follow_line\\ \\ {{ :mbotneo:lesson5:s25.png |Create line_following block}} - detect_colors\\ \\ {{ :mbotneo:lesson5:s26.png |Create enter_station block}} - start_again\\ \\ {{ :mbotneo:lesson5:s27.png |Create start_again block}} - Drag the Define blocks in the coding pane to blank areas\\ \\ {{ :mbotneo:lesson5:s28.png |Define blocks}} - Drag the "follow_line" block to the coding pane and snap it into the forever block\\ \\ {{ :mbotneo:lesson5:s29.png |follow_line block}} - Drag the "detect_colors" block to the coding pane and snap it into the forever block\\ \\ {{ :mbotneo:lesson5:s30.png |detect_colors block}} - Drag the "start_again" block to the coding pane and snap it into the forever block\\ \\ {{ :mbotneo:lesson5:s31.png |start_again block}} - Click the Variables selector and drag the "set base_power to 0" block to the coding pane and snap it to the bottom of the Define follow_line block\\ \\ {{ :mbotneo:lesson5:s32.png |Set variable block}} - Change the "base_power" to "left_power" in the "set base_power to 0" block\\ \\ {{ :mbotneo:lesson5:s33.png |Set left_power value}} - Click the Operators selector and drag the "( ) - ( )" block to the coding pane and snap it into the "set left_power to 0" block\\ \\ {{ :mbotneo:lesson5:s34.png |Subtract operator block}} - Drag the "( ) * ( )" block to the coding pane and snap it into the right side of the "set left_power to ( ) - ( )" block\\ \\ {{ :mbotneo:lesson5:s35.png |Multiply operator block}} - Click the Variables selector and drag the "base_power" block to the coding pane and snap it into the left side of the "set left_power to ( ) - ( ( ) * ( ) )" block\\ \\ {{ :mbotneo:lesson5:s36.png |base_power variable block}} - Drag the "kp" block to the coding pane and snap it into the middle of the "set left_power to (base_power) - ( ( ) * ( ) )" block\\ \\ {{ :mbotneo:lesson5:s37.png |kp variable block}} - Click the Quad RGB Sensor selector and drag the "quad rgb sensor 1 deviation (-100-100)" block to the coding pane and snap it into the right side of the "set left_power to (base_power) - ( (kp) * ( ) )" block\\ \\ {{ :mbotneo:lesson5:s38.png |RGB sensor deviation block}} - Click the Variables selector and drag the "set base_power to 0" block to the coding pane and snap it to the bottom of the Define line_following stack\\ \\ {{ :mbotneo:lesson5:s39.png |Set variable block}} - Change the "base_power" to "right_power" in the "set base_power to 0" block\\ \\ {{ :mbotneo:lesson5:s40.png |Set left_power value}} - Click the Operators selector and drag the "( ) * ( )" block to the coding pane and snap it into the "set right_power to 0" block\\ \\ {{ :mbotneo:lesson5:s41.png |Multiply operator block}} - Drag the "( ) + ( )" block to the coding pane and snap it into the right side of the "set right_power to ( ) * ( )" block\\ \\ {{ :mbotneo:lesson5:s42.png |Add operator block}} - Drag the "( ) * ( )" block to the coding pane and snap it into the right side of the "set right_power to ( ) * ( ( ) + ( ) )" block\\ \\ {{ :mbotneo:lesson5:s43.png |Multiply operator block}} - Change the first of the "set right_power to ( ) * ( ( ) + ( ( ) * ( ) ) )" block to "-1"\\ \\ {{ :mbotneo:lesson5:s44.png |base_power variable block}} - Click the Variables selector and drag the "base_power" block to the coding pane and snap it into the second of the "set right_power to (-1) * ( ( ) + ( ( ) * ( ) ) )" block\\ \\ {{ :mbotneo:lesson5:s45.png |base_power variable block}} - Drag the "kp" block to the coding pane and snap it into the third of the "set right_power to (-1) * ( (base_power) + ( ( ) * ( ) ) )" block\\ \\ {{ :mbotneo:lesson5:s46.png |kp variable block}} - Click the Quad RGB Sensor selector and drag the "quad rgb sensor 1 deviation (-100-100)" block to the coding pane and snap it into the right side of the "set right_power to (-1) * ( (base_power) + ( (kp) * ( ) ) )" block\\ \\ {{ :mbotneo:lesson5:s47.png |RGB sensor deviation block}} - Click the mBot2 Chassis selector and drag the "encoder motor EM1 rotates at 50% power, encoder motor EM2 rotates at 50% power" block to the coding pane and snap it to the bottom of the Define line_following stack\\ \\ {{ :mbotneo:lesson5:s48.png |Move motors block}} - Click the Variables selector and drag the "left_power" block to the coding pane and snap it into the left section of the "encoder motor EM1 rotates at 50% power, encoder motor EM2 rotates at 50% power" block\\ \\ {{ :mbotneo:lesson5:s49.png |left_power variable block}} - Drag the "right_power" block to the coding pane and snap it into the right section of the "encoder motor EM1 rotates at left_power% power, encoder motor EM2 rotates at 50% power" block\\ \\ {{ :mbotneo:lesson5:s50.png |right_power variable block}} - Click the Control selector and drag the "if < > then" block to the coding pane and snap it to the bottom of the Define detect_colors block 4 times\\ \\ {{ :mbotneo:lesson5:s51.png |If-then block}} - Click the Quad RGB Sensor selector and drag the "quad rgb sensor 1 probe (1) R2 detects line?" block to the coding pane and snap it into each of the "if < > then" blocks\\ \\ {{ :mbotneo:lesson5:s52.png |Quad RGB sensor block}} - Change the "(1) R2" to "random" and the "line" to "red" in the top "quad rgb sensor 1 probe (1) R2 detects line?" block\\ \\ {{ :mbotneo:lesson5:s53.png |Detect red line}} - Change the "(1) R2" to "random" and the "line" to "yellow" in the second "quad rgb sensor 1 probe (1) R2 detects line?" block\\ \\ {{ :mbotneo:lesson5:s54.png |Detect yellow line}} - Change the "(1) R2" to "random" and the "line" to "green" in the third "quad rgb sensor 1 probe (1) R2 detects line?" block\\ \\ {{ :mbotneo:lesson5:s55.png |Detect green line}} - Change the "(1) R2" to "random" and the "line" to "blue" in the bottom "quad rgb sensor 1 probe (1) R2 detects line?" block\\ \\ {{ :mbotneo:lesson5:s56.png |Detect blue line}} - Click the Audio selector and drag the "play hi" block to the coding pane and snap it into each of the if - then blocks\\ \\ {{ :mbotneo:lesson5:s57.png |Play sound block}} - Change the "hi" to "clockwork" in the top three "play hi" blocks\\ \\ The clockwork sound is near the bottom of the list {{ :mbotneo:lesson5:s58.png |Play clockwork}} - Change the "hi" to "iron" in the bottom "play hi" block\\ \\ The iron sound is near the bottom of the list {{ :mbotneo:lesson5:s59.png |Play iron}} - Click the LED selector and drag the "display" block to the coding pane and snap it into each of the if - then blocks\\ \\ {{ :mbotneo:lesson5:s60.png |LED block}} - Click on the LED colors in the top display block and change them all to "red"\\ \\ {{ :mbotneo:lesson5:s61.png |Display red}} - Click on the LED colors in the second display block and change them all to "yellow"\\ \\ {{ :mbotneo:lesson5:s62.png |Display yellow}} - Click on the LED colors in the third display block and change them all to "green"\\ \\ {{ :mbotneo:lesson5:s63.png |Display green}} - Click on the LED colors in the bottom display block and change them all to "blue"\\ \\ {{ :mbotneo:lesson5:s64.png |Display blue}} - Click the Variables selector and drag the "set base_power to 0" block to the coding pane and snap it into each of the if - then blocks\\ \\ {{ :mbotneo:lesson5:s65.png |Set variable block}} - Change the "0" to "30" in the top "set base_power to 0" block\\ \\ {{ :mbotneo:lesson5:s66.png |Set base_power}} - Change the "0" to "20" in the second "set base_power to 0" block\\ \\ {{ :mbotneo:lesson5:s67.png |Set base_power}} - Change the "0" to "10" in the third "set base_power to 0" block\\ \\ {{ :mbotneo:lesson5:s68.png |Set base_power}} - Click the Control selector and drag the "if < > then" block to the coding pane and snap it to the bottom of the Define start_again block\\ \\ {{ :mbotneo:lesson5:s69.png |If-then block}} - Click the Sensing selector and drag the "button A pressed?" block to the coding pane and snap it into the "if < > then" block\\ \\ This allows you to start the mBot Neo line following again after it stops {{ :mbotneo:lesson5:s70.png |Button A pressed block}} - Change the "A" to "B" in the "if button A pressed then" block\\ \\ {{ :mbotneo:lesson5:s71.png |Set B button}} - Click the Audio selector and drag the "play hi" block to the coding pane and snap it into the if - then block\\ \\ {{ :mbotneo:lesson5:s72.png |Play sound block}} - Change the "hi" to "iron" in the "play hi" block\\ \\ The iron sound is near the bottom of the list {{ :mbotneo:lesson5:s73.png |Play iron}} - Click the mBot2 Chassis selector and drag the "moves forward 100 cm until done" block to the coding pane and snap it into the if - then block\\ \\ {{ :mbotneo:lesson5:s74.png |Move forward block}} - Change the "100" to "5" in the "moves forward 100 cm until done" block\\ \\ {{ :mbotneo:lesson5:s75.png |Move 5 cm}} - Click the LED selector and drag the "display" block to the coding pane and snap it into the if - then block\\ \\ {{ :mbotneo:lesson5:s76.png |LED block}} - Click the Variables selector and drag the "set base_power to 0" block to the coding pane and snap it into the if - then block\\ \\ {{ :mbotneo:lesson5:s77.png |Set variable block}} - Change the "0" to "40" in the "set base_power to 0" block\\ \\ {{ :mbotneo:lesson5:s78.png |Set base_power}} - Click the Events selector and drag the "when button A pressed" block to an empty area of the coding pane\\ \\ {{ :mbotneo:lesson5:s79.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:lesson5:s80.png |Restart CyberPi block}} - Change the name of the program in the upper left to "ColorLineFollow"\\ \\ {{ :mbotneo:lesson5:s81.png |Change Program Name}} - Slide the power switch on the mBot2 Shield to the ON position and click the Connect button\\ \\ {{ :mbotneo:lesson5:s82.png |Connect to CyberPi}} - Click the Upload Code button and wait for the upload to complete\\ \\ {{ :mbotneo:lesson5:s83.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 the line following map on the floor - Place the mBot on the line 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 follow the line, press the A button - Change the sounds and LED colors to see how they change what the mBot Neo does What is your favorite sound? Try changing the LED display block with the LED animation block to see what it does What changes will make the mBot Neo accelerate on detecting colors after it has stopped? 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 76 through 78 above [[:mbotneo:start|Return to Main mBot Neo page]]