Problems regarding object detection car

Hi! I am very new to Arduino and I am trying to build an object detecting car that changes direction once it perceives an object <20cm from it. The issue I am having is that the car is halting midway most of the times even though there is no object infront of it. Also, it doesn't seem to turn that much.
My code:

#include <AFMotor.h>
#include <Servo.h>

#define TRIG_PIN A4
#define ECHO_PIN A5
#define SERVO_PIN 10  // Servo 1 on L293D board

// Initialize motors (M4 = Right, M1 = Left)
AF_DCMotor rightMotor(4);
AF_DCMotor leftMotor(1);

Servo scanServo;

void setup() {
    Serial.begin(9600);

    // Initialize motors
    rightMotor.setSpeed(150);
    leftMotor.setSpeed(150);

    // Ultrasonic sensor setup
    pinMode(TRIG_PIN, OUTPUT);
    pinMode(ECHO_PIN, INPUT);

    // Servo setup
    scanServo.attach(SERVO_PIN);
    scanServo.write(90);  // Start at front position
    delay(500);
}

float getDistance() {
    digitalWrite(TRIG_PIN, LOW);
    delayMicroseconds(2);
    digitalWrite(TRIG_PIN, HIGH);
    delayMicroseconds(10);
    digitalWrite(TRIG_PIN, LOW);

    // Measure pulse duration
    long duration = pulseIn(ECHO_PIN, HIGH, 30000); // 30ms timeout
    if (duration == 0) {
        return -1; // No echo received
    }
    // Calculate distance in cm
    float distance = (duration * 0.0343) / 2;
    // Validate distance range
    if (distance < 2 || distance > 400) {
        return -1; // Invalid distance
    }
    return distance;
}

void moveForward() {
    rightMotor.setSpeed(100); // Set right motor speed
    leftMotor.setSpeed(100);  // Set left motor speed
    rightMotor.run(FORWARD);  // Move right motor forward
    leftMotor.run(FORWARD);   // Move left motor forward
}

void moveBackward(int duration) {
    rightMotor.setSpeed(100);  // Set right motor speed
    leftMotor.setSpeed(100);   // Set left motor speed
    rightMotor.run(BACKWARD);  // Move right motor backward
    leftMotor.run(BACKWARD);   // Move left motor backward
    delay(duration);           // Move backward for the specified duration
    stopMotors();               // Stop the motors after moving
}

void turnLeft() {
    rightMotor.setSpeed(70);  // Set right motor speed
    leftMotor.setSpeed(70);   // Set left motor speed
    rightMotor.run(FORWARD);  // Move right motor backward
    leftMotor.run(BACKWARD);   // Move left motor forward
    delay(500);
    stopMotors();               // Stop the motors after turning
}

void turnRight() {
    rightMotor.setSpeed(70);  // Set right motor speed
    leftMotor.setSpeed(70);   // Set left motor speed
    rightMotor.run(BACKWARD);   // Move right motor forward
    leftMotor.run(FORWARD);  // Move left motor backward
    delay(500);
    stopMotors();               // Stop the motors after turning
}

void stopMotors() {
    rightMotor.run(RELEASE);
    leftMotor.run(RELEASE);
}

void loop() {
    float frontDistance = getDistance();
    if (frontDistance == -1) {
        Serial.println("Error: Invalid front distance reading");
        stopMotors();
        return;
    }
    Serial.print("Front Distance: ");
    Serial.println(frontDistance);

    if (frontDistance > 20) {
        moveForward();
    } else {
        stopMotors();
        delay(500);

        // Move backward slightly
        moveBackward(300); // Move backward for 500 milliseconds

        // Scan left and right
        scanServo.write(0);
        delay(500);
        float leftDistance = getDistance();

        scanServo.write(180);
        delay(500);
        float rightDistance = getDistance();

        scanServo.write(90);
        delay(500);

        if (leftDistance != -1 && rightDistance != -1) {
            if (leftDistance > rightDistance) {
                turnLeft();
            } else {
                turnRight();
            }
        } else {
            Serial.println("Error: Invalid distance reading during scan");
        }
    }

    delay(100);
}

I am using an Arduino UNO board with a motor driver L293d shield. I tried to skip soldering so I have connected the L293d with the UNO through Jumper Wires except the Analog pins which I have used for the ultrasonic sensor. The servo is direcvtly connected to the servo1 of the L293d. I am using 3*3.7V power supply and 2 dc motors.

Your wiring per your description is prone to disaster and probably your problem. With the L293D as a motor driver you need to supply it with 3 volts more then you want on the motor. Its internal darlington bipolar outputs drop about 1.4V in each leg depending on load. The best thing you can do at this point is get a decent soldering iron, a practice kit and learn how to solder. If you do not learn that skill you will spend an inordinate amount of time finding bad connections.

I am supplying 11.1V to L293D, is it not sufficient? Also my problem isn't that there are 'bad' connections, the car works smoothly for 4-5 seconds then halts. If then I press the reset button, it again works for 4-5 seconds and stops. So I am a bit doubtful about the code (it is not made by me). Could you please tell me if there is something wrong with the code? Thanks in advance

Your code talks directly to hardware but you did not post an annotated schematic showing how you wired this project. Post one be sure to show all connections, power, ground, power sources and any othe hardware connected to it.

Explain how you know your sensors are not tripping?

Of course it is while it's not doing what You want.

I believe float distance is limited in scope to getDistance()
Are you sure you're getting the actual result returned?