INTRODUCTION TO CODING

Sofia P.
Hi!
I'm Sofia, and I'm here to teach you the basics of block programming with Spike 3.0.
In this lesson, I will explain the blocks we will use throughout the course and teach you the functionalities of the new app, as well as explain the basic logic to understand how the robots participating in the First Lego League work.
I recommend reading this lesson with your robot and a device for programming, either a tablet or a computer.
During the lesson, don't be afraid to experiment with what you are learning; in programming, you learn a lot from your mistakes.

introduction
o start, you need to download the Spike 3.0 app from your device's store; on the side, you will find the direct link to the store page.
Now that you have installed the application, we can begin. Open it and select Prime.
You should now see the main screen of the app. Select 'new project,' then 'word blocks,' and create. Once the operation is complete, the project screen appears. At the top, we find the bar with open projects and the Home button, while at the bottom, we have the commands for the view, for editing, and for starting the program. The left bar, on the other hand, contains all the programming blocks you can use and that we will see shortly.
When you start writing a program, you should always keep the project organized; this will help you navigate better during the debugging phase. To start, let's rename the project. Click on the dots next to the project name, then rename the project 'Introduction.' This way, if you need to review this program, you can easily find it from the list of projects.
To connect the robot, click on the button at the top and follow the application's instructions. If your robot is new, you may need to perform an update. Once the robot is connected, the buttons at the bottom right light up, and you can load the program with a simple touch.
Block categories
Motor blocks in blue allow us to control the individual motors of the robot.
Movement blocks in pink simultaneously command the two motors dedicated to movement, allowing us to manage movements more easily.
Light blocks in purple are dedicated to managing the display, while those dedicated to Sounds are colored light purple.
Event blocks in yellow are the ones that allow us to determine when the parts of the code we write are executed.
Control blocks are the blocks that allow us to create loops or make our robot take decisions.
Sensor blocks in light blue are the ones we will use to read the values measured by our sensors.
Operator blocks in green are all the blocks that allow us to perform logical or mathematical operations.
Variable blocks in orange contain all the variables we define and the blocks that allow us to modify them.
The My Blocks section will contain all the blocks that we will define and customize ourselves.









Motor's Blocks
Among the Motor blocks, we find two different types. Most of these have the classic rectangular shape. All rectangular blocks are operation blocks and are designed to be attached to another block that precedes them. These types of blocks are used to give direct commands to our robot.
The second type of block we find has rounded edges; these always return a numerical value and must be inserted into other blocks that have a space with the same shape.
The Execute for block allows you to move a motor by a predetermined amount, after which it will stop. The Start motor block, on the other hand, simply starts the motor movement, and a Stop motor block will be needed to stop it.
This pair of blocks is very useful when we want to perform other operations while the motor is in motion.
The Speed and Power blocks allow you to read the values of these quantities for the selected motor.




Movement Blocks
The robots we use in the First Lego League have two motors dedicated to movement, allowing the wheels to move independently. Unlike cars, robots do not have steering wheels and must move the two wheels at different speeds to turn.
This is the same principle used by snowcats. This system allows for much more effective maneuvering compared to the car system, making the robots significantly more maneuverable in tight spaces.
The Movement blocks automatically control the two motors connected to the wheels, allowing us to perform maneuvers easily. Without these blocks, we would have to control the two motors individually and calculate the robot's trajectory based on the combination of individual movements.
Among the movement blocks, we find Set movement motors, which allows us to indicate to our program which ports our motors are connected to.
Here too, we distinguish between commands that allow a movement for a precise distance and those that simply start the movement, similar to the motor blocks.
Among the movement blocks, we find the one that allows us to select the trajectory of the drive unit. Positive steering values execute right turns, while negative values move us to the left. With steering values greater than 99 or less than -99, the robot turns on itself.
The Set motor rotation block is used to set the distance traveled by the robot when it performs a rotation. This distance depends on the radius of the wheels we have mounted.



Event Blocks
Event blocks allow us to determine when the parts of the program we write are executed.
Among these, we will use the When program starts block to execute a part of the code as soon as we launch the program.
The When button is pressed/released block is very useful for executing parts of the code that need to be performed on command or multiple times. Usually, this is the block to which we will attach the program that moves the robot on the competition field.


Control Blocks
These orange blocks have mainly two functions: the first is to repeat a part of the program multiple times.
The Repeat command allows us to repeat a part of the program several times; we will use this block when we execute our first square.
When we need to repeat a part of the code an indefinite number of times, we can use the Repeat until block, which allows us to set an exit condition from the repeat. This block has a hexagonal indentation where we need to insert a Boolean value, which can only be true or false.
The If then blocks allow us to decide whether a part of the program should be executed or not. Again, our choice will be determined by a Boolean input.
The Stop stacks commands allow us to interrupt the execution of some parts or the entire program. We will use it, for example, when we need to stop the robot in case of an emergency.




Sensor Block
Sensor blocks are blocks that allow us to read our sensors and always return numerical or Boolean values.
The sensors we will use are the reflected light sensor and the gyroscope.
The Reflected Light sensor only works when positioned close to a surface, and in the robots competing in the First Lego League, we find them facing downwards to detect colors and reflections on the competition field.
In Color mode, the sensor is not very reliable, so it is used to detect Reflected Light. When the sensor is positioned on a light color, the reflected light is very high; conversely, the value detected is low when it is on a dark color.
The Reflected Light command returns a numerical value between 0 and 100 that directly corresponds to the reading of the sensor connected to the indicated port.
Among the blocks dedicated to this sensor, we also find Less/Greater than reflection, which returns a Boolean value based on the indicated condition.
The Gyroscope is a sensor that measures the rotations of the robot from a reference configuration. When we start the robot, an initial position is recorded, and every time the orientation changes, this sensor measures how many degrees we have deviated. Thanks to this sensor, we can create programs that allow perfect curves and accurate movements even in case of interaction with obstacles or mission models.
We can imagine the gyroscope as the robot's internal compass that helps it maintain orientation.
Among the blocks dedicated to this sensor, we find Yaw/Pitch/Roll angle, which returns the numerical value in degrees measured by the gyroscope (-179/180), and the Set yaw angle to zero command, which allows us to reset the zero position of our gyroscope.




Operators and Variables
Operators allow us to perform Mathematical and Logical operations. The former return numerical values and have a rounded shape, while the latter have a hexagonal shape and return a Boolean value.
Variables are memory spaces to which we assign a name. There are numerical values that are characteristic of the construction of our robot. For example, the value of reflected light that the robot detects when positioned on black depends on the distance of the sensor from the ground. In the program, we might have to rewrite this value multiple times within our control blocks. Writing the same parameter many times does not force us to use a variable but can cause difficulties when this value needs to be modified. For example, if we move our sensor lower, we will have to change this value in all parts of the program and search throughout the code where we wrote it.
Using a variable makes this process much simpler; we just need to change the value of the variable, and all blocks that read it will automatically see its correct value.
Assuming we have created a variable, the blocks "set variable" and "change variable" appear, allowing us to modify its value and the block that allows its reading.



Custom Blocks
As the final part, we should talk about Custom Blocks. This is a section where we find the blocks we have created, which perform the operations we have established.
We will cover this type of Blocks in a dedicated lesson that you can find in the following pages of our guide.
If you want to further explore the functionality of individual blocks, you can open the App guide on the Home at the top left, then click on Guide at the bottom and select Word Blocks. This way, you access a screen that contains the technical description of all available blocks.
I hope this introductory guide has been helpful to you.
Now you are ready to take your first steps in the upcoming lessons.