Issues with IR detection

Jlawler:
So would something like this work?

void setup()                                 // Built-in initialization block

{
  pinMode(10, INPUT);  pinMode(9, OUTPUT);  // Left IR LED & Receiver
  pinMode(3, INPUT);  pinMode(2, OUTPUT);    // Right IR LED & Receiver

tone(4, 3000, 1000);                      // Play tone for 1 second
  delay(1000);                              // Delay to finish tone

servoLeft.attach(13);                      // Attach left signal to pin 13
  servoRight.attach(12);                    // Attach right signal to pin 12
}

void loop()                                  // Main loop auto-repeats
{
  int irLeft = irDetect(9, 10, 38000);      // Check for object on left
  int irRight = irDetect(2, 3, 38000);      // Check for object on right

if((irLeft == 0) && (irRight == 0))        // If both sides detect
  {
    backward(1000);                          // Back up 1 second
    turnLeft(800);                          // Turn left about 120 degrees
  }
  else if(irLeft == 0)                      // If only left side detects
  {
    backward(1000);                          // Back up 1 second
    turnRight(400);                          // Turn right about 60 degrees
  }
  else if(irRight == 0)                      // If only right side detects
  {
    backward(1000);                          // Back up 1 second
    turnLeft(400);                          // Turn left about 60 degrees
  }
  else                                      // Otherwise, no IR detected
  {
    forward(20);                            // Forward 1/50 of a second
  }
}

int irDetect(int irLedPin, int irReceiverPin, long frequency)
{
  tone(irLedPin, frequency, 8);              // IRLED 38 kHz for at least 1 ms
  delay(1);                                  // Wait 1 ms
  int ir = digitalRead(irReceiverPin);      // IR receiver -> ir variable
  delay(1);                                  // Down time before recheck
  return ir;                                // Return 1 no detect, 0 detect
}

void forward(int time)                      // Forward function
{
  servoLeft.writeMicroseconds(1700);        // Left wheel counterclockwise
  servoRight.writeMicroseconds(1300);        // Right wheel clockwise
  delay(time);                              // Maneuver for time ms
}

void turnLeft(int time)                      // Left turn function
{
  servoLeft.writeMicroseconds(1300);        // Left wheel clockwise
  servoRight.writeMicroseconds(1300);        // Right wheel clockwise
  delay(time);                              // Maneuver for time ms
}

void turnRight(int time)                    // Right turn function
{
  servoLeft.writeMicroseconds(1700);        // Left wheel counterclockwise
  servoRight.writeMicroseconds(1700);        // Right wheel counterclockwise
  delay(time);                              // Maneuver for time ms
}

void backward(int time)                      // Backward function
{
  servoLeft.writeMicroseconds(1300);        // Left wheel clockwise
  servoRight.writeMicroseconds(1700);        // Right wheel counterclockwise
  delay(time);                              // Maneuver for time ms
}

[\Code]

It might do. I can't tell without more details of your infra-red sensors they seem to have an LED pin and receive pin. You are driving the LED pin at 38kHz and expecting to see a signal on the sensor receive pin. Is that correct?

Are you using standard 38kHz IR receivers e.g. The VS1838B? If so how are you powering the device?

The information about how you are connecting to the sensor seems to change every time you post.

First it was pins 13 and 3. Then it was pins 3,5,7 and 11 and now it is 2, 3, 9 and 10. All very confusing.

As I said in my previous post we need details about the sensors and how they are connected.

Try hand drawing the circuit on a blank piece of paper, photographing it with your phone and posting the jpeg.

Ian