Arduino rover ping))) help!

i have my arduino rover, and it avois object. it uses a ping sensor and turns right everytime it gets 10inches or closer to an object. turning right all the time gets boreing and ive mounted a servo in the front and on top of it is my ping sensor…i was wondering how, i would make it look left, then look right, and choose the best way to turn based on ether furthest distence or maybe just higher average distence. please help!!!

#include <Servo.h> 

Servo motorLeft;
Servo motorRight;
int ledPin = 13;
const int pingPin = 12;

void setup() {
  motorLeft.attach(9);
  motorRight.attach(10);
  Serial.begin(9600);
}

void loop()
{
  long duration, inches, cm;

  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);
  cm = microsecondsToCentimeters(duration);
  
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  
  if(inches <= 10){
    digitalWrite(ledPin, HIGH);
    motorLeft.write(0);
    motorRight.write(0);   
  
  }
  else{
 digitalWrite(ledPin, LOW); 
 motorLeft.write(180);
 motorRight.write(0);  

}
  
  
  delay(200);
}

long microsecondsToInches(long microseconds)
{

  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

Put this code:

  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);
  cm = microsecondsToCentimeters(duration);

into a function that returns the distance measured.

Then, during each pass through loop, position the servo to the three positions, and call this function after each servo position is reached. Store the results in 3 variables - left, right, and center.

Then, make the decision on whether to turn based on the reading stored in center. If it is necessary to turn, turn left or right based on the readings stored in left and right.

Create functions for going straight, turning left, and turning right, too.

so, would i post that code up in a seperate void? or would they go in loops? can you modify mt code like you said, just add 1, like left or right and ill understand and finish it. haha thanks for the help.

so, would i post that code up in a seperate void?

void is a function return type. It is an adjective, not a noun.

long pingDistance()
{
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  pinMode(pingPin, INPUT);
  long duration = pulseIn(pingPin, HIGH);

  long inches = microsecondsToInches(duration);
  return inches;
}

In loop:

  // Position the servo to the left
  // Wait for it to get there
  long leftDist = pingDistance();

  // Position the servo forward
  // Wait for it to get there
  long forwardDist = pingDistance();

  // Position the servo to the right
  // Wait for it to get there
  long rightDist = pingDistance();

  // See if it's clear ahead
  if(forwardDist > 10)
    goStraight();
  else if(leftDist > rightDist)
    goLeft();
  else goRight();