During this lesson, the students gain a good understanding of the Variable Block. The students will use this information to create a cruise control feature for their wheeled robots. The students will utilize two Touch Sensors to control and maintain the speed of their robot. The students will need to program their robot so that, once moving, it can be sped up by pressing the Touch Sensor. Code blocks for controlling the Touch Sensor (wait) should be placed inside a loop.
The Variable Block is a programming block that can store data (text, logic, a numeric, or arrays), which can be overwritten at any time while the program is running. This block has to be read and then written to, using the Math, Text, or Array Operations Blocks.
Program 1: Increase Speed with Variable
- Start the program.
- Create a Variable Block called “Speed” and enter a value of 0.
- If the Touch Sensor is pressed:
- Read the variable called “Speed”
- Add 10 to the read value
- Write the result in the variable called “Speed”
- Read the variable called “Speed”
- Start motors B and C at a speed set to the value stored in the variable called “Speed”
- ELSE
- (Do nothing)
- Repeat steps 3a to 3e forever.
Program 2: Increase and Decrease Speed with Variable
With the first program written and the robot accelerating when the Touch Sensor is pressed, the students would extend the program to slow the robot down.
One solution could be to have a second unlimited loop similar to the loop used in the first programming exercise. This loop would use a different Touch Sensor port (another sensor added). The Math Block would be changed to subtract rather than add.
- Start the program.
- Create a Variable Block called “Speed”, enter a value of 0, and start two tasks.
TASK 1
- If Touch Sensor 1 is pressed:
- Read the variable called “Speed”
- Add 10 to the read value
- Write the result in the variable called “Speed”
- Read the variable called “Speed”
- Start motors B and C at a speed set to the value stored in the variable called “Speed”
ELSE
(Do nothing)
- Repeat steps 3a to 3e forever.
TASK 2
- If Touch Sensor 2 is pressed:
- Read the variable called “Speed”
- Subtract 10 from the read value
- Write the result in the variable called “Speed”
- Read the variable called “Speed”
- Start motors B and C at a speed set to the value stored in the variable called “Speed”
ELSE
(Do nothing)
- Repeat steps 5a to 5e forever.**
Program 3: Increase and Decrease Speed with Variable and Display
With the wheeled robot now accelerating and decelerating at the touch of a button (or two), students can extend their programming to show how fast their robots are moving. Students will use the Display Block to show this value in the Variable Block.
A new skill to be learned in this lesson is the creation of a My Block. Two of these can be seen in the solution below. My Blocks allow users to create subroutines of programs they have already written. In the case below, we have taken the acceleration and deceleration loops and created My Blocks from these programs. There are two reasons to do this: first to save space, and second to allow these subroutines to be reused in other programs.
- Start the program.
- Create a Variable Block called “Speed”, enter a value of 0, and start three tasks.
TASK 1
- Start My Block “Acceleration”.
TASK 2
- Start My Block “Deceleration”.
TASK 3
- Read the variable called “Speed”.
- Display the value stored in the variable called “Speed”.
- Repeat steps 5 and 6 forever.