Servo and Ultrasonic sensor

hi, ? will make robot using servo and ultrasonic sensor. then I had write a simple code using servo with sensor.
I want from program if distance under 5 cm servo turning 90 degree. please help me.thank you
where I do wrong?

#define trigPin 12
#define echoPin 13
#include <Servo.h>

Servo myservo;
 
void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  myservo.attach(9);
   
}

void loop() {
  int duration, distance,pos=0;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance<=5){
   myservo.write(90);
    delay(100);
  }
  else {
   
    Serial.print(distance);
    Serial.println(" cm");
  }
  delay(500);
}

Moderator edit: Code tags.

I think the default servo position is 90-degrees so moving it to 90-degrees won't do anything.

Is there a different position you'd like the servo to go to when the distance is > 5?

If you are using the HC-SR04 the 1000 microsecond delay is a bit long.

The specification doc says it should 10 microseconds (delayMicroseconds(10)).
Here is the doc:
http://jaktek.com/wp-content/uploads/2011/12/HC-SR04.pdf

Also you should specifically hold the trigger line LOW first for a little bit of time before setting it HIGH (just like the PING sensor).

Try the following code:

void loop() {
  int duration, distance,pos=0;
  digitalWrite(trigPin, LOW);  
  delayMicroseconds(2); 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  .....

Uncompiled, untested

#define trigPin 12
#define echoPin 13
#define servoPin 9
#define DEFAULT_SERVO_POS    0
#define TRIGGERED_SERVO_POS 90
#include <Servo.h>

Servo myservo;
 
void setup() 
{
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  pinMode(echoPin, INPUT);
  myservo.write(DEFAULT_SERVO_POS);
  myservo.attach(servoPin);
}

void loop() 
{
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  long duration = pulseIn(echoPin, HIGH);
  long distance = (duration/2) / 29.1;
  Serial.print(distance);
  Serial.println(" cm");
  if (distance<=5){
    myservo.write(TRIGGERED_SERVO_POS);
  }
  delay(500);
}

thank you for helping me.
? will use this code

#define trigPin 12
#define echoPin 13
#include <Servo.h>

Servo myservo;

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
myservo.attach(9);

}

void loop() {
int duration, distance,pos=0,i;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
Serial.print(distance);
Serial.println(" cm");
if(distance<5)
{
myservo.write(180);
}
else{
myservo.write(0);
}

delay(100);
}