Tip:
Highlight text to annotate it
X
Welcome to Blue Angels Robotics, my name is Emma. In this video, you are going to learn
how to create My Block with input and output. Let’s take a look why we need My Blocks.
First, My Block makes common code reusable. If your robot needs to repeat an action again
and again, it’s time to create My Blocks. Second, it makes code changes quickly in a
single place. Third, My Block helps organize your code. When your program grows larger,
it becomes difficult to track the logic. However, if you group the code logically into My Block,
the code will become easy to understand. Also, My Block makes debug easy, you can run each
of the My Block by itself. Last but not least, using My Block saves memory.
Now, let’s create a My Block from the sample program that we created in video 2.4: How
to convert centimeter to degree for moving distance. This sample program converts centimeter
value to degree value. The first constant block has the centimeter
value. The value transfers to the next Math block as the input value through data wire.
The math block converts the centimeter value to degree value and output it through data
wire to the next Moving Steering block. Now, let’s create the centimeter to degree
My Block. Highlight all the blocks you need for the My Block. For this sample program,
only the Math block is needed. Don’t highlight the constant block. Click “Tools” Menu,
and select “My Block Builder”. In the “My Block Builder” dialog, give this My
Block a name “cm2Degree”, Put a description, such as “This my block converts centimeter
to degree, wheel diameter 4.32 cm”. Then, select a My Block icon. I can pick up
an existing icon, or you can create your own icon. I have created the cm2Degree icon, and
I select this icon. Now, click the “Parameter Setup” tab to
setup the input and output parameters. You can edit existing parameter or add a new parameter.
Because we have input and output data wire connected to the highlighted My Block code,
the My Block builder automatically created 1 input and 1 output parameter. The first
parameter is “Number Input”, give it an appropriate name, such “input_cm”. You
can set a default value, such as 0, select a parameter style. You can also click the
“Parameter Icons” tab to select an icon for this input parameter, for example, select
the cm icon. Now, let’s configure the output parameter. Click the second parameter “Number
Output”. Give it a name, such as “output_degree”. The rest of the default values are correct
for this output parameter. Go to “Parameter Icons” tab to find an icon for the output
parameter, for example, this one. Click “Finish” button to create “cm2Degree”
My Block. You will find that the math block will be replaced by the cm2Degree My Block.
Click “My Block” palette, there will be a new My Block named “cm2Degree” that
shows up. If you want to see the code of My Block, just double click the My Block. You
can use this My Block in your code, as the way you use other EV3 blocks. For example,
in this new program, drag and drop a cm2Degree Myblock and a Moving Steering block to the
programming canvas. Set the Moving Steering block “On for degree”. Now, you can decide
how many centimeters to run. For example, I can input 10 in it, and then data wire the
output degree value to the Moving Steering block. When I run this program, my robot run
10 centimeters and then stop.