Obstacle Avoidance Robot with IR sensor problem

I have started building my first robot. For proximity sensing, I have built up simple IR sensors[. The hardware works corrctly on it's own. However when I try to implement obstacle detection in my code,The front sensor always indicates that there is an obstacle in it's path(even when there isn't), and the right and left sensors constantly change between having an obstacle in it's path or not. I cannot seem to figure out what is incorrect. My robot consists of 3 sensors, a front left and right sensor, and by using a truth table, I have come up with 8 possible actions that the robot should take.

//Pin definitions

// Motor A-Right
int EnableA = 9;
int In1 = 2;
int In2 = 3;

// Motor B-Left
int EnableB = 10;
int In3 = 4;
int In4 = 5;

//Right Sensor
int IRpin = A0;               
int IRemitter = 8;  

//Left Sensor
int IRpin1 = A1; 
int IRemitter1 = 0;

//Front sensor
int IRpin2 = A2;
int IRemitter2 = 13;
 
int ambientIR;       // variable to store the IR coming from the ambient
int obstacleIR;      // variable to store the IR coming from the obstacle
int value[10];       // variable to store the IR values
int distance;        // variable that will tell if there is an obstacle or not in the right sensor
int distancea;       // variable that will tell if there is an obstacle or not in the left sensor
int distanceb;       // variable that will tell if there is an obstacle or not in the front sensor

void setup(){
  //Configure pin modes
  
  //Motor A all outputs
  pinMode (EnableA, OUTPUT);
  pinMode (In1, OUTPUT);
  pinMode (In2, OUTPUT);
  
  //Motor B all outputs
  pinMode (EnableB, OUTPUT);
  pinMode (In3, OUTPUT);
  pinMode (In4, OUTPUT);
  
  //LED pins for testing purposes
  pinMode(7,OUTPUT); //right sensor LED
  pinMode(1,OUTPUT); //left sensor LED
  pinMode(11,OUTPUT); //front sensor LED
  
  
  
  pinMode(IRemitter,OUTPUT); //Right sensor
  digitalWrite(IRemitter,LOW);

  pinMode(IRemitter1,OUTPUT);  //Left Sensor
  digitalWrite(IRemitter1,LOW);

  pinMode(IRemitter2,OUTPUT);  //Front Sensor
  digitalWrite(IRemitter2,LOW); 
}

//Define functions for each motion

void ForwardA(){
 analogWrite (9, 128); //50% duty cycle PWM
 digitalWrite (2, HIGH);
 digitalWrite (3, LOW);
}

void ForwardB(){
  analogWrite (10, 128); //50% duty cycle PWM
  digitalWrite (4, HIGH);
  digitalWrite (5, LOW);
}

void Forward(){
  ForwardA();
  ForwardB();
}

void BackwardA(){
  analogWrite (9, 128);
  digitalWrite (2, LOW);
  digitalWrite (3, HIGH);
}

void BackwardB(){
  analogWrite (10, 128);
  digitalWrite (4, LOW);
  digitalWrite (5, HIGH);
}

void Backward(){
  BackwardA();
  BackwardB();
}

void RightTurnA(){
  analogWrite (9, 0);
  digitalWrite (2, LOW);
  digitalWrite (3, LOW);
}

void RightTurnB(){
  analogWrite (10, 128);
  digitalWrite (4, HIGH);
  digitalWrite (5, LOW);
}

void RightTurn(){
  RightTurnA();
  RightTurnB();
}

void LeftTurnA(){
  analogWrite (9, 128);
  digitalWrite (2, HIGH);
  digitalWrite (3, LOW);
}

void LeftTurnB(){
  analogWrite (10, 0);
 digitalWrite (4, LOW);
 digitalWrite (5, LOW);
}

void LeftTurn(){
  LeftTurnA();
  LeftTurnB();
  //delay(duration);
}

void Stop(){
  analogWrite(9,0);
  analogWrite(10,0);
}

void Start(){
  analogWrite(9,128);
  analogWrite(10,128);
}

//for right sensor
int readIR(int times){
  for(int x=0;x<times;x++){
    digitalWrite(IRemitter,LOW);     //turning the IR LEDs off to read the IR coming from the ambient
    delay(1);                        // minimum delay necessary to read values
    ambientIR = analogRead(IRpin);   // storing IR coming from the ambient
    digitalWrite(IRemitter,HIGH);    //turning the IR LEDs on to read the IR coming from the obstacle
    delay(1);                        // minimum delay necessary to read values
    obstacleIR = analogRead(IRpin);  // storing IR coming from the obstacle
    value[x] = ambientIR-obstacleIR; // calculating changes in IR values and storing it for future average
  }
  
  for(int x=0;x<times;x++){          // calculating the average based on the "accuracy"
    distance+=value[x];
  }
  return(distance/times);            // return the final value
}

//for left sensor
int readIR1(int times){
 for(int x=0;x<times;x++){
    digitalWrite(IRemitter1,LOW);     //turning the IR LEDs off to read the IR coming from the ambient
    delay(1);                        // minimum delay necessary to read values
    ambientIR = analogRead(IRpin1);   // storing IR coming from the ambient
    digitalWrite(IRemitter1,HIGH);    //turning the IR LEDs on to read the IR coming from the obstacle
    delay(1);                        // minimum delay necessary to read values
    obstacleIR = analogRead(IRpin1);  // storing IR coming from the obstacle
    value[x] = ambientIR-obstacleIR; // calculating changes in IR values and storing it for future average
 }
  
  for(int x=0;x<times;x++){          // calculating the average based on the "accuracy"
    distancea+=value[x];
  }
  return(distancea/times);            // return the final value
}

//for front sensor
int readIR2(int times){
  for(int x=0;x<times;x++){
    digitalWrite(IRemitter2,LOW);     //turning the IR LEDs off to read the IR coming from the ambient
    delay(1);                        // minimum delay necessary to read values
    ambientIR = analogRead(IRpin2);   // storing IR coming from the ambient
    digitalWrite(IRemitter2,HIGH);    //turning the IR LEDs on to read the IR coming from the obstacle
    delay(1);                        // minimum delay necessary to read values
    obstacleIR = analogRead(IRpin2);  // storing IR coming from the obstacle
    value[x] = ambientIR-obstacleIR; // calculating changes in IR values and storing it for future average
  }
  
  for(int x=0;x<times;x++){          // calculating the average based on the "accuracy"
    distanceb+=value[x];
  }
  return(distanceb/times);            // return the final value
}

void loop(){
  distance = readIR(5); 
  distancea=readIR1(5);
  distanceb=readIR2(5);
  if (distance<1 && distancea<1 && distanceb<1){
    //if there are  no bstacle sin front of right,left or front sensors, go front, led will be low
      digitalWrite(7,LOW);
      digitalWrite(1,LOW);
      digitalWrite(11,LOW);
      Forward();
  }
  if (distance>1 && distancea<1 && distanceb<1){
    //if there is an obstacle in front of right sensor, and no obstacle in front and left sensors, go front, 
      digitalWrite(7,HIGH);
      digitalWrite(1,LOW);
      digitalWrite(11,LOW);
      Forward();
  }

 if(distance<1 && distancea>1 && distanceb<1){
   //if there is na obstacle in front of left sensor, no obstacle in front of front and right sensors, go front
    digitalWrite(1,HIGH);
    digitalWrite(7,LOW);
      digitalWrite(11,LOW);
    Forward();
  }
  
 if (distance>1 && distancea>1 && distanceb<1){
    //if there is an obsyacle in front of left and  right sensors, no obstacle in front front sensor, go front
      digitalWrite(7,HIGH);
      digitalWrite(1,HIGH);
    digitalWrite(11,LOW);  
     Forward();
  }
if(distance<1 && distancea<1 && distanceb>1){
   //if there is na obstacle in front of front sensor, no obstacle in front of left and right sensors, go right
    digitalWrite(11,HIGH);
    digitalWrite(7,LOW);
      digitalWrite(1,LOW);
    RightTurn();
  }
  if(distance>1 && distancea<1 && distanceb>1){
   //if there is na obstacle in front of front and right sensors, no obstacle in left, go left
     digitalWrite(7,HIGH);
    digitalWrite(11,HIGH);
    digitalWrite(1,LOW);
    LeftTurn();
  }
   if(distance<1 && distancea>1 && distanceb>1){
   //if there is na obstacle in front of front and left sensors, no obstacle in right, go right
    digitalWrite(1,HIGH);
    digitalWrite(11,HIGH);
    digitalWrite(7,LOW);
    RightTurn();
  }
  if(distance>1 && distancea>1 && distanceb>1){
   //if there is na obstacle in front of all sensors, go back
    digitalWrite(1,HIGH);
    digitalWrite(7,HIGH);
    digitalWrite(11,HIGH);
    Backward();
  }
   
  }

](http://www.instructables.com/id/Simple-IR-proximity-sensor-with-Arduino/all/)

The entire code is abitlong to go through, however I think my problem lies in the void loop();

There is no reason to store the values in an array in readIR(), when all you do is then loop to add up all the values. Just add the new value to distance in the first loop. Get rid of the array and the second loop.

Having two nearly identical functions is stupid. Change one function to take a pin number, and get rid of the other function.

Make that three functions!

  distance = readIR(5); 
  distancea=readIR1(5);
  distanceb=readIR2(5);

It really pisses me off when people add suffixes to 2 out of 3 variables. If you can't, for some bizarre reason, use an array, the add suffixes to ALL the related variables.

Now, to deal with your problem. Ditch all the code that deals with moving the motors. If you can't get good data from the sensors, being able to move is pointless. Get rid of all the crap that deals with the motors until you get good data from the sensors.

Get rid of two of the three functions that get data from the sensors. Make the ONE remaining function work for any sensor (pin). Then, you'll know if you have a hardware problem (a bad sensor or bad wiring for that sensor) or a software problem.

If you use ONE function to read from three pins, and get good data for two of the three pins, then you have a hardware problem. If you get good data for none of the sensors, then you have a software problem. But, you only have ONE software problem, not three.