HC-SR04 won't read while servo is moving

I'm hoping my problem is obvious to those with more experience. I've been scouring the internet, and tearing my hair out for several days trying to figure this out.
I'm building an object avoiding car. I'm using the parts and even the code that most people use for their projects, however, I'm running into a problem.

I'm using;
an Arduino Mega2560 R3 with an Adafruit motor shield v2
Two standard DC motors (they came with to robot chassis)
a servo
an HC-SRO4 ultrasonic sensor

The Arduino is powered by a 9v battery (fresh).
The motor shield is powered by 4 AA batteries (fresh).
The servo is connected to the motor shield in the servo-2 slot
The HC-SRO4 is connected to the 5V and ground on the motorsheild.
The trig is in pin10, and echo in pin11 on the motorsheild.

The motors work smooth, it moves fine.
The ultrasonic is reading fine. It gives me measurements on serial monitor, and stops the car correctly.

The problem comes when the servo rotates the ultrasonic sensor. Its supposed to take measurements on each side, and turn toward the direction with more space.
It appears as if the sensor is not reading when the servo rotates.

I was hoping you could tell me if its the code. This is pretty much the same code that most people use, but I'm not sure if I made an error.

Thank you in advance.

//MotorShield libraries
#include <Wire.h>
#include <Adafruit_MotorShield.h>

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();

// Select which 'port' M1, M2, M3, or M4. In this case, M1
Adafruit_DCMotor *Motor1 = AFMS.getMotor(1);
Adafruit_DCMotor *Motor2 = AFMS.getMotor(2);

#include <Servo.h> //servo library
Servo myservo; //servo name

//Ultrasonic definitions
const int trigPin = 10;
const int echoPin = 11;

//Needed for operation of ultrasonic sensor
long duration;
int distance;

//set pins for LEDs
const int ledPinBlue = 12;
const int ledPinWhite = 13;
const int ledPinRed = 5;
const int ledPinGreen = 4;

void setup() {
AFMS.begin(); //create with the default frequency 1.6KHz
//AMFS.begin(1000); //OR with a different frequency, say 1KHz

//Set the speed to start, from 0 (off) to 255 (max speed)
//Motor 1 set up
Motor1 -> setSpeed (255);
Motor1 -> run (FORWARD);
//turn on motor
Motor1 -> run (RELEASE);

//Motor 2 set up
Motor2 -> setSpeed (255);
Motor2 -> run (FORWARD);
//turn on motor
Motor2 -> run (RELEASE);

  
  //Ultrasonic set up
pinMode (trigPin, OUTPUT);
pinMode (echoPin, INPUT);

//LED set up
//pinMode (ledPinBlue, OUTPUT);
//pinMode (ledPinWhite, OUTPUT);
//pinMode (ledPinRed, OUTPUT);
//pinMode (ledPinGreen, OUTPUT);

myservo.attach (9); //servo motor pin

myservo.write (90);
delay (2000);
distance = pingSound ();
delay (100);
distance = pingSound ();
delay (100);
distance = pingSound ();
delay (100);
distance = pingSound ();
delay (100);

Serial.begin (9600);
}

void loop() {
uint8_t i; //has to do w/ 8 bit information

int distanceRight = 0;
int distanceLeft = 0;
delay (50);
    
if (distance <= 30) { //sets distance in cm of object distance from sensor
  halt ();
  delay (300);
  reverse ();
  delay (300);
  halt ();
  delay (300);
  distanceRight = lookRight ();
  delay (300);
  distanceLeft = lookLeft ();
  delay (300);

  if (distance >= distanceLeft)
  {
    turnRight ();
    halt ();
  }
  else 
  {
    turnLeft ();
    halt ();
  }
}
else {
  ahead ();
}
distance = pingSound ();

Serial.print ("Distance: "); //allows me to check if the sensor is functioning if connected to computer
Serial.println (distance);

delay (150);
}

int pingSound ()
{
   //creates ultrasonic pulse
delay (70);
digitalWrite (trigPin, LOW);
delay (2);
digitalWrite (trigPin, HIGH);
delay (10);
digitalWrite (trigPin, LOW);
duration = pulseIn (echoPin, HIGH);
distance = duration * 0.034/2; //calculation accounts for speed of sound
return distance;
}

int lookRight ()
{
  myservo.write (10);
  delay (500);
  int distance = pingSound ();
  delay (100);
  myservo.write (90);
  return distance;
}

int lookLeft ()
{
  myservo.write (170);
  delay (500);
  int distance = pingSound ();
  delay (100);
  myservo.write (90);
  return distance;
  delay (100);
}

void halt () //lights blue LED when object is closer than set distance
{
  Motor1 -> run (RELEASE);
  Motor2 -> run (RELEASE);
}

void ahead () //lights white LED when object is further than set distance
{
 Motor1 -> run (FORWARD);
 Motor2 -> run (FORWARD);
}

void reverse ()
{
  Motor1 -> run (BACKWARD);
  Motor2 -> run (BACKWARD);
}

void turnRight ()
{
 Motor1 -> run (FORWARD);
 Motor2 -> run (BACKWARD);
}

void turnLeft ()
{
 Motor1 -> run (BACKWARD);
 Motor2 -> run (FORWARD);
}

You used the delay() function. This is a blocking function.
Use millis() instead of delay(). See LED blink without delay example

But mostly you need to sort out the scope of your variables like distance.

By always putting int in front of it when you use it you create separate versions of that variable in loop() and in the lookLeft(), lookRight() functions instead of using the global version created at the top of the program. So you put a value in the lookLeft version then go back to loop() and use a completely different version of distance which has never had a value put in it.

Steve