Help with code for obstacle avoiding robot with HC-SR04 on servo

I have been working at this for days now and would appreciate any help. I am trying to figure out the code for an obstacle avoidance robot with a HC-SR04 sensor mounted on a servo for scanning.
set up-
arduino uno with adafruit motor controller V2.3
mounted on platform with Emgreat geared dc motors.
I use these libraries-

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include “utility/Adafruit_PWMServoDriver.h”
#include <Servo.h>

I can make it work without the scanning servo but I really want it to take three distance measurements , left, right and center and react to those measurements.

I have found that the sensor will work without a special library and can work with ultrasonic.h included. It will not work with Ping or New Ping libraries which it seems everyone uses.

I am new to coding so I have been trying to translate and adapt from other code but I the 3 position servo and sensor scan has been too much for me to adapt.

in case it helps, below is the code that is working for me. It does not have a scanning servo.
Thanks for any help.

Obstacle_Avoiding_Robot_Code_w_o_servo_swivel.ino (2.11 KB)

I've been pondering doing this too, but the Zumo chassis I use is a bit tight for space and not sure where I'll fit a servo in.

But anyway, I'd go with something like having 3 distance variables not just the one, say distanceLeft, distanceAhead, distanceRight.

Then in loop send the servo to the left with say myservo.write(0), and take distanceLeft. Then myservo.write(90) and take distanceAhead, and then same for distanceRight.

Some comparisons of the various distances will then inform the decision of where to go next.

pseudo code:

in loop()

move servo to left
measure distanceleft

move servo to middle
measure distanceahead

move servo to right
measure distanceright

compare values

based on comparison:
    go ahead
    go left
    go right
    go back
    panic and roll over