FREE SHIPPING for all orders over $25 shipped within the USA
Tutorial #3: Blinky
Let’s talk about time.
There are many ways to divide time. We have big chunks of time like a decade or year. There are also smaller chunks like a week or day.
We can divide days into smaller moments too. There are 24 hours in a day, and 60 minutes in each hour. Each minute can be divided into 60 seconds.
A second is a pretty small unit of time, but when it comes to programming a second isn’t small enough! Computers and robots are capable of acting very, very fast which means that sometimes we need smaller units of time.
When you program with Cubelets you will use a unit of time called a “millisecond.” A millisecond is 1/1000th of a second. Said another way, there are 1000 milliseconds in a single second.
Milliseconds are hard to imagine because they happen so fast. Every time you blink your eyes about 200 to 300 milliseconds pass!
In this tutorial we will look at how we can use time to change the way our Cubelets behave.
- Follow the on-screen instructions to complete your robot.
- Click the button to download the cubelets_tutorial_3.cubelet lesson file.
- Power on your robot. Click the connection icon and complete the Bluetooth pairing process.
- In this tutorial we are going to reprogram a flashlight so it blinks on for one second and off for one second. This means we will be using a new block called the “wait block.”
- The “wait block” tells the program to continue executing the previous command for a fixed amount of time.
- To begin, you’ll need to load the “cubelets_tutorial_3.cubelet” file. Click the load icon in the toolbar. Then follow the system dialogue to select the proper file.
- When the tutorial file loads you should see a canvas with a few preplaced blocks. You’ll need to arrange these blocks to create a working program. So let’s get started.
- Start by setting the Flashlight to full brightness. Drag the “set actuator value to” block into your “forever do” block.
- Drag the “number field” block to the right end of the “set actuator value to” block.
- The number field block allows us to directly set a value in our program. Unlike weighted average which relies on other blocks. The number field allows us to override and set block values.
- Set your Flashlight to full brightness by entering the maximum Cubelet Block Value in the number field. (255)
- Nice work! You’ve just made a line of code tells our Flashlight to turn on at full brightness.
- Next, we need to tell our Flashlight how long it should stay on. For that we will need a timing block.
- Drag one of the “wait milliseconds” blocks below the line of code you just completed.
- You’ll notice that this block has a default value of 1000 milliseconds. This means our robot will perform the code above this command for 1 second, then move on to the next bit of code.
- Our challenge is to create a blinking Flashlight that turns on and off. So far you’ve created a program that will turn your Flashlight on for 1 second.
- Now you need to add some code blocks so your Flashlight turns off for one second. Add the two blocks needed to turn the Flashlight off.
If you need help click here for a hint.
HINT: You’ll need to use the “set actuator value to” block and the “number field” block. Instead of a big number like, 255 you’ll need to set the Flashlight to a small number so it turns off.
Next, add a timing command that tells our robot to keep the light off for 1 second. If you need a hint, click here.
HINT: In the Timing menu, drag the “wait milliseconds” block below the line of code you just completed. Make sure the the number field is set to 1000 milliseconds.
- Nice job! Let’s read our program from top to bottom to see if it is complete. Remember we are trying to create a program for a Flashlight that blinks on for one second and off for one second.
- Because we’ve placed the blocks inside the “forever do” block the program will repeat itself as long as the robot is on.
- The first line of code tells the Flashlight to turn on at full brightness
- The second line of code tells the Flashlight to keep the brightness at full value for 1000 milliseconds.
- The third line of code tells the Flashlight to set its brightness to zero.
- The fourth line of code tells the Flashlight to keep its brightness at zero for 1000 milliseconds.
You can directly control a variable in a Cubelet by setting numeric values instead of weighted averages. When you are programming, you’ll need to include timing blocks if you want your robot to perform an action for a specific amount of time.