Object avoiding car

So i am doing this object avoiding car project and for some reason it just doesn't avoid obstacles at all and i cant figure out why can someone please help.
The jest of the project is that i want the car to go right when its close to an object on the left and vice versa. i also want to display a stopwatch and distance traveled on my lcd screen
the servo is attached to the ultrasonic sensor to expand the area it covers i guess

here is the code i am using
`

[#include <Wire.h>
#include <LiquidCrystal_I2C.h>

#include <Servo.h>

//LCD
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

//Motor f/b
int forward = 9;
int backward = 10;

//motor L/R
int right = 5;
int left = 6;
int ena = 2;

Servo sensor;
int posi = 0;

#define trigPin 13
#define echoPin 12

void setup()
{
sensor.attach(4);
pinMode(forward, OUTPUT);
pinMode(backward, OUTPUT);

pinMode(right, OUTPUT);
pinMode(left, OUTPUT);
pinMode(ena,OUTPUT);

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

Serial.begin (9600);
lcd.begin(16, 2);

}

void gofor() {

digitalWrite(forward, HIGH);
digitalWrite(backward, LOW);
digitalWrite(right, LOW);
digitalWrite(left, LOW);
digitalWrite(ena, LOW);
}

void goback()
{
analogWrite(forward, 0);
analogWrite(backward, 127);
digitalWrite(right, LOW);
digitalWrite(left, LOW);
digitalWrite(ena, LOW);
}

void stopcar()
{
analogWrite(forward, 0);
analogWrite(backward, 0);
digitalWrite(right, LOW);
digitalWrite(left, LOW);
digitalWrite(ena, LOW);
}

void sharpl() {
digitalWrite(right, LOW);
digitalWrite(left, HIGH);
digitalWrite(ena, HIGH);
}

void sharpr() {
digitalWrite(right, HIGH);
digitalWrite(left, LOW);
digitalWrite(ena, HIGH);
}

void center() {
digitalWrite(right, LOW);
digitalWrite(left, LOW);
digitalWrite(ena, HIGH);
}

void sense()
{

long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
lcd.print(distance);
lcd.print(" m");

if (10 > distance && posi == (180 || 360) ) {
Serial.println("left!!");
sharpl();
delay(750);
}

else if(10 > distance && posi == (45 || 0)){
Serial.println(" RIGHT!!");
sharpr();
delay(750);
}
else{
Serial.print(distance);
Serial.println(" cm");
}

if(0 == distance){
goback();
delay(2000);
}
delay(500);

}

void servo() {
sensor.write(180);
posi = 180;
sense();
sensor.write(135);
posi = 135;
sense();
sensor.write(90);
posi = 90;
sense();
sensor.write(45);
posi = 45;
sense();
sensor.write(0);
posi = 0;
sense();
sensor.write(45);
posi = 45;
sense();
sensor.write(90);
posi = 90;
sense();
sensor.write(135);
posi = 135;
sense();
}

void pattern() {
center();
gofor();
delay(20);
servo();
}

void loop() {

pattern();
lcd.setCursor(1, 1);

lcd.print(millis() / 3600000);
lcd.print("h:");

lcd.print(millis() / 60000);
lcd.print("m:");

lcd.print(millis() / 1000);
lcd.print("s");

}]

everything i guess works except for the avoidance part and by testing different codes i am sure everything is functional im sure the problem is with the code.

Colonelpress:
the servo is attached to the ultrasonic sensor to expand the area it covers i guess

everything i guess works except ....

I'm hoping that's just the way you speak and not you actually guessing :wink:

Does this:

lcd.print(distance);
lcd.print(" m");

...show the correct values?

Yeah thats how i speak but don't you think that in theory it should work

and no it doesn't work as it should for some reason in displays the distance in the serial monitor (in cm of course) but it's just a place holder

Colonelpress:
displays the distance in the serial monitor

Is it the right value in the serial monitor though: I'm trying to establish if the sensor is working in the first place, with no guessing.

Yes the sensor is definitly working i have tried this code

const int pingPin = 7; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 6; // Echo Pin of Ultrasonic Sensor

void setup() {
   Serial.begin(9600); // Starting Serial Terminal
}

void loop() {
   long duration, inches, cm;
   pinMode(pingPin, OUTPUT);
   digitalWrite(pingPin, LOW);
   delayMicroseconds(2);
   digitalWrite(pingPin, HIGH);
   delayMicroseconds(10);
   digitalWrite(pingPin, LOW);
   pinMode(echoPin, INPUT);
   duration = pulseIn(echoPin, HIGH);
   inches = microsecondsToInches(duration);
   cm = microsecondsToCentimeters(duration);
   Serial.print(inches);
   Serial.print("in, ");
   Serial.print(cm);
   Serial.print("cm");
   Serial.println();
   delay(100);
}

long microsecondsToInches(long microseconds) {
   return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {
   return microseconds / 29 / 2;
}

and it works fine

i also added an if statement in the same code so that the right left motor moves at different distances and it worked completely fine

im sure the problem is in the code maybe the void sense() part im not sure how to fix it though

The servo will take a while to get into position. You instruct it to move and then immediately check for obstacles. You'll need some means of allowing it to get where it's going first.

Once you have, does the serial output correspond to what you think it should be? i.e. if you observe an obstacle on the right, do you see "left!!"?