i made one for you ! :*
it uses a diferent libary ,, this one for the sensor ,,
you can change it to cm values but i prefer the normal ones ,, check it out ,,
this is the lib website
https://code.google.com/p/arduino-new-ping/
here is the code
//dexter gear control made 1-8-2014/
#include <Servo.h>
#include <NewPing.h>
#define TRIGGER_PIN 3 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 2 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 400
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
// set height here //
const int height =1200; // set your height here ,, i use digital value's they give better reading then the cm values ,,
int cm ; //incoming value of sensor
Servo gear; // make servo output to gear
int signal =0; // signal procces
int sensor =0;
void setup() {
Serial.begin(9600); // starting serial port baud rate 9600
delay (2000);
gear.attach(8); // servo conection out pin 8 to gear //
}
void loop() {
Serial.println(cm); // wil print digital values
Serial.println(sensor); //wil print cm
cm = sonar.ping();
sensor = cm/ US_ROUNDTRIP_CM;
if(height<cm){ // if sensor is higher height set
gear.writeMicroseconds(1800); // write system value to servo //
delay (35);
}
else if(height>cm){ // if sensor is lower height set
gear.writeMicroseconds(1000); // write system value to servo //
delay (35);
}
}