ultrasonic sencor give wrong distances . I used      the inbuilt basic example of measuring the distance to test my ultrasonic sensor. the object is kept at 10 - 15 cm. form the ultrasonic sensor but still the readings show 135 cm only  

#include &lt;Ultrasonic.h&gt;
int trigPin = 5;    
int echoPin = 6; 
#include &lt;Servo.h&gt;   //servo library
Servo servo;        
int servoPin = 7;
long duration, dist, average;   
long aver[3];   //array for average


void setup() {       
    Serial.begin(9600);
    servo.attach(servoPin);  
    pinMode(trigPin, OUTPUT);  
    pinMode(echoPin, INPUT);  
    servo.write(0);         //close cap on power on
    delay(100);
    servo.detach(); 
} 

void measure() {  
 digitalWrite(100,HIGH);
digitalWrite(trigPin, LOW);
delayMicroseconds(100);
digitalWrite(trigPin, HIGH);
delayMicroseconds(150);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
dist = (duration/2.0) / 29.1;    //obtain distance
}
void loop() { 
  for (int i=0;i&lt;=2;i++) {   //average distance
    measure();               
   aver[i]=dist;            
    delay(100);              //delay between measurements
  }
 dist=(aver[0]+aver[1]+aver[2])/3;    

if ( dist&lt;15 ) {
//Change distance as per your need
 servo.attach(servoPin);
  delay(1);
 servo.write(0);  
 delay(2500);       
 servo.write(100);    
 delay(500);
 servo.detach();      
}
Serial.print(dist);
}




#include &lt;Ultrasonic.h&gt;
int trigPin = 5;    
int echoPin = 6; 
#include &lt;Servo.h&gt;   //servo library
Servo servo;        
int servoPin = 7;
long duration, dist, average;   
long aver[3];   //array for average

void setup() {       
    Serial.begin(9600);
    servo.attach(servoPin);  
    pinMode(trigPin, OUTPUT);  
    pinMode(echoPin, INPUT);  
    servo.write(0);         //close cap on power on
    delay(100);
    servo.detach(); 
} 

void measure() {  
    digitalWrite(100,HIGH);
    digitalWrite(trigPin, LOW);
    delayMicroseconds(100);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(150);
    digitalWrite(trigPin, LOW);
    pinMode(echoPin, INPUT);
    duration = pulseIn(echoPin, HIGH);
    dist = (duration/2.0) / 29.1;    //obtain distance
}

void loop() { 
  for (int i=0;i&lt;=2;i++) {   //average distance
    measure();               
   aver[i]=dist;            
    delay(100);              //delay between measurements
  }
  dist=(aver[0]+aver[1]+aver[2])/3;    

  if ( dist&lt;15 ) {
    //Change distance as per your need
    servo.attach(servoPin);
    delay(1);
    servo.write(0);  
    delay(2500);       
    servo.write(100);    
    delay(500);
    servo.detach();      
  }
  Serial.print(dist);
}
You have a lot of redundant code:


include Ultrasonic.h is unnecessary.
servo.attach and servo.detach without reusing pin is unnecessary
The ultrasonic code is not correct.
It should be:


    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);

    // measure duration of pulse from ECHO pin
    duration_us = pulseIn(echoPin, HIGH);

    // calculate the distance
    distance_cm = 0.017 * duration_us;


Refer to the Ultrasonic sensor tutorial and Servo motor tutorial