Self-driving Car Ultrasonic Sensor Question

I am new to Arduino and trying to build a self driving car that changes direction as soon as the sensor detects an obstacle.
The problem i am facing is that it moves just in one direction. And although the sensor identifies obstacles, it doesn’t transmit the information to the device for it to change direction.

The code I have done so far is the following:

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

#define trigPin 12 // define the pins of your sensor
#define echoPin 13 

// DC motor on M2
AF_DCMotor motor(2);
// DC hobby servo
Servo servo1;
// Stepper motor on M3+M4 48 steps per revolution
AF_Stepper stepper(48, 2);

void setup() {
Serial.begin(9600);           // set up Serial library at 9600 bps
Serial.println("Motor party!");

// turn on servo
servo1.attach(9);
 pinMode(trigPin, OUTPUT);// set the trig pin to output (Send sound waves)
pinMode(echoPin, INPUT);// set the echo pin to input (recieve sound waves)
 
// turn on motor #2
motor.setSpeed(200);
motor.run(RELEASE);
}

int i;

// Test the DC motor, stepper and servo ALL AT ONCE!
void loop() {
   long duration, distance; // start the scan
digitalWrite(trigPin, LOW);  
delayMicroseconds(2); // delays are required for a succesful sensor operation.
digitalWrite(trigPin, HIGH);

delayMicroseconds(10); //this delay is required as well!
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;// convert the distance to centimeters.
if (distance < 10)/*if there's an obstacle 25 centimers, ahead, do the following: */ {   
 Serial.println ("Close Obstacle detected!" );
Serial.println ("Obstacle Details:");
Serial.print ("Distance From Robot is " );
Serial.print ( distance);
Serial.print ( " CM!");// print out the distance in centimeters.

Serial.println (" The obstacle is declared a threat due to close distance. ");
Serial.println (" Turning !");

motor.run(FORWARD);
for (i=0; i<255; i++) {
  servo1.write(i);
  motor.setSpeed(i);  
  stepper.step(1, FORWARD, INTERLEAVE);
  delay(3);
}

for (i=255; i!=0; i--) {
  servo1.write(i-255);
  motor.setSpeed(i);  
  stepper.step(1, BACKWARD, INTERLEAVE);
  delay(3);
}
}
else{
motor.run(BACKWARD);
for (i=0; i<255; i++) {
  servo1.write(i);
  motor.setSpeed(i);  
  delay(3);
  stepper.step(1, FORWARD, DOUBLE);
}

for (i=255; i!=0; i--) {
  servo1.write(i-255);
  motor.setSpeed(i);  
  stepper.step(1, BACKWARD, DOUBLE);
  delay(3);
}
}}

Should I modify/add something to the code? Or am I missing a component in the self driving robot?
Attached you can find an image of the self driving robot I have done.

Thanks,

Agustin V.

Please edit your post to add code tags (see "How to use this forum").

if (distance < 10)/*if there's an obstacle 25 centimers, ahead, do the following: */It’s great when comment and code concur.

Perhaps "10" is equal to "25 centimers"?

it doesn't transmit the information to the device for it to change direction.

What do you mean by this?

The program, not the sensor, must do whatever is required to change direction, and there are no comments in the code to suggest how that direction change is supposed to happen.

10 inches is about 25.4 centimeters, so perhaps a valid comment, ignoring the misspelling.
Or the distance was shortened for testing.