LOOPS AND CONDITIONS

Giulia C.
Hey,
I'm Giulia and in this lesson I'll teach you how to use control blocks to create repeat loops.
In the first part, we'll program a robot to move until it detects the presence of a black line using the color sensor.
Next, we'll program some movements through repeat loops.
The result we will achieve is the same as that obtained with the simple Move straight block, but this work is necessary to be able to intervene in the control of movements through sensors.
We will deal with controlled movements in the following lessons when you have become familiar with the control blocks.
Intro
First, we will learn to use color/reflected light sensors to stop the robot over a black line.
You will simply need something black to place on the surface where the robot will move.
This time there will be no path, but the robot will have to move in a straight line until it reaches a pre-set condition, namely the change in color perception.

Stop on the line
As in the last lesson, we start by inserting blocks for the starting conditions, setting the motors and the movement speed.
We also set the Stack stop with the left button.
The goal is to use a Repeat until block to start the movement until the reflected light is less than a certain value (10% more than that perceived on black), at which point the cycle stops and the movement is halted.
Let's proceed by inserting the Repeat until block.
In the appropriate space, insert a Sensor block that uses the reflected light sensor, like the one in the image. Set the port connected to the sensor (in our case A) and change the reflected light value. To know the right value, place the sensor over a black surface and observe the value detected by the sensor. To that value (in our case 20%), it is always better to add 10% to ensure that, even when the robot is moving quickly, it perceives the black.

The value you have entered is one that you might need to use multiple times in different parts of your program. Additionally, it is a value that you might need to modify during programming because you realize it is not the optimal value.
To avoid manually changing this value in all parts of the program, I recommend creating a variable to which you assign this value. By modifying the variable's value, the parameter's value will automatically be updated in all the blocks where you have used it.
First, create a variable and name it "Stop Reflection."
At the start of the program, set the value of this variable to the desired value and use the variable as the parameter for your stop conditions.


We finish by inserting a Start Motion block inside the loop and a Stop Motion block immediately after.
When you run the program the robot will continue to start the movement until it reaches a black surface, at which point the clicking stops and the CPU will move on to the next operation which in our case is a stop movement.



Timed stop
The timed exit condition is widely used in First Lego League, we use it when we want to lean against a wall or a mission model. When the robot leans against a structure, in fact, the wheels could remain blocked, making the rotation condition ineffective.
Let's start again from our Repeat Until block.
This time insert the logical greater than operator inside the exit condition of the block.
Enter the value measured by the timer found in the sensors in the first slot of the logical operator while in the second set the desired time.

When we use the internal timer of our Hub, we must remember that it started measuring time from when we started the program. To ensure the correct functioning of this exit condition, we need to make sure the timer is reset before starting our loop.
To do this, place a Reset timer block before the repeat loop.
We conclude the program by inserting the two blocks Start movement and Stop movement as before.
With this code, we have created a program that makes the robot move for a number of seconds equal to the value we enter in the blank space.




Rotation stop
The stop condition based on wheel rotations is certainly more reliable than the timed one because the measurement of the distance traveled is a direct measure.
The distance traveled in a given time period depends on the robot's speed. In particular, the speed can vary slightly depending on the charge level, which leads to inaccuracies.
To implement this condition, we start again from the Repeat until block where we inserted the greater than logical condition and insert Relative position, remembering to select one of the two movement motors.
A common mistake in implementing this condition is choosing the "position" block instead of the "relative position" block.
The first block measures the absolute position of the motor and resets the count at the end of each rotation, while the second counts the angle in degrees relative to the initial position, including values greater than 360°.

The count of degrees relative to the initial position can be either positive or negative, depending on the direction of rotation of our wheels.
Using only the greater than condition cannot work when the motor moves towards negative angles. In this case, the condition would never be met, and the loop would repeat indefinitely.
The simplest way to solve this problem is to insert an absolute value to the relative position measurement. This way, the value we compare will be positive regardless of whether the wheel turns clockwise or counterclockwise.
The absolute value block can be found as the last block in the operators section.
Take this block and insert the relative position measurement inside it.

During some maneuvers the robot may move using only one wheel, for this reason we cannot rely only on the size of one of the two.
We then calculate the average between the relative positions of the two wheels.

The value of the motor position measurement is in degrees, so in the second slot of the Greater than block, we should insert a value in degrees.
To work with smaller and more tangible numbers, we can perform a conversion that allows us to use rotations.
To use this as a parameter, simply multiply the number of rotations you want to perform by the full angle (360°).
Finally, we need to reset the relative position before starting the loop, just as we did for the timer.
Insert the block "set relative position of motors A+B to 0." We conclude by adding the start movement and stop movement commands.

The code we have written allows us to move the robot back and forth by the number of rotations we enter in the blank space.




Controlled movements
The programs we have created in this lesson are not useful if used directly in this way. To move the robot by a certain number of rotations or for a time interval, we can rely on the Move for blocks.
These programs we have written repeatedly start the movement in the same way. In the next lessons, we will update them so that the speed and trajectory values can change with each iteration of the loop to meet our programming needs.