MAKING LINE FOLLOWER.PLS SOMEONE HELP ME! :(

Been doing this project for days now and my problem is my line follower robot cannot follow the line when it takes turn left/right. Im using adafruit motor shield, two TCRT5000 sensors, and a 4WD smart car chassis. I don’t know what to do anymore been calibrating this for days and I really need help.
I made two type of program one is a robot following the black line and one is a robot following the white surface.

Line Follower Reading black Line

#include <AFMotor.h>

AF_DCMotor motorR1(1); //Motor Front-Right
AF_DCMotor motorR2(2); //Motor Back-Right
AF_DCMotor motorL1(3); //Motor Front-Left
AF_DCMotor motorL2(4); //Motor Back-Left
/*------ Program for Line Follower Robot using Arduino----- */
/*-------definning Inputs------*/
#define LS 53      // left sensor. digitalReading output
#define RS 52      // right sensor. digitalReading output
/*-------definning Outputs------*/

int tcrt1Pin = A14;// AnalogReading
int tcrt2Pin = A15;// AnalogReading
void setup()
{
  Serial.begin(9600);
  pinMode(LS, INPUT); // digitalReading for Left Sensor
  pinMode(RS, INPUT);// digitalReading for Right Sensor
 
}
void loop()
{
  int tcrt1 = analogRead(tcrt1Pin); //Sensor tcrt1
  int tcrt2 = analogRead(tcrt2Pin); //Sensor tcrt2
  analogWrite(52,tcrt1/4); //Sensor tcrt1
  analogWrite(53,tcrt2/4); //Sensor tcrt2

  Serial.println(tcrt1);
  Serial.println(tcrt2);
  
  if(digitalRead(LS) && digitalRead(RS))     // Move Forward
  {
    forward();//FORWARD DIRECTION
  }
  
  if(!(digitalRead(LS)) && digitalRead(RS))     // Turn right
  {
    turnright();  //TURN RIGHT DIRECTION
  }
  
  if(digitalRead(LS) && !(digitalRead(RS)))     // turn left
  {
    turnleft();//TURN LEFT DIRECTION
  }
  
  if(!(digitalRead(LS)) && !(digitalRead(RS)))     // stop
  {
    motorstop();
  }
  delay(150);
}
void forward()
{
  motorR1.setSpeed(100);
  motorR2.setSpeed(100);
  motorL1.setSpeed(100);
  motorL2.setSpeed(100);

  motorR1.run(FORWARD);
  motorR2.run(FORWARD);
  motorL1.run(FORWARD);
  motorL2.run(FORWARD);
  digitalWrite(51,HIGH);
  digitalWrite(31,HIGH);
  delay(100);
}
void turnright()
{             
  motorR1.setSpeed(250);
  motorR2.setSpeed(0);
  motorL1.setSpeed(0);
  motorL2.setSpeed(250);

  motorR1.run(FORWARD); ////// TCRT1 SENSING BLACK COLOR.OUTPUT IS HIGH.//////
  motorR2.run(RELEASE); //////////////////////////////////////////////////////
  motorL1.run(RELEASE);
  motorL2.run(FORWARD);
  digitalWrite(31,HIGH);
  delay(100);
}
void turnleft() 
{
  motorR1.setSpeed(0);
  motorR2.setSpeed(250);
  motorL1.setSpeed(250);
  motorL2.setSpeed(0);

  motorR1.run(RELEASE);
  motorR2.run(FORWARD);
  motorL1.run(FORWARD);
  motorL2.run(RELEASE);
  digitalWrite(51,HIGH);
  delay(100);
}

void motorstop()
{
  motorR1.run(RELEASE);
  motorR2.run(RELEASE);
  motorL1.run(RELEASE);
  motorL2.run(RELEASE);
  delay(0);
  digitalWrite(51,LOW);
  digitalWrite(31,LOW);
}

Line follower reading white background

#include <AFMotor.h>

AF_DCMotor motorR1(1); //Motor Front-Right
AF_DCMotor motorR2(2); //Motor Back-Right
AF_DCMotor motorL1(3); //Motor Front-Left
AF_DCMotor motorL2(4); //Motor Back-Left

int tcrt1Pin = A14; //AnalogReading
int tcrt2Pin = A15; //AnalogReading

void setup()
{ 
  delay(2000);  
  Serial.begin(9600);
  pinMode(52,INPUT); //Sensor tcrt1 OUTPUT PIN. DigitalReading
  pinMode(53,INPUT); //Sensor tcrt2 OUTPUT PIN. DigitalReading
  pinMode(51,OUTPUT); //BLUE LED OUTPUT PIN
  pinMode(31,OUTPUT); //RED LED OUTPUT PIN



}

void loop()
{
  int tcrt1 = analogRead(tcrt1Pin); //Sensor tcrt1.AnalogReading
  int tcrt2 = analogRead(tcrt2Pin); //Sensor tcrt2.AnalogReading
  analogWrite(52,tcrt1/4); //Sensor tcrt1 .AnalogReading
  analogWrite(53,tcrt2/4); //Sensor tcrt2 .AnalogReading

  Serial.println(tcrt1);
  Serial.println(tcrt2);

  if ((tcrt1<=500)&&(tcrt2<=500))
  {
    forward();//FORWARD DIRECTION
  }

  if ((tcrt1>=500)&&(tcrt2<=500))
  {
    turnright();  //TURN RIGHT DIRECTION
    
  }

  if ((tcrt1<=500)&&(tcrt2>=500)) 
  {
    turnleft();//TURN LEFT DIRECTION
  }

  else if((tcrt1>=500)&&(tcrt2>=500))
  {
    motorstop();
  }

  delay(150);
}

void forward()
{
  motorR1.setSpeed(100);
  motorR2.setSpeed(100);
  motorL1.setSpeed(100);
  motorL2.setSpeed(100);

  motorR1.run(FORWARD);
  motorR2.run(FORWARD);
  motorL1.run(FORWARD);
  motorL2.run(FORWARD);
  digitalWrite(51,HIGH); //LED
  digitalWrite(31,HIGH); //LED
  delay(100);
}

void turnright()
{             
  motorR1.setSpeed(200);
  motorR2.setSpeed(0);
  motorL1.setSpeed(0);
  motorL2.setSpeed(200);

  motorR1.run(FORWARD); ////// TCRT1 SENSING BLACK COLOR.OUTPUT IS HIGH.//////
  motorR2.run(RELEASE); //////////////////////////////////////////////////////
  motorL1.run(RELEASE);
  motorL2.run(FORWARD);
  digitalWrite(31,HIGH); //LED
  delay(100);
}

void turnleft() 
{
  motorR1.setSpeed(0);
  motorR2.setSpeed(200);
  motorL1.setSpeed(200);
  motorL2.setSpeed(0);

  motorR1.run(RELEASE);
  motorR2.run(FORWARD);
  motorL1.run(FORWARD);
  motorL2.run(RELEASE);
  digitalWrite(51,HIGH); //LED
  delay(100);
}

void motorstop()
{
  motorR1.run(RELEASE);
  motorR2.run(RELEASE);
  motorL1.run(RELEASE);
  motorL2.run(RELEASE);
  delay(0);
  digitalWrite(51,LOW);
  digitalWrite(31,LOW);
}

Line Follower for black line.txt (2.34 KB)

Line follower for white surface.txt (2.2 KB)

killian13th:
my line follower robot cannot follow the line when it takes turn left/right.

It would be helpful if you provided more details on the problem.

Unless your code is longer than the forum will allow please post it using code tags(</> button on the toolbar) like this:
Line Follower for black line

#include <AFMotor.h>

AF_DCMotor motorR1(1); //Motor Front-Right
AF_DCMotor motorR2(2); //Motor Back-Right
AF_DCMotor motorL1(3); //Motor Front-Left
AF_DCMotor motorL2(4); //Motor Back-Left
/*------ Program for Line Follower Robot using Arduino----- */
/*-------definning Inputs------*/
#define LS 53      // left sensor
#define RS 52      // right sensor
/*-------definning Outputs------*/

int tcrt1Pin = A14;
int tcrt2Pin = A15;
void setup()
{
  Serial.begin(9600);
  pinMode(LS, INPUT);
  pinMode(RS, INPUT);
 
}
void loop()
{
  int tcrt1 = analogRead(tcrt1Pin); //Sensor tcrt1
  int tcrt2 = analogRead(tcrt2Pin); //Sensor tcrt2
  analogWrite(52,tcrt1/4); //Sensor tcrt1
  analogWrite(53,tcrt2/4); //Sensor tcrt2

  Serial.println(tcrt1);
  Serial.println(tcrt2);
  
  if(digitalRead(LS) && digitalRead(RS))     // Move Forward
  {
    forward();//FORWARD DIRECTION
  }
  
  if(!(digitalRead(LS)) && digitalRead(RS))     // Turn right
  {
    turnright();  //TURN RIGHT DIRECTION
  }
  
  if(digitalRead(LS) && !(digitalRead(RS)))     // turn left
  {
    turnleft();//TURN LEFT DIRECTION
  }
  
  if(!(digitalRead(LS)) && !(digitalRead(RS)))     // stop
  {
    motorstop();
  }
  delay(150);
}
void forward()
{
  motorR1.setSpeed(100);
  motorR2.setSpeed(100);
  motorL1.setSpeed(100);
  motorL2.setSpeed(100);

  motorR1.run(FORWARD);
  motorR2.run(FORWARD);
  motorL1.run(FORWARD);
  motorL2.run(FORWARD);
  digitalWrite(51,HIGH);
  digitalWrite(31,HIGH);
  delay(100);
}
void turnright()
{             
  motorR1.setSpeed(250);
  motorR2.setSpeed(0);
  motorL1.setSpeed(0);
  motorL2.setSpeed(250);

  motorR1.run(FORWARD); ////// TCRT1 SENSING BLACK COLOR.OUTPUT IS HIGH.//////
  motorR2.run(RELEASE); //////////////////////////////////////////////////////
  motorL1.run(RELEASE);
  motorL2.run(FORWARD);
  digitalWrite(31,HIGH);
  delay(100);
}
void turnleft() 
{
  motorR1.setSpeed(0);
  motorR2.setSpeed(250);
  motorL1.setSpeed(250);
  motorL2.setSpeed(0);

  motorR1.run(RELEASE);
  motorR2.run(FORWARD);
  motorL1.run(FORWARD);
  motorL2.run(RELEASE);
  digitalWrite(51,HIGH);
  delay(100);
}

void motorstop()
{
  motorR1.run(RELEASE);
  motorR2.run(RELEASE);
  motorL1.run(RELEASE);
  motorL2.run(RELEASE);
  delay(0);
  digitalWrite(51,LOW);
  digitalWrite(31,LOW);
}

What’s the purpose of this:

  analogWrite(52,tcrt1/4); //Sensor tcrt1
  analogWrite(53,tcrt2/4); //Sensor tcrt2

Are those the IR leds on the TCRT5000 sensors?

You have this code:

#define LS 53      // left sensor
#define RS 52      // right sensor
/*-------definning Outputs------*/

int tcrt1Pin = A14;
int tcrt2Pin = A15;

First off, I’d recommend making your code more consistent. #defining some pins and then making others ints just makes your code more confusing than necessary. I’d use const byte for all of them. Second you don’t even use the LS and RS definitions:

  analogWrite(52,tcrt1/4); //Sensor tcrt1
  analogWrite(53,tcrt2/4); //Sensor tcrt2

Why do you set LS and RS as inputs:

  pinMode(LS, INPUT);
  pinMode(RS, INPUT);

and then only use them as outputs in your code:

  analogWrite(52,tcrt1/4); //Sensor tcrt1
  analogWrite(53,tcrt2/4); //Sensor tcrt2

What’s on pins 51 and 31:

  digitalWrite(51,HIGH);
  digitalWrite(31,HIGH);

Why didn’t you set those pins as outputs?

Im using 4WD chassis with 4 DC motors for my line follower bot,and two TCRT5000 with a Pins of VCC,GND,A0,and D0. And my problem is when it needs to turn left/right to follow the line,it couldn't take turns fast even the sensors read the INPUTS. I used DigitalRead and AnalogRead to be able to read and analyze the readings. How do I able to calibrate the 4 DC motors with the readings of the Sensors so it will follow the line correctly/accurately?

Sorry if my english is not good.