need help with line following with uno

I don't know what's wrong with this code, my wiring and components are good. But when It crosses the black line it dosn't trigger the second 'if' clause. please help!

#include <Servo.h>
int lphoto=0;
int rphoto=0;
Servo lservo;
Servo rservo;
int ledpin=4;
int lphotopin=0;
int rphotopin=5;
int lservopin=8;
int rservopin=9;
int t=0;

void setup(){
lservo.attach(lservopin);
rservo.attach(rservopin);
pinMode(ledpin,OUTPUT);
Serial.begin(9600);
}
void loop(){
Serial.println(analogRead(lphotopin));
Serial.print(analogRead(rphotopin));
digitalWrite(ledpin,HIGH);
trip1();
}

void forward(){
lservo.writeMicroseconds(1600);
rservo.writeMicroseconds(1400);}
void reverse(){
lservo.writeMicroseconds(1300);
rservo.writeMicroseconds(1700);}
void halt(){
lservo.writeMicroseconds(1500);
rservo.writeMicroseconds(1500);}
void left(){
lservo.writeMicroseconds(1300);
rservo.writeMicroseconds(1300);}
void right(){
lservo.writeMicroseconds(1700);
rservo.writeMicroseconds(1700);}

void trip1(){
while (t<30){
if (analogRead(lphotopin)>=analogRead(rphotopin)){
forward();}
else if (analogRead(lphotopin)<analogRead(rphotopin)){
right();
forward();
t++;}}
while (t>30){
if (analogRead(lphotopin)>=analogRead(rphotopin)){
right();
delay(200);
forward();
t++;}
else if (analogRead(lphotopin)<analogRead(rphotopin)){
forward();}}}

  Serial.println(analogRead(lphotopin));
  Serial.print(analogRead(rphotopin));

Why is one value output using Serial.println() and the other with Serial.print()? How do you know which value is which?

while (t<30){
  if (analogRead(lphotopin)>=analogRead(rphotopin)){
    forward();}   
  else if (analogRead(lphotopin)<analogRead(rphotopin)){
    right();
    forward();
    t++;}}

Why 30? Is there some significance to that number?

What exactly is right() doing? It writes a set of values to the two servos, but forward() immediately changes them.

  1. the two print commands print the values side by side with the the left values on the left and the right values on the right
  2. 30 was an arbitrary number that will be changed after I can get the line following to work. my thoughts were to use a variable to count the number of turns to a specific point and then use that variable to trigger a second while statement to go back to the starting point.
    3)I thought that if i included a delay in between the right and forward commands, that it would delay the reading of the photo resistor values
  1. the two print commands print the values side by side with the the left values on the left and the right values on the right
    No, they don't. The two values aren't even on the same line:

If you had this:

  Serial.print("Left: ");
  Serial.print(analogRead(lphotopin));
  Serial.print("   Right: "); 
  Serial.println(analogRead(rphotopin));

you'd KNOW which value was which.

3)I thought that if i included a delay in between the right and forward commands, that it would delay the reading of the photo resistor values

It would. But, the way it is, the right() function accomplishes nothing. By the time the servos have even thought about moving, you've told them to stop doing that and go straight.

You are trying to follow a line. If the line goes right, you need to turn right. If the line goes left, you need to turn left. If the line goes straight, you need to go straight. There is no need for delay, or to go straight after a turn.

Basically, what you need to do, to follow the line the fastest, is to change the speed of the left motor (faster) and right motor (slower) as the right sensor gets a higher reading (or maybe that should be lower) reading than the left sensor. Stopping and starting is not required.

As you are driving down the road, you don't stomp on the brakes when the road curves right, crank the wheel, and stomp on the gas again, do you? I certainly hope not.

I think I get what you are saying , but how could i write that code?

I think I get what you are saying , but how could i write that code?

The code you have is a good start. You have functions for turning left and right, and for going straight. The only issue is that you are calling the forward function at inappropriate times.

You know when you need to turn left, so turn left. Don't worry about trying to go forward after the turn.

You know when you need to turn right, so turn right. Don't worry about trying to go forward after the turn.

You know when you need to go forward, so go forward.

When you do this, you might see that your code to go left or right causes the robot to turn too much. You can adjust that - it's based on the ratio between left speed and right speed. The ratio does not have to be 100%.

Rather than

void left()
{
  lservo.writeMicroseconds(1300);
  rservo.writeMicroseconds(1300);
}

I'd prefer to see

#define STOP 1500
#define LEFTTURN 200
#define RIGHTTURN -200
void left()
{
  lservo.writeMicroseconds(STOP - LEFTTURN);
  rservo.writeMicroseconds(STOP + RIGHTTURN);
}

If it turns out that 1500 doesn't make the robot stop (maybe 1510 or 1495 does), you can change the value in one place.

You can adjust the differential needed to make the robot turn, and clearly see it as a differential.

You might decide that a turn is best accomplished by keeping one wheel's speed constant, increasing the other wheel's speed, rather than decreasing one and increasing the other. This is easier to do if the left and right wheel speeds are relative to stopped, rather than absolute values.