For Milestone 1, our objective was to use line sensors to successfully follow a line, and to successfully traverse a grid in a figure 8.
The first thing we needed to do to make the robot follow a line while traversing was integrating line sensors, which is a light emitting diode and a photosensitive transistor in one. The line sensor emits IR light from the LED and the phototransistor detects amount of light reflected back. For our robot, we used two line sensors on the sides. Our idea was that if one line sensor was to detect a line, the robot would shift slightly to the other direction to correct its movement. The robot also stops when both sensors detect a line. The pseudocode can be found below:
void loop() {
leftline = analogRead(leftLinePin);
rightline = analogRead(rightLinePin);
Serial.println(leftline);
// for left, above 90 is forward
// for right, less than 90 is forward
if (leftline > threshold && rightline > threshold){
// stop
}
else if (leftline > threshold){
// adjust left
}
else if (rightline > threshold){
// adjust right
}
else
// move forward
delay(100);
}
After achieving line following, we moved on to getting the robot to traverse in a figure 8. This involved using our previous code for line following as well as writing code so that the robot could detect when it was at an intersection, so that it knew when to turn left or right. We originally thought this would be as simple as having 2 for-loops (4 right turns, 4 left turns) but trying to get the robot to recognize when it was at an intersection and how to turn while following a line were a lot more difficult than we originally thought. The basic algorithm can be seen from the void loop() function:
void loop() {
leftline = analogRead(leftLinePin);
rightline = analogRead(rightLinePin);
//turn right 4 times
for (int i = 0; i < 4; i++){
followLine();
turnRight();
}
for (int i = 0; i < 4; i++){
followLine();
turnLeft();
}
}
Our turning algorithm involved the robot detecting when it was at an intersection (both line sensors detect a line) and then moving in the corresponding direction until both line sensors do not detect the line. The code can be found below:
void turnRight(){
while (rightline < threshold || leftline < threshold){
leftline = analogRead(leftLinePin);
rightline = analogRead(rightLinePin);
left.write(155);
right.write(95);
}
left.write(90);
right.write(90);
}
void turnLeft(){
while (rightline < threshold || leftline < threshold){
leftline = analogRead(leftLinePin);
rightline = analogRead(rightLinePin);
left.write(85);
right.write(25);
}
left.write(90);
right.write(90);
}