i use this for also a quadcopter , lol but different way ![]()
http://forum.arduino.cc/index.php?topic=257823
this scipt wil do nothing when 0 input
//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 (sensor =0)return;
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);
}
}
}