===== Objective =====
This lesson will show you how to program the mBot Neo to follow 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:lesson3:s1.png |Open mBlock app}}
- Click on the Extension button\\ \\ {{ :mbotneo:lesson3:s2.png |Add new extension}}
- Click the Add button under the mBot2 shield\\ \\ {{ :mbotneo:lesson3:s3.png |Select mBot2 extension}}
- Click on the Extension button\\ \\ {{ :mbotneo:lesson3:s4.png |Add new extension}}
- Click the Add button under the Quad RGB Sensor\\ \\ {{ :mbotneo:lesson3: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:lesson3:s6.png |Select Upload mode}}
- Click the Events selector and drag the "when CyberPi starts up" block to the coding pane\\ \\ {{ :mbotneo:lesson3: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:lesson3:s8.png |Display block}}
- Change the "makeblock" to "Press B to start line following and A to stop" in the "print makeblock and move to a newline" block\\ \\ {{ :mbotneo:lesson3: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:lesson3: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 line following after you power it on {{ :mbotneo:lesson3:s11.png |Button A pressed block}}
- Change the "A" to "B" in the "when button A pressed" block\\ \\ {{ :mbotneo:lesson3: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:lesson3:s13.png |Create Variables}}
- base_power\\ \\ {{ :mbotneo:lesson3:s14.png |Create base_power variable}}
- kp\\ \\ {{ :mbotneo:lesson3:s15.png |Create kp variable}}
- left_power\\ \\ {{ :mbotneo:lesson3:s16.png |Create left_power variable}}
- right_power\\ \\ {{ :mbotneo:lesson3: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:lesson3:s18.png |Set variable block}}
- Change the "0" to "30" in the "set base_power to 0" block\\ \\ {{ :mbotneo:lesson3: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:lesson3: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:lesson3:s21.png |Set kp 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:lesson3:s22.png |Forever block}}
- Click the Variables selector and drag the "set base_power to 0" block to the coding pane and snap it into the forever block\\ \\ {{ :mbotneo:lesson3:s23.png |Set variable block}}
- Change the "base_power" to "left_power" in the "set base_power to 0" block\\ \\ {{ :mbotneo:lesson3:s24.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:lesson3:s25.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:lesson3:s26.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:lesson3:s27.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:lesson3:s28.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:lesson3:s29.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 into the forever block\\ \\ {{ :mbotneo:lesson3:s30.png |Set variable block}}
- Change the "base_power" to "right_power" in the "set base_power to 0" block\\ \\ {{ :mbotneo:lesson3:s31.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:lesson3:s32.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:lesson3:s33.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:lesson3:s34.png |Multiply operator block}}
- Change the first of the "set right_power to ( ) * ( ( ) + ( ( ) * ( ) ) )" block to "-1"\\ \\ {{ :mbotneo:lesson3:s35.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:lesson3:s36.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:lesson3: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 right_power to (-1) * ( (base_power) + ( (kp) * ( ) ) )" block\\ \\ {{ :mbotneo:lesson3:s38.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 into the forever block\\ \\ {{ :mbotneo:lesson3:s39.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:lesson3:s40.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:lesson3:s41.png |right_power variable block}}
- Click the Events selector and drag the "when button A pressed" block to an empty area of the coding pane\\ \\ {{ :mbotneo:lesson3:s42.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:lesson3:s43.png |Restart CyberPi block}}
- Change the name of the program in the upper left to "LineFollow"\\ \\ {{ :mbotneo:lesson3:s44.png |Change Program Name}}
- Slide the power switch on the mBot2 Shield to the ON position and click the Connect button\\ \\ {{ :mbotneo:lesson3:s45.png |Connect to CyberPi}}
- Click the Upload button and wait for the upload to complete\\ \\ {{ :mbotneo:lesson3:s46.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 initial values of the base_power and kp variables to see how they change the mBot Neo line following
What values make the mBot Neo follow the line fast without losing control
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 42 through 44 above
[[:mbotneo:start|Return to Main mBot Neo page]]