# Line follow with multiple states algorithm for Color Sensor from LEGO Education SPIKE Prime ProPreview

On the topic of following lines with a robot there is a way to make the robot follow the line a little more precisely. This method is called "Multi-states" program. Or at least we call it like this. In this tutorial you will see how we program 5 state for the robot line following program. This could be particularly important with FIRST LEGO League competitions where we must build consistent and reliable robots.

• #1935
• 29 Jun 2022
• 6:50

## Why 5 states

Theoretically and practically you can get to a rather smooth line following with configuring the values for the programs and understanding how the values for the reflection of the light sensor and the values for turns change the behavior of the robot. 5 states is enough for this algorithm.

## How to use this tutorial?

Download the program and experiment with it. Configure the different values and see what the behavior of the robot is. Try to get the robot to follow the line without missing it and to keep the robot on the edge of the line moving forward without the 'wiggle wiggle' part.

### English

In this tutorial we again follow a line, but this time with a multi-state program. And the difference is that this time we are using the reflection of the light sensor rather than the color black or white. We use the reflection, which is sometimes more powerful. Let's see how it works. We start from base and we couldn't catch the line initially. Second time, let's start from the base. And what we see here is how the robot moves slightly to the left and then to the right, the left and to the right. And this robot uses the reflection of the light sensor. We'll get into more details about how to use the reflection and why the program is called Multi-state. The benefit of this program is that you can theoretically and sometimes rather practically follow a line very fast if you manage to get the right values for the reflection. Let's see the program. Now we see the program on the left along with the recording of the robot on the right. And as most programs in our course, we set the movement motors A and B and the speed of the motors is 25%. You can experiment with different speed settings, but when following a line it is a good practice to follow the line rather slow and this allows us to detect the proper values with the light sensor. The next thing is this repeat until block. It's rather large and it has a lot of if statement statements in it. And we repeat this block for about 10 seconds. That's how you limit the duration of the program. And you can modify this, of course. 10 seconds timer. Repeat until the timer is 10 seconds. Done. Next thing in the repeat until we have a number of if statements. And let's take a close look at the first if statement. This one here. If reflection is less than 20%, this means that the sensor E is over black because the reflection is less than 20%. And I'll give you a minute. Think about this. It's the way the sensors work. If you are over black, the reflection is low value, something like below 20% probably. And if this is the case, we move to the right rather fast with a value of 100. Same time, if the reflection is between 20 and 40%, which means it is still black, but it's rather more like a white. Now there is no such thing as a "whitish" black. What this means is that we are probably closer to the edge because the sensor detects a little bit of white. So we are coming to the edge and if we are coming to the edge of the line, we don't stop, but we change the speed with which we rotate to the right and reset 95. Let's see how this works in the program.