Measuring distance with infrared sensor

Hi all,

I am trying to build a robot that uses a parallax kit. I attached a picture of the bot and circuit.

The goal is to have the robot approach a wall, hit the wall, then reverse until the infrared sensor can no longer see the wall. I then want it to stop for a few seconds.

Below is the code I have so far. I dont know where I am going wrong, but the robot does not drive reverse when it hits the wall. I think I am calling the irDetect function incorrectly, or I may need to write a new one altogether. Any ideas??

#include <Servo.h>

Servo Right;
Servo Left;

void setup() {
// put your setup code here, to run once:

pinMode(2,OUTPUT); //IR LED
pinMode(3,INPUT); //IR sensor
pinMode(5,INPUT); //left whisker
pinMode(7,INPUT); //right whisker
Right.attach(12); //right servo
Left.attach(13); //left servo

}

int irDetect(int irLedPin, int irReceiverPin, long frequency){ 
tone(2, 3800, 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 loop() {
// put your main code here, to run repeatedly:

int irRight = irDetect(2, 3, 38000); // Check for object
//read sensors
int leftLED = digitalRead(5);
int rightLED = digitalRead(7);
delay(300);

//act upon sensory input

//3. right whisker touched, stop, turn left a little then drive straight
if ((leftLED==1)&&(rightLED==0)) {

//brief hold
Right.writeMicroseconds(1500);
Left.writeMicroseconds(1500);
delay(500);

//detect infrared drive reverse briefly
irDetect;
delay(100);
if (irRight = 0){
  Right.writeMicroseconds(1600);
  Left.writeMicroseconds(1400);
  delay(1000);
}

//brief hold
Right.writeMicroseconds(1500);
Left.writeMicroseconds(1500);
delay(500);

return;

}

//4.Neither whisker touched, drive straight 
else // if ((leftLED==1)&&(rightLED==1)) {

//drive straight
Right.writeMicroseconds(1300);
Left.writeMicroseconds(1700);



}

Do you know the difference between these?
if (irRight = 0){
if (irRight == 0){

I do. Sorry I am new to arduino and programming. Thanks for pointing that out.

At this point the bahvior is that is senses the whisker touch then pauses, then continues going forward.
However, if it manually hold the whisker down, it will go reverse briefly, before continuing forward and looping.

Here is what I have now:

#include <Servo.h>

Servo Right;
Servo Left;

void setup() {
// put your setup code here, to run once:

pinMode(2,OUTPUT); //IR LED
pinMode(3,INPUT); //IR sensor
pinMode(5,INPUT); //left whisker
pinMode(7,INPUT); //right whisker
Right.attach(12); //right servo
Left.attach(13); //left servo

}

int irDetect(int irLedPin, int irReceiverPin, long frequency){
tone(2, 3800, 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 loop() {
// put your main code here, to run repeatedly:

int irRight = irDetect(2, 3, 38000); // Check for object
//read sensors
int leftLED = digitalRead(5);
int rightLED = digitalRead(7);
delay(300);

//act upon sensory input

//3. right whisker touched, stop, turn left a little then drive straight
if ((leftLED==1)&&(rightLED==0)) {

//brief hold
Right.writeMicroseconds(1500);
Left.writeMicroseconds(1500);
delay(500);

//detect infrared drive reverse briefly
irDetect;
delay(100);
if (irRight == 0){
  Right.writeMicroseconds(1600);
  Left.writeMicroseconds(1400);
  delay(1000);
}

//brief hold
Right.writeMicroseconds(1500);
Left.writeMicroseconds(1500);
delay(500);

return;

}

//4.Neither whisker touched, drive straight
else // if ((leftLED==1)&&(rightLED==1)) {

//drive straight
Right.writeMicroseconds(1300);
Left.writeMicroseconds(1700);



}

What is connected to your IR input and output pins?

In irDetect() the frequency currently is only 3800Hz. The parameter should be used, or 38000U for frequency.