It's time to start moving on to larger and more challenging projects. We're going to use the Light Sensor we used earlier to control the motors and get your robot to follow a line on the ground. From now on, I'm only going to give you the new parts of the code. You will have to use previous knowledge to fill in the missing pieces.
The basic idea behind the Line Follower is that you will place the robot on the line so the light sensor is next to the edge of the line. When you turn it on, it will drive one motor forward until it sees the line. Then it will turn that motor off and the other one on until it no longer sees the line. This process will repeat with the robot following the edge of the line. It's important to remember that no motor should ever go in reverse on this program.
Start by running your Light Sensor code with the sensor pointed at the ground next to the line. Write down the value you get. Let's say it's 200. Then place the sensor above the black line and record that value. Let's assume it's 300. Now we need to determine the threshold value (we'll use this later) by finding a middle number between these two. For our example, we'll use a threshold value of 250, but you'll need to test your robot because you will have a different threshold value.
For this program, the new blocks we need are if/else from Control and "<" from Test. The if/else tells the Arduino to test whether the light sensor gets a value higher or lower than the threshold value we got before. If the light value is lower then, we know we're on the line. Turn the right motor on and set the left motor to 90 (or whatever your stopped value is). Else, we know we're off the line and need to switch motor is on. Here we'll drive the left motor forward and stop the right motor. Below is an example of the code, but you'll need to replace the bright green code in the then and else sections with the correct code for the motors
Here's the code with the same parts that you'll need to complete. You can see how the if/else statement is done in code.
void setup()
{
}
void loop()
{
if ( ( analogRead(0) ) < ( 250 ) )
{
//Right motor forward
//Left motor stopped
}
else
{
//Right motor stopped
//Left motor forward
}
}
As a class, place a winding line of tape on the floor. How quickly can your robot get from the start to the finish? What can you do to make it go fast? Would your time improve if both motors were driving? How fast can you go without driving away from the line?
If your robot could drive forward with both wheels most of the time, it would go faster than jittering back and forth on the edge of the line. To do that, you need two sensors (one on each side of the line) and you'll need to read them at the same time. For this, you need the and command (which can also be written &&) along with else if. Here's a start to the code you might use with a lot that you still need to complete. Hopefully this is enough to get you started using these commands, but there's plenty more information about both and as well as else if.
if ((( analogRead(0)) < (250)) and ((analogRead(1)) < (225))) { //Drive Forward Code Goes Here }
else if (( analogRead(0)) < (250)) { //Turn Left Code }
else if (( analogRead(1)) < (225)) { //Turn Right Code }
else { //If both sensors see the line, something has gone wrong so have the servos stop }
Now with two sensors, your robot should go extra fast. What else could you do to make your Line Follower even faster?