How to avoid obstacle with 2x IR

I've got a Arduino Uno with a Ping sensor and 2 InfraRed sensors on the left and right front of my robot.

I want it to drive autonomous and avoiding objects with Ping (works already), but i want IR as backup system.
i've already the code for detecting objects with IR but i can't get them work together, so i can't detect left and right.

This is what i already got.

//define pins.
#define irLedPinLeft 30          // IR Led on this pin
#define irSensorPinLeft 29       // IR sensor on this pin
#define irLedPinRight 4          // IR Led on this pin
#define irSensorPinRight 3       // IR Sensor on this pin

int irRead(int readPin, int triggerPin); //function prototype

void setup()
{
  pinMode(irSensorPinLeft, INPUT);
  pinMode(irLedPinLeft, OUTPUT);
  pinMode(irSensorPinRight, INPUT);
  pinMode(irLedPinRight, OUTPUT);
  Serial.begin(9600); 
  // prints title with ending line break 
  Serial.println("Program Starting"); 
  // wait for the long string to be sent 
  delay(100); 
}

void loop()
{  
  Serial.println(irRead(irSensorPinLeft, irLedPinLeft)); //display the results
  Serial.println(irRead(irSensorPinRight, irLedPinRight)); //display the results
  delay(10); //wait for the string to be sent
}

/******************************************************************************
 * This function can be used with a panasonic pna4602m ir sensor
 * it returns a zero if something is detected by the sensor, and a 1 otherwise
 * The function bit bangs a 38.5khZ waveform to an IR led connected to the
 * triggerPin for 1 millisecond, and then reads the IR sensor pin to see if
 * the reflected IR has been detected
 ******************************************************************************/
int irRead(int readPin, int triggerPin)
{
  int halfPeriod = 13; //one period at 38.5khZ is aproximately 26 microseconds
  int cycles = 38; //26 microseconds * 38 is more or less 1 millisecond
  int i;
  for (i=0; i <=cycles; i++)
  {
    digitalWrite(triggerPin, HIGH); 
    delayMicroseconds(halfPeriod);
    digitalWrite(triggerPin, LOW); 
    delayMicroseconds(halfPeriod - 1);     // - 1 to make up for digitaWrite overhead    
  }
  return digitalRead(readPin);
}

What i want is a "irDetectLeft" and a "irDetectRight" so i can use a if..else loop to turn left on a irDetectRight, and turn right on a irDetectLeft.

What i want is a "irDetectLeft" and a "irDetectRight" so i can use a if..else loop to turn left on a irDetectRight, and turn right on a irDetectLeft.

So instead of just printing out the values returned from your calls to irRead, store them in two variables you define as irDetectLeft and irDetectRight. Then you can use those two variables as conditions for your turn left and turn right actions. IE... (pseudocode)

if irDetectLeft and not irDetectRight
turn Right

if irDetectRight and not irDetectLeft
turn Left

if irDetectLeft and irDetectRight
Stop.

I've changed the code so i get "irReadLeft" and "irReadRight". But my sketch is very long to do only this job.

Is there any way to shorten this??

//define pins. I used pins 4 and 5
#define irLedPinLeft 30          // IR Led on this pin
#define irSensorPinLeft 29       // IR sensor on this pin
#define irLedPinRight 4          // IR Led on this pin
#define irSensorPinRight 3       // IR Sensor on this pin

//int irRead(int readPin, int triggerPin); //function prototype

void setup()
{
  pinMode(irSensorPinLeft, INPUT);
  pinMode(irLedPinLeft, OUTPUT);
  pinMode(irSensorPinRight, INPUT);
  pinMode(irLedPinRight, OUTPUT);
  Serial.begin(9600); 
  // prints title with ending line break 
  Serial.println("Program Starting"); 
  // wait for the long string to be sent 
  delay(100); 
}

void loop()
{  
  Serial.println(irReadLeft(irSensorPinLeft, irLedPinLeft)); //display the results
  Serial.println(irReadRight(irSensorPinRight, irLedPinRight)); //display the results
  delay(10); //wait for the string to be sent
}

/******************************************************************************
 * This function can be used with a panasonic pna4602m ir sensor
 * it returns a zero if something is detected by the sensor, and a 1 otherwise
 * The function bit bangs a 38.5khZ waveform to an IR led connected to the
 * triggerPin for 1 millisecond, and then reads the IR sensor pin to see if
 * the reflected IR has been detected
 ******************************************************************************/
int irReadLeft(int readPinLeft, int triggerPinLeft)
{
  int halfPeriod = 13; //one period at 38.5khZ is aproximately 26 microseconds
  int cycles = 38; //26 microseconds * 38 is more or less 1 millisecond
  int i;
  for (i=0; i <=cycles; i++)
  {
    digitalWrite(triggerPinLeft, HIGH); 
    delayMicroseconds(halfPeriod);
    digitalWrite(triggerPinLeft, LOW); 
    delayMicroseconds(halfPeriod - 1);     // - 1 to make up for digitaWrite overhead    
  }
  return digitalRead(readPinLeft);
}

int irReadRight(int readPinRight, int triggerPinRight)
{
  int halfPeriod = 13; //one period at 38.5khZ is aproximately 26 microseconds
  int cycles = 38; //26 microseconds * 38 is more or less 1 millisecond
  int i;
  for (i=0; i <=cycles; i++)
  {
    digitalWrite(triggerPinRight, HIGH); 
    delayMicroseconds(halfPeriod);
    digitalWrite(triggerPinRight, LOW); 
    delayMicroseconds(halfPeriod - 1);     // - 1 to make up for digitaWrite overhead    
  }
  return digitalRead(readPinRight);
}

You could get rid of one of those IR read functions.
That would shorten it

This is wat i've already got for now...

#include <Servo.h>

Servo leftServo;
Servo rightServo;

//define pins. 
#define irLedPinLeft 30          // IR Led on this pin
#define irSensorPinLeft 29       // IR sensor on this pin
#define irLedPinRight 4          // IR Led on this pin
#define irSensorPinRight 3       // IR Sensor on this pin


void setup()
{
  tone(5, 440, 1000);
  pinMode(irSensorPinLeft, INPUT);
  pinMode(irLedPinLeft, OUTPUT);
  pinMode(irSensorPinRight, INPUT);
  pinMode(irLedPinRight, OUTPUT);
  /*Serial.begin(9600); 
  // prints title with ending line break 
  Serial.println("Program Starting"); 
  // wait for the long string to be sent 
  delay(100);*/ 
}

void loop()
{ /* 
  Serial.println(irReadLeft(irSensorPinLeft, irLedPinLeft)); //display the results
  Serial.println(irReadRight(irSensorPinRight, irLedPinRight)); //display the results
  delay(10); //wait for the string to be sent*/
  
  int pulseleft;
  int pulseright;
  int (irReadLeft(irSensorPinLeft, irLedPinLeft)); 
  int (irReadRight(irSensorPinRight, irLedPinRight)); 
  
  if (irReadLeft == 0 and irReadRight == 0)
  { //Backup
   pulseleft = 1400;
   pulseright = 1600;   
  }
  else if (irReadLeft == 0)
  { //turn right
   pulseleft = 1400;
   pulseright = 1400;
  }
  else if (irReadRight == 0)
  { //turn left
   pulseleft = 1600;
   pulseright = 1600;
  }
  else 
  {
    pulseleft = 1600;
    pulseleft = 1400;
  }
  
{
  leftServo.attach(9);
  leftServo.writeMicroseconds(pulseleft);
  rightServo.attach(8);
  rightServo.writeMicroseconds(pulseright);
}
}


int irReadLeft(int readPinLeft, int triggerPinLeft)
{
  int halfPeriod = 13; //one period at 38.5khZ is aproximately 26 microseconds
  int cycles = 38; //26 microseconds * 38 is more or less 1 millisecond
  int i;
  for (i=0; i <=cycles; i++)
  {
    digitalWrite(triggerPinLeft, HIGH); 
    delayMicroseconds(halfPeriod);
    digitalWrite(triggerPinLeft, LOW); 
    delayMicroseconds(halfPeriod - 1);     // - 1 to make up for digitaWrite overhead    
  }
  return digitalRead(readPinLeft);
}

int irReadRight(int readPinRight, int triggerPinRight)
{
  int halfPeriod = 13; //one period at 38.5khZ is aproximately 26 microseconds
  int cycles = 38; //26 microseconds * 38 is more or less 1 millisecond
  int i;
  for (i=0; i <=cycles; i++)
  {
    digitalWrite(triggerPinRight, HIGH); 
    delayMicroseconds(halfPeriod);
    digitalWrite(triggerPinRight, LOW); 
    delayMicroseconds(halfPeriod - 1);     // - 1 to make up for digitaWrite overhead    
  }
  return digitalRead(readPinRight);
}

But de if...else string doesn't get any data. The serial.printin shows the data on the serial monitor and the ir sensors seem to respond well. But it wont reach the if...else string. My robot turns only left for some reason...

Any help?

What do the two lines after the declaration of pulseleft and pulseright do?

You are using the irRead functions addresses, not what they return - that's the reason your ifs are misbehaving

First, follow AWOL's suggestion - your IRread routines are identical. Drop one of them and rename the other irRead

Then replace:

  int (irReadLeft(irSensorPinLeft, irLedPinLeft)); 
  int (irReadRight(irSensorPinRight, irLedPinRight));

with this:

  int irReadLeft = irRead(irSensorPinLeft, irLedPinLeft); 
  int irReadRight = irRead(irSensorPinRight, irLedPinRight);

Thanks for the help!

It runs a bit buggy... I think the problem is the IR Sensor or Detector...

Anyway this problem is solved so the topic can be closed!