'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 sensor. These 2 threads work independently and simultaneously. The first thread calculates the precise value and the second and the third thread set the value to be used by the motors and that's our program. Let's download and run it.
This here is the program that we have just implemented working. The robot finds the black line, moves but does not follow it. That's a problem that we'll solve in the next video.