Making blocks with input and return value
A program block contains a series of steps. To make a block more intelligent, you can add input and output functionality to it. In this lecture you will learn how to create input variables, how to return a result and how to use the result of a program block in a condition.
Figure 1 - A program using input and return values
- Ozeki 10 installed: http://www.ozeki.hu/index.php?owpn=6066
- SNAP: http://www.ozeki.hu/index.php?owpn=6180
- Sniffer: http://www.ozeki.hu/index.php?owpn=6204
Discover how to give input parameter for a block
In this task you will discover how you can give input parameters to the blocks. If you create a new block you can set input parameters easily. When you create a new block you should click a '+' icon on the block name's right side. You will see a window 'Create input name'. You can type the input parameter's name here. Click on 'OK' then you will have an input parameter to your block (Figure 2). If you will call that block it will wait an input parameter from the program.
Figure 2 - A block with an input parameter
Discover how to use a return value
In this task you will discover how you can use return value. If you create a reporter block it has got a 'report [number]' block (Figure 3). If that block has executed it will return with a value (the 'report' block's parameter).You can give that value or you can set that value with a variable. If you create a variable to save the value you can use that value later, for example you can use it in a condition.
Figure 3 - A reporter block with 'report'
Discover how to use a return value in a condition
In this task you will learn how you can use a return value in a condition. If you would like to use the return value in a condition you need to create a variable for save it. If your program has a 'report' block give the variable's name to the 'report' block's parameter. Now, that variable contains the return value. If you need to use that value for example in a predicate block you can do that easily. Just drag the variable and drop it into the predicate block's parameter like in Figure 4.
Figure 4 - Using a return value in a condition
Make a reporter block that can limit the speed
In this task you will make a block for the program called 'SpeedLimiter'. That block can limit the speed if it is too high. The block need an input parameter, the speed. The block can check the speed's value and if it is too high the Brick play a 'beep' and limit the speed to 15. In the block you need to use return values and you need to create a variable (speed_par) for the return value. If the speed is correct the block drives the robot forward and returns with the robot's speed (Figure 5).
Figure 5 - Block for the program using return values
Write a program that use the return value from the block
In this task you will write a program that use the block you have created in the previous task. You will need a variable called 'speed'. The variable will store the robot's speed. You need to control your robot with the up arrow and down arrow on your keyboard. If you press the up arrow the robot's speed will increase and if you press the down arrow the robot's speed will decrease. With 'set [speed] to [SpeedLimiter [speed]]' block you can set the robot's speed and you can use your block. Write the same program in Figure 6 and limit your robot's speed.
Figure 6 - Program using return value
Download the program|
The program is capable to limit the speed and play a 'beep' if the speed is too high and control the robot with keyboard.
Download Ozeki SNAP code