Obstacle avoiding robot servo random

Hello everyone, I am making an obstacle avoiding robot that can detect obstacles and then turn a servo to look left and right and turn according to which side has more space. My code is down below and every time i try to turn the servo only if the distance is less than 18cm, the servo turns even if the distance is over 18cm's.

(just to be clear i want a robot that can detect an obstacle in front of it and stop about 20cm away then look left and right and turn left or right to whatever side has more space and it can turn for a random set time(from 500ms to 1400ms). Thank you!

#include<AFMotor.h> ///ALWAYS keep this at the top when using motors in program
#include<Servo.h>
#include<NewPing.h>
int trigPin = A4;
int echoPin = A5;
#define TRIGGER_PIN A4 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN A5 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 250 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 250cm.

Servo servo_motor; // Servo's name
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
long duration, distance;//the ultrasonic sensor measures in cm

Servo servo;

AF_DCMotor motor2(1); //the 1 in perenthesis is the port on the motor sheild its plugged into
AF_DCMotor motor4(4);//the 4 in perenthesis is the port on the motor sheild its plugged into

int pos = 90;

void setup(){
pinMode(trigPin, OUTPUT); // set trig pin as output
pinMode(echoPin, INPUT); //set echo pin as input to capture reflected waves

motor2.setSpeed(255); //set motors speed and this will also arm the motors and prepare it for driving
motor4.setSpeed(255);
//speed ranges from 0 to 255

servo.attach(10);///attach servo to top servo port on motor sheild(port closest to edge)
servo.write(90);
}

void loop() {
int distanceRight =0;
int distanceLeft =0;
int distance =0;
delay(100);

digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH); // send waves for 10 us
delayMicroseconds(10);
duration = pulseIn(echoPin, HIGH); // receive reflected waves
distance = duration / 58.2; // convert to distance
delay(10);
// If you don't get proper movements of your robot then alter the pin numbers

if (distance > 19){
motor2.run(FORWARD);
motor4.run(FORWARD);
}
if(distance < 18){
motor2.run(RELEASE);
motor4.run(RELEASE);
delay(500);
}

}///KEEP THIS ALWAYS AT THE END-MAKE SURE THERE ARE

If you know the answer to my problem, please help me. Thank you!

there is an option for uploading code in the fourm.....plz use that

No need to attach such a small amount of code, just use code tags.

@roboman1693330

TOPIC SPLIT
DO NOT HIJACK / NECRO POST !

Could you take a few moments to Learn How To Use The Forum.
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

I can't see anything to help with. There is nothing in your code that ever tries to turn a servo even though you have two Servo objects called servo_motor and servo.

And why have you included NewPing if you never use it?

Steve