User Tools

Site Tools


mbotneo:lesson5

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
mbotneo:lesson5 [2024/02/16 13:08] jstruebelmbotneo:lesson5 [2024/03/29 09:52] (current) jstruebel
Line 1: Line 1:
 +===== 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\\ \\ <WRAP round tip>This will charge up your mBot Neo while you create the program</WRAP>
 +  - Open the mBlock app on your computer\\ \\ <WRAP round tip>You can download the mBlock app from [[https://www.mblock.cc/en/download/|mBlock Download]] if it isn't already installed</WRAP> <WRAP box>{{ :mbotneo:lesson5:s1.png |Open mBlock app}}</WRAP>
 +  - Click on the Extension button\\ \\ <WRAP box>{{ :mbotneo:lesson5:s2.png |Add new extension}}</WRAP>
 +  - Click the Add button under the mBot2 shield\\ \\ <WRAP box>{{ :mbotneo:lesson5:s3.png |Select mBot2 extension}}</WRAP>
 +  - Click on the Extension button\\ \\ <WRAP box>{{ :mbotneo:lesson5:s4.png |Add new extension}}</WRAP>
 +  - Click the Add button under the Quad RGB Sensor\\ \\ <WRAP box>{{ :mbotneo:lesson5:s5.png |Select quad RGB sensor extension}}</WRAP>
 +  - Click the Upload button under the Mode Switch section\\ \\ <WRAP round tip>This allows you to write your program without having the mBot Neo connected to your computer</WRAP> <WRAP box>{{ :mbotneo:lesson5:s6.png |Select Upload mode}}</WRAP>
 +  - Click the Events selector and drag the "when CyberPi starts up" block to the coding pane\\ \\ <WRAP box>{{ :mbotneo:lesson5:s7.png |Startup block}}</WRAP>
 +  - 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\\ \\ <WRAP box>{{ :mbotneo:lesson5:s8.png |Display block}}</WRAP>
 +  - 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\\ \\ <WRAP box>{{ :mbotneo:lesson5:s9.png |Set display message}}</WRAP>
 +  - Click the Control selector and drag the "wait until < >" block to the coding pane and snap it to the bottom of the stack\\ \\ <WRAP box>{{ :mbotneo:lesson5:s10.png |Wait until block}}</WRAP>
 +  - Click the Sensing selector and drag the "button A pressed?" block to the coding pane and snap it into the "wait until < >" block\\ \\ <WRAP round tip>This allows you to control when the mBot Neo starts running the color line following after you power it on</WRAP> <WRAP box>{{ :mbotneo:lesson5:s11.png |Button A pressed block}}</WRAP>
 +  - Change the "A" to "B" in the "when button A pressed" block\\ \\ <WRAP box>{{ :mbotneo:lesson5:s12.png |Set B button}}</WRAP>
 +  - Click the Variables selector and click the Make a Variable button to create the following variables for all sprites\\ \\ <WRAP round tip>This will allow your program to store information that it needs and change it as it runs</WRAP> <WRAP box>{{ :mbotneo:lesson5:s13.png |Create Variables}}</WRAP>
 +    - base_power\\ \\ <WRAP box>{{ :mbotneo:lesson5:s14.png |Create base_power variable}}</WRAP>
 +    - kp\\ \\ <WRAP box>{{ :mbotneo:lesson5:s15.png |Create kp variable}}</WRAP>
 +    - left_power\\ \\ <WRAP box>{{ :mbotneo:lesson5:s16.png |Create left_power variable}}</WRAP>
 +    - right_power\\ \\ <WRAP box>{{ :mbotneo:lesson5:s17.png |Create right_power variable}}</WRAP>
 +  - Drag the "set base_power to 0" block to the coding pane and snap it to the bottom of the stack\\ \\ <WRAP box>{{ :mbotneo:lesson5:s18.png |Set variable block}}</WRAP>
 +  - Change the "0" to "40" in the "set base_power to 0" block\\ \\ <WRAP box>{{ :mbotneo:lesson5:s19.png |Set base_power value}}</WRAP>
 +  - Drag the "set base_power to 0" block to the coding pane and snap it to the bottom of the stack\\ \\ <WRAP box>{{ :mbotneo:lesson5:s20.png |Set variable block}}</WRAP>
 +  - Change the "base_power" to "kp" and the "0" to "0.5" in the "set base_power to 0" block\\ \\ <WRAP box>{{ :mbotneo:lesson5:s21.png |Set kp value}}</WRAP>
 +  - Click the LED selector and drag the "display" block to the coding pane and snap it to the bottom of the stack\\ \\ <WRAP round tip>This will allow your mBot Neo to display colors on its LEDs</WRAP> <WRAP box>{{ :mbotneo:lesson5:s22.png |LED block}}</WRAP>
 +  - Click the Control selector and drag the "forever" block to the coding pane and snap it to the bottom of the stack\\ \\ <WRAP round tip>This will allow your program to keep running until you press the A button on the CyberPi</WRAP> <WRAP box>{{ :mbotneo:lesson5:s23.png |Forever block}}</WRAP>
 +  - Click the My Blocks selector and click the Make a Block button to create the following blocks\\ \\ <WRAP round tip>This will allow you to make your program neater</WRAP> <WRAP box>{{ :mbotneo:lesson5:s24.png |Create Blocks}}</WRAP>
 +    - follow_line\\ \\ <WRAP box>{{ :mbotneo:lesson5:s25.png |Create line_following block}}</WRAP>
 +    - detect_colors\\ \\ <WRAP box>{{ :mbotneo:lesson5:s26.png |Create enter_station block}}</WRAP>
 +    - start_again\\ \\ <WRAP box>{{ :mbotneo:lesson5:s27.png |Create start_again block}}</WRAP>
 +  - Drag the Define blocks in the coding pane to blank areas\\ \\ <WRAP box>{{ :mbotneo:lesson5:s28.png |Define blocks}}</WRAP>
 +  - Drag the "follow_line" block to the coding pane and snap it into the forever block\\ \\ <WRAP box>{{ :mbotneo:lesson5:s29.png |follow_line block}}</WRAP>
 +  - Drag the "detect_colors" block to the coding pane and snap it into the forever block\\ \\ <WRAP box>{{ :mbotneo:lesson5:s30.png |detect_colors block}}</WRAP>
 +  - Drag the "start_again" block to the coding pane and snap it into the forever block\\ \\ <WRAP box>{{ :mbotneo:lesson5:s31.png |start_again block}}</WRAP>
 +  - 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\\ \\ <WRAP box>{{ :mbotneo:lesson5:s32.png |Set variable block}}</WRAP>
 +  - Change the "base_power" to "left_power" in the "set base_power to 0" block\\ \\ <WRAP box>{{ :mbotneo:lesson5:s33.png |Set left_power value}}</WRAP>
 +  - Click the Operators selector and drag the "( ) - ( )" block to the coding pane and snap it into the "set left_power to 0" block\\ \\ <WRAP box>{{ :mbotneo:lesson5:s34.png |Subtract operator block}}</WRAP>
 +  - Drag the "( ) * ( )" block to the coding pane and snap it into the right side of the "set left_power to ( ) - ( )" block\\ \\ <WRAP box>{{ :mbotneo:lesson5:s35.png |Multiply operator block}}</WRAP>
 +  - 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\\ \\ <WRAP box>{{ :mbotneo:lesson5:s36.png |base_power variable block}}</WRAP>
 +  - Drag the "kp" block to the coding pane and snap it into the middle of the "set left_power to (base_power) - ( ( ) * ( ) )" block\\ \\ <WRAP box>{{ :mbotneo:lesson5:s37.png |kp variable block}}</WRAP>
 +  - 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\\ \\ <WRAP box>{{ :mbotneo:lesson5:s38.png |RGB sensor deviation block}}</WRAP>
 +  - 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\\ \\ <WRAP box>{{ :mbotneo:lesson5:s39.png |Set variable block}}</WRAP>
 +  - Change the "base_power" to "right_power" in the "set base_power to 0" block\\ \\ <WRAP box>{{ :mbotneo:lesson5:s40.png |Set left_power value}}</WRAP>
 +  - Click the Operators selector and drag the "( ) * ( )" block to the coding pane and snap it into the "set right_power to 0" block\\ \\ <WRAP box>{{ :mbotneo:lesson5:s41.png |Multiply operator block}}</WRAP>
 +  - Drag the "( ) + ( )" block to the coding pane and snap it into the right side of the "set right_power to ( ) * ( )" block\\ \\ <WRAP box>{{ :mbotneo:lesson5:s42.png |Add operator block}}</WRAP>
 +  - Drag the "( ) * ( )" block to the coding pane and snap it into the right side of the "set right_power to ( ) * ( ( ) + ( ) )" block\\ \\ <WRAP box>{{ :mbotneo:lesson5:s43.png |Multiply operator block}}</WRAP>
 +  - Change the first of the "set right_power to ( ) * ( ( ) + ( ( ) * ( ) ) )" block to "-1"\\ \\ <WRAP box>{{ :mbotneo:lesson5:s44.png |base_power variable block}}</WRAP>
 +  - 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\\ \\ <WRAP box>{{ :mbotneo:lesson5:s45.png |base_power variable block}}</WRAP>
 +  - Drag the "kp" block to the coding pane and snap it into the third of the "set right_power to (-1) * ( (base_power) + ( ( ) * ( ) ) )" block\\ \\ <WRAP box>{{ :mbotneo:lesson5:s46.png |kp variable block}}</WRAP>
 +  - 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\\ \\ <WRAP box>{{ :mbotneo:lesson5:s47.png |RGB sensor deviation block}}</WRAP>
 +  - 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\\ \\ <WRAP box>{{ :mbotneo:lesson5:s48.png |Move motors block}}</WRAP>
 +  - 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\\ \\ <WRAP box>{{ :mbotneo:lesson5:s49.png |left_power variable block}}</WRAP>
 +  - 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\\ \\ <WRAP box>{{ :mbotneo:lesson5:s50.png |right_power variable block}}</WRAP>
 +  - 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\\ \\ <WRAP box>{{ :mbotneo:lesson5:s51.png |If-then block}}</WRAP>
 +  - 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\\ \\ <WRAP box>{{ :mbotneo:lesson5:s52.png |Quad RGB sensor block}}</WRAP>
 +  - Change the "(1) R2" to "random" and the "line" to "red" in the top "quad rgb sensor 1 probe (1) R2 detects line?" block\\ \\ <WRAP box>{{ :mbotneo:lesson5:s53.png |Detect red line}}</WRAP>
 +  - Change the "(1) R2" to "random" and the "line" to "yellow" in the second "quad rgb sensor 1 probe (1) R2 detects line?" block\\ \\ <WRAP box>{{ :mbotneo:lesson5:s54.png |Detect yellow line}}</WRAP>
 +  - Change the "(1) R2" to "random" and the "line" to "green" in the third "quad rgb sensor 1 probe (1) R2 detects line?" block\\ \\ <WRAP box>{{ :mbotneo:lesson5:s55.png |Detect green line}}</WRAP>
 +  - Change the "(1) R2" to "random" and the "line" to "blue" in the bottom "quad rgb sensor 1 probe (1) R2 detects line?" block\\ \\ <WRAP box>{{ :mbotneo:lesson5:s56.png |Detect blue line}}</WRAP>
 +  - Click the Audio selector and drag the "play hi" block to the coding pane and snap it into each of the if - then blocks\\ \\ <WRAP box>{{ :mbotneo:lesson5:s57.png |Play sound block}}</WRAP>
 +  - Change the "hi" to "clockwork" in the top three "play hi" blocks\\ \\ <WRAP round tip>The clockwork sound is near the bottom of the list</WRAP> <WRAP box>{{ :mbotneo:lesson5:s58.png |Play clockwork}}</WRAP>
 +  - Change the "hi" to "iron" in the bottom "play hi" block\\ \\ <WRAP round tip>The iron sound is near the bottom of the list</WRAP> <WRAP box>{{ :mbotneo:lesson5:s59.png |Play iron}}</WRAP>
 +  - Click the LED selector and drag the "display" block to the coding pane and snap it into each of the if - then blocks\\ \\ <WRAP box>{{ :mbotneo:lesson5:s60.png |LED block}}</WRAP>
 +  - Click on the LED colors in the top display block and change them all to "red"\\ \\ <WRAP box>{{ :mbotneo:lesson5:s61.png |Display red}}</WRAP>
 +  - Click on the LED colors in the second display block and change them all to "yellow"\\ \\ <WRAP box>{{ :mbotneo:lesson5:s62.png |Display yellow}}</WRAP>
 +  - Click on the LED colors in the third display block and change them all to "green"\\ \\ <WRAP box>{{ :mbotneo:lesson5:s63.png |Display green}}</WRAP>
 +  - Click on the LED colors in the bottom display block and change them all to "blue"\\ \\ <WRAP box>{{ :mbotneo:lesson5:s64.png |Display blue}}</WRAP>
 +  - 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\\ \\ <WRAP box>{{ :mbotneo:lesson5:s65.png |Set variable block}}</WRAP>
 +  - Change the "0" to "30" in the top "set base_power to 0" block\\ \\ <WRAP box>{{ :mbotneo:lesson5:s66.png |Set base_power}}</WRAP>
 +  - Change the "0" to "20" in the second "set base_power to 0" block\\ \\ <WRAP box>{{ :mbotneo:lesson5:s67.png |Set base_power}}</WRAP>
 +  - Change the "0" to "10" in the third "set base_power to 0" block\\ \\ <WRAP box>{{ :mbotneo:lesson5:s68.png |Set base_power}}</WRAP>
 +  - 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\\ \\ <WRAP box>{{ :mbotneo:lesson5:s69.png |If-then block}}</WRAP>
 +  - Click the Sensing selector and drag the "button A pressed?" block to the coding pane and snap it into the "if < > then" block\\ \\ <WRAP round tip>This allows you to start the mBot Neo line following again after it stops</WRAP> <WRAP box>{{ :mbotneo:lesson5:s70.png |Button A pressed block}}</WRAP>
 +  - Change the "A" to "B" in the "if button A pressed then" block\\ \\ <WRAP box>{{ :mbotneo:lesson5:s71.png |Set B button}}</WRAP>
 +  - Click the Audio selector and drag the "play hi" block to the coding pane and snap it into the if - then block\\ \\ <WRAP box>{{ :mbotneo:lesson5:s72.png |Play sound block}}</WRAP>
 +  - Change the "hi" to "iron" in the "play hi" block\\ \\ <WRAP round tip>The iron sound is near the bottom of the list</WRAP> <WRAP box>{{ :mbotneo:lesson5:s73.png |Play iron}}</WRAP>
 +  - 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\\ \\ <WRAP box>{{ :mbotneo:lesson5:s74.png |Move forward block}}</WRAP>
 +  - Change the "100" to "5" in the "moves forward 100 cm until done" block\\ \\ <WRAP box>{{ :mbotneo:lesson5:s75.png |Move 5 cm}}</WRAP>
 +  - Click the LED selector and drag the "display" block to the coding pane and snap it into the if - then block\\ \\ <WRAP box>{{ :mbotneo:lesson5:s76.png |LED block}}</WRAP>
 +  - 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\\ \\ <WRAP box>{{ :mbotneo:lesson5:s77.png |Set variable block}}</WRAP>
 +  - Change the "0" to "40" in the "set base_power to 0" block\\ \\ <WRAP box>{{ :mbotneo:lesson5:s78.png |Set base_power}}</WRAP>
 +  - Click the Events selector and drag the "when button A pressed" block to an empty area of the coding pane\\ \\ <WRAP box>{{ :mbotneo:lesson5:s79.png |Button pressed event block}}</WRAP>
 +  - 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\\ \\ <WRAP round tip>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</WRAP> <WRAP box>{{ :mbotneo:lesson5:s80.png |Restart CyberPi block}}</WRAP>
 +  - Change the name of the program in the upper left to "ColorLineFollow"\\ \\ <WRAP box>{{ :mbotneo:lesson5:s81.png |Change Program Name}}</WRAP>
 +  - Slide the power switch on the mBot2 Shield to the ON position and click the Connect button\\ \\ <WRAP box>{{ :mbotneo:lesson5:s82.png |Connect to CyberPi}}</WRAP>
 +  - Click the Upload Code button and wait for the upload to complete\\ \\ <WRAP box>{{ :mbotneo:lesson5:s83.png |Upload program to mBot}}</WRAP>
 +  - 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
 +<WRAP round help>What is your favorite sound?</WRAP>
 +<WRAP round tip>Try changing the LED display block with the LED animation block to see what it does</WRAP>
 +<WRAP round help>What changes will make the mBot Neo accelerate on detecting colors after it has stopped?</WRAP>
 +<WRAP round tip>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</WRAP>
 +
 +[[:mbotneo:start|Return to Main mBot Neo page]]