Sign in:

Tutorial #9: Countdown to power down

Instructions:

  1. In this tutorial we are going to expand on the robot we created in Tutorial #8. We will be creating a program for the Distance Cubelet that determines how long your robot will “live,” or remain active.
  2. For this tutorial you will need to duplicate the robot your built in Tutorial #8. If you need to rebuild the robot click here for instructions.
    1. Follow the on-screen instructions to complete your robot.
    2. Download the cubelets_tutorial_8a.cubelet and cubelets_tutorial_8b.cubelet starter files.

    3. Power on your robot and be aware that your Drive Cubelets may still be running custom programs. Be sure that your robot won’t drive away when you power it on!
    4. Click the connection icon and complete the Bluetooth pairing process.
    5. Load the “cubelets_tutorial_8a_complete.cubelet” file. Click the load icon in the toolbar. Then follow the system dialogue to select the proper file.

    6. Select the Drive Cubelet that is directly connected to the Distance and Bluetooth Cubelets, then click “Program Selected Cubelet.”
    7. Once your Cubelet has been successfully updated, load the “cubelets_tutorial_8b_complete.cubelet” file. Click the load icon in the toolbar. Then follow the system dialogue to select the proper file.

    8. Select the Drive Cubelet that is directly connected to the Battery Cubelet, then click “Program Selected Cubelet.”
    9. Once your Cubelet has been successfully updated, you’re ready to begin Tutorial #9.
  3. Click the button to download the cubelets_tutorial_9.cubelet lesson file.

  4. Load the “cubelets_tutorial_9.cubelet” file. Click the load icon in the toolbar. Then follow the system dialogue to select the proper file. This tutorial file has groups of code blocks arranged from top to bottom to make it easier to assemble each component.

  5. Cubelets can use variables to keep track of numbers besides the Block Value. Creating variables allows you to create more complex behaviors.
  6. In this tutorial you’re going to create a variable called “life.” The life variable will represent how long our robot remains active before it stops moving.
  7. The life variable is a great example of why we might use a the “setup” block. “Setup” is a portion of our program that only runs once. Unlike “forever” which will keep repeating the action as long as your robot is on.
  8. Arrange the first group of blocks inside the setup block so you are setting a variable to a random integer from 100-300.
  9. Use the dropdown to rename your variable. Name your variable “life.”
  10. Arrange the next group of blocks inside setup so that every 750 milliseconds life is changed by -10. You are creating a countdown timer for your robot’s active life.
  11. Nice work! You’ve just created a variable called life and a countdown timer that reduces the amount of robot life. Now you’ll need to create a statement that tells the robot what to do as the life variable changes.
  12. Move to the next group of blocks. Arrange the blocks to create an if/else statement in the forever block that says “if life is greater than or equal to 0 then set the block value to sensor value, else set the block value to 255.”
  13. Well done! Let’s look at what’s the program you’ve created is going to do. This program is creating a variable we called life. Life is assigned a random value between 100 and 300 and that value is decreasing by 10 every 750 milliseconds. When as long as life is greater than 0 then the block value is equal to the sensor value. If life isn’t greater than or equal to zero the block value gets set to 255.
  14. Select the Distance Cubelet from the Block Map and click “Program Selected Cubelet.”
  15. When you’re ready to see what your robot is doing take it to a large flat area like the floor or a big table. Turn your power off then on to start the life counter and see how your robot behaves. Does it avoid objects? How long did your robot “live” before it went inactive? It is pretty amazing that you can create a robot that “lives and dies” while avoiding things in front of it with only 3 simple rules!
  16. When you’ve finished playing with your robot, connect to Cubelets Blockly and restore the default programs for your Distance and Drive Cubelets.