ghtly and then continue forward. The program looks like this. We need to control 2 of the motors and we need to control motor D and we need to move it forward and we need to control motor A. These are the 2 motors that we need to control and we had one sensor, this sensor is located on port 1, it's a color sensor. We want to measure the reflected light, not the color but the reflected light because this is a proportional algorithm. How do we control these motors?
We use the block for detecting the reflected light then we must calculate what should the power of motor A be and what should the power of motor D be.
How do we do this? First we have motor D and in our robot, if you imagine it, in our robot motor D is the left motor. If we have our robot and this here is the front, we have the sensor here and we have one of the motors here and we have the other motor here. As the robot moves forward we have the following numbers. This is the sensor on port 1 and this is motor A and this is motor D and this here is the line, that we are following. We are following the edge of this line, it's all black and we are following the edge of this line. Motor A, motor D. So motor A is our right motor and motor D is our left motor and that's important. How does the proportional algorithm works? We detect with the color sensor and we do this in a loop
and we take the value of the color sensor. This is more advanced program and it will include some variables and some threads but stay with me. Detect the value and then we do a calculation with this value and the calculation is the following.
If we place our robot over the black line so now it's over the black line
and currently this is the value detected by our robot.
We are over black and the sensor detects 16%. If we move the robot over the table which is in our case something like white. We detect a value of 68%. So black is 16 and white is 68.
The value between 68 and 16 is around 40. We can use this as a threshold value. We detect the value of the sensor and we subtract 40, that's the middle. If we have 70 here and 40 here then the result of this operation will be 30 and we also do the same operation but in reversed order. We take the value of the sensor and we subtract this value from 40, so it's 40 minus this value. This is very crucial to understanding this algorithm. When we have the sensor detecting white we have one of the values large and the other value small. If the sensor detects 70 something white we have this block returning 30 and this block returning minus 30. If we are in the middle between the white and the black that means on the edge of the line then both of this blocks will return 0. This is how the proportional works. As we are far away from the line this block will return large number and this block will return a small number.
So we subtract from the detected value 40 and the detected value from 40. Then we do the following thing. We add these values to variables and I'll call this variable motorR (motor Right). That's the power of the right motor. We want to write a numeric value and this numeric value will be the result of this block. We need one more variable that's called motorL (motor Left)
and the result of this block we'll write this result in this variable. Then what you'll do is pass this variables to the motors and you have 2 motors, the first one is motor D and we are starting with on and we set the power of motor D to be equal to the power of the left motor because in our case motor D is the left motor. We read a numeric value and we pass it to the block. We also do the same thing with the other motor. We connect this.
We add the same block for the other motor. We read the value from motorR and we pass this value to the block and it's motor A. Again, we connect this.
Now we have multithreaded program.
It works in the following way. We have 2 separate threads, the first thread is controlling motor D and the second thread is controlling motor A. These 2 threads happen simultaneously and we have a third thread that's actually detecting the value of the sens