Counting lines and stopping on the third is the subject of this video. It is important to know how to do this in order to conduct more than one experiment in STEM classes (if we consider that each line is an experiment)
- 23 Oct 2015
First detect a line. Stop at it. Then try to move and detect a second line. This seems quite straighforward, but is quite un-intuitive, yet again logical. Many times this should be implement in FIRST LEGO League (FLL) or World Robotics Olympiad (WRO) competitions or just in different STEM classes.
The next program will be how do we build a program that stops the robot on the third black line. I'll add 2 more lines here and we want to stop on the third line.
Let's add the 3 lines. The second line will be right here
and the third line will be right here.
Now, what you might think is that it's quite straight forward just move stop at this line, then move stop at this, then move stop at this. As you might remember from the touch sensor some of the programs although very not intuitive are kind of logical so the solution for this will be not intuitive but logical, again. Let's start with the first try to build such a program. In the program we move forward, we wait for a black line, then we stop. This here moves the robot to the first line, then we can again move forward,
follow the same principle, move forward,
then wait for the color sensor to detect black
and then stop again.
Take the Tank block and make it stop.
This here is the program. I'll start it.
As you can see the robot stops at the first line and based on our program we would expect the robot to move forward detects the line, stop, then move forward again, detect the line, stop again. It only stops on the first line. Let's start the program again.
What happens here is the same that happened with our touch sensor a few videos ago and it is the way the robotic sensors work, basically on every robotic system.
The problem is that when the robot moves, it detects a black line, then it stops again and then it starts moving again until it detects a black line, the sensor is already above a black line, so it's already detecting a black line and what happens is that it moves, stops, tries to move again, but it is already over a black line, so it detects the black line instantly after a millisecond or so, and it stops. So it's actually detecting this line twice, that's why the program is not behaving as we would expect. How do we solve this? The first solution is to add a small Wait block. Let's try it. If we now add a small Wait block, just after we detect the first line. We can edit it several times. Let's do it even simpler. So, we move forward, detect a line, stop, then let's move forward with motor A and D, wait for about a second or half a second
and only after this half a second has passed we would start searching for the next line, the third line. This will give the robot a chance to move away from the black line,
before trying to detect the second black line, we are stopping, moving again, waiting for half a second, so that we are no longer over a black line and then trying to detect the second black line. The program is now running.
Let's run the program again.
Why is this happening, because we are moving, we are reaching the black line and after we reached the black line, we stop, then we start moving again for about half a second and in this half a second we actually move to here, so it's after the second line and then we start looking for a black line and then we detect the third. The question is how much should we wait after we are on the first line and after we move forward. It should not be half a second, let's try with a 10th of a second, something smaller. I have changed the value in the wait block in the program for 10th of a second. I'll now run the program and as you can see the robot stops on the second line. Again.
This is for this video in the next video we'll try to remove this wait block, because although it is working it is not a good practice to have such wait blocks in the program and it's kind of a hack so it's not a real solution.
Courses and lessons with this Tutorial
This Tutorial is used in the following courses and lessons