Help with robot code

so i have made an obstacle avoidance robot but there seems to be a problem as it will not return a value when the ping sensor turns left or right

#include<Servo.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // lcd screen pins
const int pingPin = 4; //declares the ping pin
const int irPin = 0;  //Sharp infrared sensor pin
const int RForward = 180; //full speed forward
const int RBackward = 0; //full speed reverse
const int LForward = RBackward; //this decides the motor control
const int LBackward = RForward; //this decides the motor control
const int RNeutral = 90; //nuetral for motor
const int LNeutral = 90; //nuetral for motor
const int dangerThresh = 2; //threshold for obstacles (in)
int leftDistance, rightDistance; //distances on either side
Servo panMotor;  //this is the "neck" of the robot
Servo leftMotor; //declare left servo motor
Servo rightMotor; //declare right servo motor
long duration; //time it takes to recieve PING))) signal

void setup() 
{
  Serial.begin(9600); //allows for serial monitoring
  lcd.begin(16, 2); //turn on the lcd screen
  lcd.print("Hello Jason"); //threw this in for fun
  delay(250);
  lcd.clear();  //clears the lcd
  lcd.print("Activating KITT");  //threw this in for fun
  delay(250);
  lcd.clear();  //clears the lcd
  rightMotor.attach(5); //attach right servo motor to proper pin
  leftMotor.attach(6); //attach left servo motor to proper pin
  panMotor.attach(3); //attach pan servo motor to proper pin
  panMotor.write(90); //center the "neck" of the robot
  delay(50);
  lcd.print("KITT Online");  //threw this in for fun
  delay(250);
  lcd.clear();  //clears the lcd
  
}


void loop() 
{
   
  int distanceFwd = ping(); //tells the robot distance forward
  if (distanceFwd>dangerThresh) //if path is clear
  {
    leftMotor.write(LForward); //move forward
    rightMotor.write(RForward); //move forward
  }
  else //if path is blocked
  {
    leftMotor.write(LNeutral);  //stop forward motion
    rightMotor.write(RNeutral); //stop forward motion
    panMotor.write(0); // turn ping sesor right
    delay(1000);
    rightDistance = ping(); //scan to the right
    delay(1000);
    panMotor.write(180);  //turn ping sensor left
    delay(1000);
    leftDistance = ping(); //scan to the left
    delay(1000);
    panMotor.write(90); //return to center
    delay(1000);
    compareDistance();  //determine the distance on both sides
  }
}
void compareDistance()
{
  if (leftDistance>rightDistance) //if left is less obstructed 
  {
    leftMotor.write(LBackward); //turn left
    rightMotor.write(RForward); //turn left
    delay(100); 
  }
  else if (rightDistance>leftDistance) //if right is less obstructed
  {
    leftMotor.write(LForward);  //turn right
    rightMotor.write(RBackward); //turn right
    delay(1000);
  }
   else //if they are equally obstructed
  {
    leftMotor.write(LForward); //turn 180 degrees
    rightMotor.write(RBackward); //turn 180 degrees
    delay(1000);
  }
}
long ping()  //ping sensor programming from library
{
  long duration, inches;
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  inches = microsecondsToInches(duration);
  lcd.print(inches);
  lcd.print("in to obstacle");  //displays ditance on the lcd
  delay(100);
  lcd.clear();  //clears the lcd
  delay(100);
  Serial.print(inches);
  Serial.print("in, ");
  Serial.println(); // this is for serial monitoring of the ping signal
  return inches; //return measurement for ping sensor
}
long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}

I have to present this to a bunch of kids tomorrow and just realized this error please help

Do you get any values at all if you print the left and right distances ?

Also, note that ping() returns a long but you are comparing the value returned with ints.

but there seems to be a problem as it will not return a value when the ping sensor turns left or right

How does the ping sensor turn right or left?

Be sure to allow plenty of time for answers. Those delay()s are going to kill you.

I think it had to do with the delays getting ready to retest it and the ping sensor is mounted on a servo for turning

ok so far its kind of working but i still get no value showing on the lcd screen when it turns right or left

so i still have the same problem with ping returning a zero after the pan servo moves it left or right i changed the delays and still the same thing

#include<Servo.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // lcd screen pins
const int pingPin = 4; //declares the ping pin
//const int irPin = 0;  //Sharp infrared sensor pin
const int RForward = 180; //full speed forward
const int RBackward = 0; //full speed reverse
const int LForward = RBackward; //this decides the motor control
const int LBackward = RForward; //this decides the motor control
const int RNeutral = 90; //nuetral for motor
const int LNeutral = 90; //nuetral for motor
const int dangerThresh = 4; //threshold for obstacles (in)
int leftDistance, rightDistance; //distances on either side
Servo panMotor;  //this is the "neck" of the robot
Servo leftMotor; //declare left servo motor
Servo rightMotor; //declare right servo motor
long duration; //time it takes to recieve PING))) signal

void setup() 
{
  Serial.begin(9600); //allows for serial monitoring
  lcd.begin(16, 2); //turn on the lcd screen
  lcd.print("Hello Jason"); //threw this in for fun
  delay(250);
  lcd.clear();  //clears the lcd
  lcd.print("ScrapBot V1.0");  //threw this in for fun
  delay(250);
  lcd.clear();  //clears the lcd
  lcd.print("SCRAPPER SAYS HI");  //threw this in for fun
  delay(250);
  lcd.clear();  //clears the lcd
  rightMotor.attach(5); //attach right servo motor to proper pin
  leftMotor.attach(6); //attach left servo motor to proper pin
  panMotor.attach(3); //attach pan servo motor to proper pin
  panMotor.write(90); //center the "neck" of the robot
  delay(50);

  
}


void loop() 
{
   
  int distanceFwd = ping(); //tells the robot distance forward
  if (distanceFwd>dangerThresh) //if path is clear
  {
    leftMotor.write(LForward); //move forward
    rightMotor.write(RForward); //move forward
  }
  else //if path is blocked
  {
    leftMotor.write(LNeutral);  //stop forward motion
    rightMotor.write(RNeutral); //stop forward motion
    panMotor.write(0); // turn ping sesor right
    delay(500);
    rightDistance = ping(); //scan to the right
    delay(500);
    panMotor.write(180);  //turn ping sensor left
    delay(500);
    leftDistance = ping(); //scan to the left
    delay(500);
    panMotor.write(90); //return to center
    delay(100);
    compareDistance();  //determine the distance on both sides
    }
}
void compareDistance()
{
  if (leftDistance>rightDistance) //if left is less obstructed 
  {
    leftMotor.write(LBackward); //turn left
    rightMotor.write(RForward); //turn left
    delay(500); 
  }
  else if (rightDistance>leftDistance) //if right is less obstructed
  {
    leftMotor.write(LForward);  //turn right
    rightMotor.write(RBackward); //turn right
    delay(500);
  }
   else //if they are equally obstructed
  {
    leftMotor.write(LForward); //turn 180 degrees
    rightMotor.write(RBackward); //turn 180 degrees
    delay(500);
  }
}
long ping()  //ping sensor programming from library
{
  long duration, inches;
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  inches = microsecondsToInches(duration);
  lcd.print(inches);
  lcd.print("in to obstacle");  //displays ditance on the lcd
  delay(100);
  lcd.clear();  //clears the lcd
  delay(100);
  Serial.print(inches);
  Serial.print("in, ");
  Serial.println(); // this is for serial monitoring of the ping signal
  return inches; //return measurement for ping sensor
}
long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}

I only have one question. If the function ping() returns a long, why the variables that hold the values that the function returns are integer?

Umm im not sure how to answer that i used the ping code from arduino library and i need the output for the display to be a number

Do the duration variable have the value zero too? You can print to the LCD this value.
And if you start with the neck turned to the left (or the right) and move to centre in the loop() function, do the ping return the right value?

EDIT: If you do a dummy ping in setup the ping in the loop function still working?

once the "neck" moves left or right ping returns a 0 then when the "neck" is centered ping return normal values also i have noticed that the ping has been randomly returning false values while moving forward

as for the duration variables are you referring to the long duration; part of the code?

long ping()  //ping sensor programming from library
{
  long duration, inches;
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);     ///////// <- this duration, add:   lcd.print(duration);     delay(1000);   //////////
  inches = microsecondsToInches(duration);
  lcd.print(inches);
  lcd.print("in to obstacle");  //displays ditance on the lcd
  delay(100);
  lcd.clear();  //clears the lcd
  delay(100);
  Serial.print(inches);
  Serial.print("in, ");
  Serial.println(); // this is for serial monitoring of the ping signal
  return inches; //return measurement for ping sensor
}

To have some clue here the problem is, in the sense part, or in the math.

ok i tried that and it made it even worse

i think the problem is somewhere in this part of the code because whenever the "neck" turns to the left or right the ping sensor gives a delay then returns a zero and centers the "neck"

void loop() 
{
   
  int distanceFwd = ping(); //tells the robot distance forward
  if (distanceFwd>dangerThresh) //if path is clear
  {
    leftMotor.write(LForward); //move forward
    rightMotor.write(RForward); //move forward
  }
  else //if path is blocked
  {
    leftMotor.write(LNeutral);  //stop forward motion
    rightMotor.write(RNeutral); //stop forward motion
    panMotor.write(0); // turn ping sesor right
    delay(500);
    rightDistance = ping(); //scan to the right
    delay(500);
    panMotor.write(180);  //turn ping sensor left
    delay(500);
    leftDistance = ping(); //scan to the left
    delay(500);
    panMotor.write(90); //return to center
    delay(100);
    compareDistance();  //determine the distance on both sides
    }
}
void compareDistance()
{
  if (leftDistance>rightDistance) //if left is less obstructed 
  {
    leftMotor.write(LBackward); //turn left
    rightMotor.write(RForward); //turn left
    delay(500); 
  }
  else if (rightDistance>leftDistance) //if right is less obstructed
  {
    leftMotor.write(LForward);  //turn right
    rightMotor.write(RBackward); //turn right
    delay(500);
  }
   else //if they are equally obstructed
  {
    leftMotor.write(LForward); //turn 180 degrees
    rightMotor.write(RBackward); //turn 180 degrees
    delay(500);
  }
}

I think that what we are trying to tell you is that we can't see your serial output, and you are doing a crappy job of explaining what you see. Print the angle that the servo is pointing at, and the ping() output after the function returns. Make sure everything that is printed is identified (no mystery values) and SHOW US THE OUTPUT!

well i will try again right now i believe it is a hardware problem because even running the ping example is returning erroneous values

Maybe is a problem like this.

What kind of motors are you using?
You can do too several (10 or 20) measurements at the same position an calculate the average, maybe this way you can mask the problem. But anyway read the other thread.

EDIT: The answer appears at this point (1 capacitor for each motor, and change the inputs pinModes from INPUT to INPUT_PULLUP).

im using two parralax continuous rotation servos and an eflite micro servo all of which have worked fine with the sensor together in previous projects

So, the problem is hardware but without being hardware (because the hardware is fine because it was working fine in another project). I don't know what it can be.

well after reading the parralax forums regarding symptoms of a ping sensor failure im going to take the safe route and replace the sensor before moving ahead in the project although i am happy to say that little scrapper the robot made the kindergarten class very happy this morning so happy infact they almost missed recess asking questions about how he was built how he works and what his components do