Line follower robot

Hi,

I am new to arduino and I am currently working on a line following robot using 3 QRE1113 reflectance sensors.

The problem is it doesn’t follow the line and the response is very slow.Before the next response to the sensor reading the robot is already off course.

See my code below and what is wrong:

// Sensors input definition
const int Rsense=A0;
const int Msense=A1;
const int Lsense=A2;

//defining variables
const int threshold=500;
const int Fspeed=225;
const int Nspeed=0;

const int motRig=3;
const int Rdir=12;
const int motLef=11;
const int Ldir=13;
int numAve=10;

int LefIR=0;
int CenIR=0;
int RigIR=0;

void setup() {
//intitialize I/O ports
pinMode(Ldir, OUTPUT);
pinMode(Rdir, OUTPUT);

// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop() {
// read the analog in value:

RigIR=sAverage(numAve,Rsense);
CenIR=sAverage(numAve,Msense);
LefIR=sAverage(numAve,Lsense);

//Motion and considtions

if (RigIR>=threshold)
{
//turn Right
analogWrite(motRig,Nspeed);
analogWrite(motLef,Fspeed);
digitalWrite(Rdir, HIGH);
digitalWrite(Ldir, HIGH);
// analogWrite(motRig,Nspeed);
// analogWrite(motLef,Nspeed);
// delay(2000);

}

else if(LefIR>=threshold)
{
//Turn left
analogWrite(motRig,Fspeed);
analogWrite(motLef,Nspeed);
digitalWrite(Rdir, HIGH);
digitalWrite(Ldir, HIGH);
// analogWrite(motRig,Nspeed);
// analogWrite(motLef,Nspeed);
// delay(2000);
}
else if(CenIR>=threshold)
{
digitalWrite(Rdir, HIGH);
digitalWrite(Ldir, HIGH);
analogWrite(motRig,Fspeed);
analogWrite(motLef,Fspeed);
// analogWrite(motRig,Nspeed);
// analogWrite(motLef,Nspeed);
// delay(2000);

}
Serial.print(“RigIR = " );
Serial.print(RigIR);
Serial.print(”\n LefIR = “);
Serial.println(LefIR);
Serial.print(”\n CenIR = " );
Serial.println(CenIR);

// wait 2 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(2000);
}

//Average function
int sAverage(int nAve, int sInput){

int i,sSum,reading;
sSum=0;
for (int i = 0; i < 10; i++)
{
sSum+= analogRead(sInput);

}
reading=sSum/nAve;
return reading;
}

  // wait 2 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(2000);

You are waiting 2 SECONDS not looking at sensors...

Other: Some sensors like THIS: show "Slightly off tracks" and "Way off track"...