E-2 Follow Line
Next, you'll code an app that uses the IR line sensors to make your robot follow a line. The line determines the robot's path.

Create Line on Surface

Your teacher might have set up one or more line paths for the class to use for this tutorial
If not, then create a line on your floor or surface (e.g., large sheet of paper, etc.) that forms a closed path (i.e., an oval or rounded rectangle). The line should be about 0.5—0.75 inch wide. The line should not have any "sharp" turns — instead, use a curved path to make any turns.
The diagram below represents a line enclosing an area about 3 feet by 4 feet in size. You'll also use your line for the next test (E-3 Avoid Line), so be sure the area enclosed by the line is at least 2 feet by 3 feet.

How Line Following Works

The robot's goal during line following is to try stay centered on the line as the robot drives.
Let's assume that the robot is trying to follow a dark line on a light-colored surface. When the robot is centered on the line, the center IR line sensor will have a high reading (due to the dark line) while the left and right sensors will have low readings (due to the light-colored surface on either side of the line).
If the robot is trying to follow a line, there are 3 possible situations at any given point:
    If only the center IR line sensor detects the line, this means the robot is centered on the line. In this situation, the robot should drive straight to keep following the line.
    If only the left IR sensor detects the line, this means the line has started to curve to the left. In this situation, the robot should adjust its motors to curve left and keep following the line.
    If only the right IR line sensor detects the line, this means the line has started to curve to the right. In this situation, the robot should adjust its motors to curve right and keep following the line.

Save Copy of App With New Name

In your Arduino code editor, use the "Save As" command to save a copy of the line_sensors_test app as a different app named: follow_line_test
Once you saved the new app name, modify the block comment near the beginning of the app code to change Line Sensors Test to Follow Line Test.

Create Objects for Motors and Button

Your app will need to create new objects (as global variables) to represent the robot's motors and button. Add this code before the setup() function:
1
RedBotMotors motors;
2
RedBotButton button;
Copied!

Add Code for "Press to Start"

This app will use the "Press to Start" code. You'll press the D12 button to "start" the robot. Once the robot is "started," you can press the button again to "pause" the robot. (Pressing the button yet again will "start" the robot again.)
You need to declare global variables for the LED pin and speaker pin. You also need a global variable to keep track of whether or not the robot has been "started." Add this code before the setup() function:
1
int LED = 13;
2
int speaker = 9;
3
bool started = false;
Copied!
Set the pin modes for the LED and speaker by adding this code within the setup() function:
1
pinMode(LED, OUTPUT);
2
pinMode(speaker, OUTPUT);
Copied!
This app does not need the Serial.begin() statement within the setup() function. You can turn this code statement into a comment by typing two forward slashes // at the beginning of the statement.
Next, you need to add the custom function named checkButton() that will check whether the D12 button is pressed, in order to "start" or "pause" the robot.
Add this custom function after the loop() function:
1
void checkButton() {
2
if (button.read() == true) {
3
// reverse value of started
4
started = !started;
5
6
// beep and blink as feedback
7
digitalWrite(LED, HIGH);
8
tone(speaker, 2000);
9
delay(200);
10
digitalWrite(LED, LOW);
11
noTone(speaker);
12
delay(200);
13
}
14
}
Copied!
Next, delete the code statement that calls the testLineSensors() function within the loop() function. This will give you an "empty" loop() function.
Now add this new code within the loop() function:
1
checkButton();
2
if (started == true) {
3
// add code to perform when "started"
4
5
}
6
else {
7
// add code to perform when "paused"
8
9
}
Copied!
Later, you'll add the code to be performed when the robot is started or paused.

Add Custom Function to Follow Line

You'll add a custom function named followLine() which will contain code to use readings from the three IR line sensors to decide whether to drive straight, curve to the left, or curve to the right.
Line following works best at slower speeds, so this function uses an initial value of 100 for the motor power (but shifts each motor power up or down by 50 in order to curve left or right).
This function assumes that your robot will be following a dark line on a light-colored surface. However, you can modify the function to instead follow a light line on a dark surface.
Add this custom function after the loop() function:
1
void followLine() {
2
/* FOLLOW LINE
3
To follow dark line on light surface:
4
Use high threshold & see if sensors greater than threshold
5
6
To follow light line on dark surface:
7
Use low threshold & see if sensors less than threshold
8
*/
9
10
int leftPower, rightPower;
11
int power = 100;
12
int powerShift = 50;
13
int lineThreshold = 800; // change value if necessary
14
15
// get IR sensor readings
16
int leftSensor = leftLine.read();
17
int centerSensor = centerLine.read();
18
int rightSensor = rightLine.read();
19
20
// if line under center sensor, drive straight to stay aligned
21
if (centerSensor > lineThreshold) {
22
// set both motors to same power
23
leftPower = power;
24
rightPower = power;
25
}
26
// if line under left sensor, curve left to realign
27
else if (leftSensor > lineThreshold) {
28
// decrease left motor, increase right motor
29
leftPower = power - powerShift;
30
rightPower = power + powerShift;
31
}
32
// if line under right sensor, curve right to realign
33
else if (rightSensor > lineThreshold) {
34
// increase left motor, decrease right motor
35
leftPower = power + powerShift;
36
rightPower = power - powerShift;
37
}
38
39
// drive motors using power values from above
40
motors.leftDrive(leftPower);
41
motors.rightDrive(rightPower);
42
43
delay(25); // can change delay to adjust line detection sensitivity
44
}
Copied!

Add Code to Perform When Robot is Started

When the D12 button is pressed to "start" the robot, we want the robot to follow the line.
Add this code statement within the if statement in the loop() function, so it will be performed when started is true:
1
followLine();
Copied!

Add Code to Perform When Robot is Paused

Once the robot has been "started," the D12 button can be pressed again to "pause" the robot.
When the robot is "paused," we want make the robot stop driving.
Add this code statement within the else statement in the loop() function, so it will be performed when started is false:
1
motors.brake();
Copied!

Upload App to Robot

Follow the steps to connect your robot to your computer, and upload the app.
Unplug the USB cable from the robot, and place the robot on your line, so only the robot's center IR line sensor is "on" the line.
Press the D12 button to "start" the robot. The robot should (hopefully) follow the line.
When you're done testing the robot, you can pick it up, and press the D12 button to "pause" the robot (or you can press the Reset button).
If you want to test further, place the robot back on the line, and press the button to "start" the robot again.

TROUBLESHOOTING

If your robot is unable to consistently follow the line, here are several things you could investigate:
    Make sure your line path does not have sharp turns.
    Make sure your line is dark enough and the right width. A uniform black line about 0.5–0.75 inch wide is ideal.
    Make sure your surface is lighter in color than your line. A uniform white surface is ideal. However, the surface could be another color or have a pattern, as long as the IR sensor readings for the surface are consistently and significantly lower than the readings for the line.
    You can use the line_sensors_test app to check the IR sensor readings for your line and surface. The readings for a dark line should be consistently high (i.e., line readings of 800 or higher). The readings for your surface should consistently be at least 100 less than your line readings. If necessary, you can modify the value assigned to lineThreshold in the followLine() function.
LOW BATTERY: As your robot's battery power gets low, the IR line sensors will stop working — even though there still might be enough power to keep driving. If your robot was previously successful at line following and then starts having problems, try replacing the robot's batteries.
Last modified 2yr ago