top of page

COSTUM BLOCKS

Federico R.

HI,

I'm Federico and today I'll teach you how to create your first custom block.

As always, be prepared with your robot and the device you use to program it.

For this lesson we will start from the programs we wrote in the Cycles and Conditions lesson.

Federico

Intro

When you build a program, you will often have to write the same lines of code multiple times.

Think, for example, of the code you wrote in the previous lesson to move the robot forward for a certain number of rotations. To complete a certain path, you might want to execute the same code by changing the number of rotations, the trajectory, and the speed of the robot.

In computer science, it is possible to create parts of code that can be executed multiple times by modifying only certain parameters. These parts of code are called functions and are always composed of a definition and a call code. On Spike, they are called Custom Blocks.

Creating the Custom Block

et's start by creating the define block of our function. First, go to the Custom Blocks section and click on "create a block", this will open a window dedicated to writing our block.

In the center of the page, a preview of the custom block that you will use to call your function is shown. It consists of text labels and inputs that can be generic or boolean.

Since we want to create a function for the monitor blocks we learned to write in the previous lesson, let's start by inserting a label that says: "Move the robot forward for".

At this point, we need to insert the "Rotations" parameter, which will indicate how many rotations our robot should move.

Add a second label that says "Rotations at the speed of" and the "Speed" parameter. You should get the result shown in the figure.

block program

The names of the parameters must also be repeated in the labels because the call block appears with blank spaces that do not specify which value they should contain.

By clicking save, a define block is generated that allows us to write the part of the code we want to repeat.

Additionally, the call block is automatically created within the My Blocks section.

block program
block program

Writing the Code

Now that we have created the definition of our custom block we can write the code we want to repeat.
Let's retrieve the code written for a rotational movement that we learned in the Cycles and Conditions lesson and hook it into the define block.

programma blocchi
block program
block program
block program

Since in our case we want to control the robot's movement speed, we need to add the Set Movement Speed block to the already written code before executing the loop. Instead, we remove the Stop Movement block because we are not sure if a stop is necessary after each movement.

For example, if we want to move at two different speeds, we can concatenate two movement blocks without stopping the movement between them.

At this point, we just need to insert our parameters into the slots of the blocks that control the rotations and speed.

Drag the rotations label from our define block into the blank space where you normally write the value of the rotations and the speed label where the movement speed is set.

block program
programma blocchi
block program
block program

Editing a Custom Block

Often, we may realize during the program writing process that our custom block needs an additional parameter.

In our case, for example, it might be useful to add the trajectory parameter, which allows us to add flexibility to our function. The user might want to use the block for turning as well as moving straight.

Right-click on the define block of our custom block and select Edit.

The window we saw earlier will open again, where we can add the trajectory parameter. Remember to also write a label for the new parameter.

Finally, insert the trajectory parameter into the Start Movement block.

block program
block program
block program
block program

Using the Custom Block

We conclude the lesson by calling the custom block to move the robot first straight, then curving to the right and then straight again.

Let's insert 3 custom blocks following an event as we learned to do in previous lessons.
By inserting different parameters into the spaces we can modify the length, speed and trajectory of our movements.

Remember that in the custom block there is no block to stop the movement and it is therefore necessary to insert the pink block when we have finished our chain of movements.

block program
block program
block program
block program
block program
bottom of page