Arduino Uno Boebot Hall Runner

I am relatively new to Arduino although I know some basic Javascript and electronics. My school purchased 9 arduino starter kits and the teachers weren't sure what they wanted to do with them, so I 'borrowed' one and assembled a boebot using two parallax continuous servos. The boebot kit was from 2006 in case you were wondering. I only used the frame, servos, and wheels from said kit. I am using the Arduino Uno (Branded as Elegoo Uno R3, the kit was from Elegoo) to control everything. My only sensor is a HC-SR04 ultrasonic sensor, and I have one indication LED. The objective of the robot is to move through a straight hallway and avoid obstacles such as boxes. The code I wrote is rough and could be improved upon, but it is functional, my only issue is consistency (the sonic sensor doesn't sense some objects, the who thing drift left or right, etc.).
The method I am using to navigate is when the robot finds an obstacle, it will turn right and go as far as it can, then turn left. Then as long as there is an object in front of it, it will move left and check if there's an object in front of it again. Once there isn't an object in front, it will go forward. The robot goes through the different modes by using the 'direct' variable. There are some remnants of things I tried but gave up on, but didn't delete. They are just idle variables.

If anyone has any suggestions on improving either the consistency or code, please feel free to comment.

//  Servo Setup
#include <Servo.h>
Servo SerL;
Servo SerR;

//  Pins
const int trig = 6;
const int echo = 7;
const int led = 13;
const int sr = 8;
const int sl = 9;

const int turnDelay = 750;
//  variables
int direct = 0;
boolean fre = true;
int distance;

int calcDist() {
  long duration, distance;
  digitalWrite(trig, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trig, HIGH);
  delayMicroseconds(10); // Added this line
  digitalWrite(trig, LOW);
  duration = pulseIn(echo, HIGH);
  distance = (duration/2) / 29.1;
  return distance;
}

void Move() {
  SerL.writeMicroseconds(2000);
  SerR.writeMicroseconds(1000);
  delay(1500);
  SerR.writeMicroseconds(1500);
  SerL.writeMicroseconds(1500);
}

void turnR() {
  SerL.writeMicroseconds(2000);
  SerR.writeMicroseconds(2000);
  Serial.print("Ran Turn Right");
  Serial.print("\n");
  digitalWrite(led, HIGH);
  delay(turnDelay);
  digitalWrite(led, LOW);
  SerL.writeMicroseconds(1500);
  SerR.writeMicroseconds(1500);
}

void turnL() {
  SerL.writeMicroseconds(1000);
  SerR.writeMicroseconds(1000);
  digitalWrite(led, HIGH);
  Serial.print("Ran Turn Left");
  Serial.print("\n");
  delay(turnDelay);
  digitalWrite(led, LOW);
  SerL.writeMicroseconds(1500);
  SerR.writeMicroseconds(1500);
}

void setup() {
  pinMode(sl, OUTPUT);
  pinMode(sr, OUTPUT);
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  pinMode(led, OUTPUT);
  
  SerL.attach(sl);
  SerR.attach(sr);
  
  Serial.begin(9600);
  Serial.print("Ran Setup");
  Serial.print("\n");
}

void loop() {
//Distance
  Serial.print("\n");
  distance = calcDist();
  if(distance < 20 && distance > 0) {
    if(direct == 0) {
      direct = 1;
      turnR();
      distance = calcDist();
      if(distance > 20) {      
        SerL.writeMicroseconds(2000);
        SerR.writeMicroseconds(1000);
        calcDist();
      }
    }
    else if(distance < 20 && direct == 1) {
      turnL();
      distance = calcDist();
      direct = 2;
    }
    else if(distance < 20 && direct == 2) {
      turnL();
      Move();
      turnR();
      delay(50); 
    }
  }
  else if(direct != 1) {
    direct = 0;
    fre = true;
    Serial.print("Going");
    Serial.print("\n");
    SerL.writeMicroseconds(2000);
    SerR.writeMicroseconds(1000);
  }

//  Serial Print
  distance = calcDist();
  Serial.print("\n");
  Serial.print(direct);
  Serial.print("\n");
  Serial.print(distance);
  Serial.print("\n\n");
  delay(500);
  
}

Another note that may need clarity is the robot's objective is to simply go straight and avoid obstacles, not solve a maze. Essentially it needs to navigate random boxes and "gates" or two boxes side by side. It's a simple project for a high school class, and I am helping make it for the teachers.

dogburtjunior:
my only issue is consistency (the sonic sensor doesn't sense some objects, the who thing drift left or right, etc.).

Your description is very vague and your question is very wide.

Have you done any tests to help you figure out why it is not detecting some objects?

My wild guess is that all those delay()s are the root of the problem. The Arduino can do nothing else during a delay(). Have a look at how millis() is used to manage timing without blocking in Several things at a time

I also suspect that managing turns using time is not going to work well. Errors can easily accumulate. Really you need something like a compass module that gives the Arduino an independent measure of the direction it is pointing.

...R

Thanks Robin, I got rid of most of the delay()s, but for this project I am trying to use only parts that came with kits from the school, which sadly did not include a compass. I may ask the teachers if they want to spend some money on some compass modules, which shouldn't be too much. The consistency of detecting objects is because of the ultrasonic sensor not picking up soft objects that absorb the pings. It works decently with the way it is currently, I just wanted to see if anyone saw anything that stood out to them. I will work on using millis() to run multiple things at a time, and I may make it more more complex but better at navigating by using millis() on the return run. Right now I am just making a prototype so some of the reliability issues should be fixable later on, but for now I can at least prove the project is do-able for high school students at a basic level. Thank you, I will try the compass and see how it works. For now I will be done with this project.