User Tools

Site Tools


mbot:lesson2

Differences

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

Link to this comparison view

mbot:lesson2 [2023/10/14 16:12] – created jstruebelmbot:lesson2 [2023/10/14 17:00] (current) jstruebel
Line 1: Line 1:
 +===== Objective =====
  
 +This lesson will show you how to program the mBot to avoid obstacles. It will then show you how test your program on the mBot.
 +
 +===== 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-robot-kit|mBot kit]] that can be purchased on Amazon.
 +
 +  - Assembled mBot from [[:mbot:lesson1|Lesson 1]]
 +  - USB Cable
 +  - Styrofoam Blocks that are at least 5in tall and 2in thick
 +
 +===== Software Programming =====
 +
 +  - 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>{{ :mbot:lesson2:s1.png |Open mBlock app}}</WRAP>
 +  - Click on the add button on the Devices tab\\ \\ <WRAP round tip>You can also click the 'X' for the CyberPi device since it won't be used</WRAP> <WRAP box>{{ :mbot:lesson2:s2.png |Add new device}}</WRAP>
 +  - Select the mBot and click the OK button\\ \\ <WRAP box>{{ :mbot:lesson2:s3.png |Select mBot device}}</WRAP>
 +  - Click the Upload button under the Mode Switch section\\ \\ <WRAP round tip>This allows you to write your program without having the mBot connected to your computer</WRAP> <WRAP box>{{ :mbot:lesson2:s4.png |Select Upload mode}}</WRAP>
 +  - Click the Events selector and drag the "when mBot(mCore) starts up" block to the coding pane\\ \\ <WRAP box>{{ :mbot:lesson2:s5.png |Startup block}}</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>{{ :mbot:lesson2:s6.png |Wait until block}}</WRAP>
 +  - Click the Sensing selector and drag the "when on-board button 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 starts running the obstacle avoidance after you power it on</WRAP> <WRAP box>{{ :mbot:lesson2:s7.png |Button pressed 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 turn the mBot power off</WRAP> <WRAP box>{{ :mbot:lesson2:s8.png |Forever block}}</WRAP>
 +  - Drag the "if < > then else" block to the coding pane and snap it inside the forever block\\ \\ <WRAP box>{{ :mbot:lesson2:s9.png |If-Then-Else block}}</WRAP>
 +  - Click the Operators selector and drag the "( ) < 50" block to the coding pane and snap it into the if - then block\\ \\ <WRAP box>{{ :mbot:lesson2:s10.png |Less than block}}</WRAP>
 +  - Click the Sensing selector and drag the "ultrasonic sensor port 3 distance (cm)" block to the coding pane and snap it into the "( ) < 50" block\\ \\ <WRAP box>{{ :mbot:lesson2:s11.png |Ultrasonic sensor block}}</WRAP>
 +  - Change the "50" to "10" in the "( ) < 50" block\\ \\ <WRAP box>{{ :mbot:lesson2:s12.png |Set distance value}}</WRAP>
 +  - Click the Action selector and drag the "turn right at power 50% for 1 secs" block to the coding pane and snap it inside the top section of the if-then-else block\\ \\ <WRAP round tip>This will cause the mBot to turn right whenever it senses an obstacle within 10cm</WRAP> <WRAP box>{{ :mbot:lesson2:s13.png |Turn right block}}</WRAP>
 +  - Change the "1" to "0.5" in the "turn right at power 50% for 1 secs" block\\ \\ <WRAP box>{{ :mbot:lesson2:s14.png |Set time for turning}}</WRAP>
 +  - Drag the "move forward at power 50%" block to the coding pane and snap it inside the bottom section of the if-then-else block\\ \\ <WRAP round tip>This will cause the mBot to move forward whenever it does not sense an obstacle within 10cm</WRAP> <WRAP box>{{ :mbot:lesson2:s15.png |Move forward block}}</WRAP>
 +  - Connect the USB cable to the mCore mainboard on the mBot and plug the cable into your computer
 +  - Slide the power switch on the mCore mainboard on the mBot to the ON position and click the Connect button\\ \\ <WRAP box>{{ :mbot:lesson2:s16.png |Connect to mBot}}</WRAP>
 +  - Click the Upload button and wait for the upload to complete\\ \\ <WRAP box>{{ :mbot:lesson2:s17.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
 +
 +===== 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 mCore mainboard on the mBot to the ON position
 +  - Press the black button on the front left of the mCore mainboard on the mBot to start the mBot moving
 +  - When you're finished watching the mBot avoid obstacles, pick it up and slide the power switch on the mCore mainboard to the OFF position
 +<WRAP round help>What should change in your program to make the mBot turn left when it senses an obstacle? Make that change and see if it works</WRAP>
 +<WRAP round tip>When you're ready to try out changes to your program, repeat steps 16 through 19 above</WRAP>
 +<WRAP round help>How could you change the program so that you can tell the mBot to stop moving instead of picking it up when you're done? Make that change and see if it works</WRAP>
 +<WRAP round help>How can you make the mBot start the obstacle avoidance using the infrared remote control like it did in [[:mbot:lesson1|Lesson 1]]? Make that change and see if it works</WRAP>
 +
 +[[:mbot:start|Return to Main mBot page]]