FREE SHIPPING for all orders over $25 shipped within the USA
Tutorial #6: Back up bot
In this tutorial you’re going to explore Conditionals. Don’t let the fancy name worry you! If you talk through the concepts they are actually a pretty intuitive coding practice.
An “if statement” is a coding tool that allows you to create an action if something occurs. A simple example is: If I see you wave “hello”, then I will wave hello in response.
Conditionals are statements that outline requirements for whether or not something has occurred. Conditionals allow us to be more specific when we write an if statement. To continue with our earlier example… If I see you wave hello and you are looking at me, then I will wave hello in response.
Let’s start programming so you can see how if statements and conditionals help expand your robot’s capabilities!
- In this tutorial you are going to create a robot that will drive forward when it detects a nearby object but reverse if it gets too close.
- Before you begin, be sure to download the “cubelets_tutorial_6.cubelet” starter file.
- Then assemble the tutorial robot using the on-screen instructions.
- Power on your robot and be aware that your Drive Cubelet may still be running a custom program. Be sure that your robot won’t drive away when you power it on! There’s no need to return your Cubelets to the default program if you’re going to program them again, but you may find it easier to do so.
- Click the connection icon and complete the Bluetooth pairing process.
- Next you’ll need to load the “cubelets_tutorial_6.cubelet” file. Click the load icon in the toolbar. Then follow the system dialogue to select the proper file.
- In this tutorial you’ll be creating a program for your Drive Cubelet. This program has two sections. The first portion tells your Drive Cubelet to move forward according to the weighted average Block Value. The second portion tells your Drive Cubelet to switch motor directions to a set level for a fixed period of time if the Block Value exceeds 180.
- Let’s start with the first portion, finish the first line of code so it sets the actuator value to the weighted average.
- In the next line you’ll see the start of an if statement and a conditional. In the future, you can find these blocks in the “Logic” menu. Complete the conditional statement so that it reads “if the weighted average is greater than 180.”
If you’re having difficulties completing this section, click here for a hint.
HINT: Use the weighted average block in the first puzzle piece window, and the number field in the second puzzle piece window. Set the number equal to 180. Finally, use the dropdown to select the greater than symbol (>).
- Nice job! You’ve completed the first part of your if statement. Now, arrange the remaining blocks inside of the if statement so that the robot switches directions, sets its motor to full speed for a period of 500 milliseconds then switches directions again so it moves forward.
Alright, double check your work by clicking here to reveal the solution. If your program matches, you’re ready to program your Drive Cubelet. Select the Drive Cubelet from the block map and click “Program Selected Cubelet.”
- Once Cubelets Blockly has finished updating your Drive Cubelet, take a moment to experiment with the new behavior you created using an if statement and a conditional!
- In Tutorial #7 we are going to use the same robot as a starting point! If you’d like to focus on something else and wish to continue your tutorial progress later, select your Drive Cubelet from the block map and click “Restore Default Program.” Otherwise click here to proceed to Tutorial #7!