Tak Artur, to ogólnie działa, tylko że na serwie , a mechanizm podwozia ni hu hu
ED
Adrian się zlitował nade mną i zmienił w programie ustawianie pozycji serwa z kątowego na milisekundowe tak po chłopsku mówiąc i zaczęło działać
, poniżej zamieszczam drugi kod, tamten który zrobił Hubert też jest ok bo można go zastosować do serw, co też może się komuś przydać, np do świnki
natomiast kod Adriana jest typowo on/off i taki właśnie potrzebowałem.
Chyba ogórków braknie
/*
Written by ediblefly 2015
Inspired by Benbojangles 2015
Written for Tarot TL65B44 copter landing gear
to set trigger altitude change "int trigger_altitude" to your desired alt itude in cm
The circuit:
*HC-SR04 Vcc connection attached to +5V Arduino
*HC-SR04 GND connection attached to gnd Arduino
*HC-SR04 Trig connection attached to pin 7
*HC-SR04 Echo connection attached to pin 8
*Sonar to APM connection is not active and will be developed in future versions of
the script
This example code is in the public domain.
*/
#include <Servo.h>
#include <Average.h>
#define CNT 10 //defines the size of the floating array used for averaging
//bigger the size: better averaging, very big arrays will cause long delays at the startup
Average<long> sonarRead(CNT);
Servo myservo; // create servo object to control a servo
const int pos1 = 1000; // variable to store the servo position
const int pos2 =1500; // not used
const int pos3 = 2000; // variable to store the servo position
//data Pin number of the Arduino:
const int pingPin = 7; //trigger pin for HC-SR04 sonar unit
const int echoPin = 8; //echo pin for HC-SR04 sonar unit
const int sonarToAPM = 3; //reserved for future use
int ledPin = 13; //LED connected to digital pin 13 doubled by the onboard LED
int LED_1 = 2; //new LED strip no 1
int LED_2 = 3; //new LED strip no 2
int LED_3 = 4; //new LED strip no 3
int LED_4 = 5; //new LED strip no 4
int trigger_altitude = 19; //here we define the height in cm that we want to trigger
boolean IsGearDown = false;
int countLand = 0;
int countTakeOff = 0;
int attemptsL = 2; //change for acceptable number of falsepositives on landing
int attemptsTO = 4; //change for acceptable number of falsepositives on TakeOff
long CutOffDist = 1000; //cutoff dostance, this is usefull to cut off extreme values
//additional mechanism of averaging
void setup() {
// initialize serial communication:
Serial.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(ledPin, OUTPUT); // sets the digital pin as output
pinMode(LED_1, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(pingPin, OUTPUT);
pinMode(sonarToAPM, OUTPUT);
}
void loop()
{
//distance in centimeters:
long duration, cm;
// The sensor is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
//fill the array for data averaging
int i;
//for(i=0; i<CNT; i++)
//{
// The same pin is used to read the signal from the sensor: a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
duration = pulseIn(echoPin, HIGH);
// convert the time into a distance
cm = microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.print("cm-real");
Serial.println();
if (cm < trigger_altitude)
{
sonarRead.push(1);
}
else
{
sonarRead.push(0);
}
delay(10);
//}
cm = sonarRead.mode();
//Serial.println(sonarRead.mode()); //mode(sonarRead, CNT);
Serial.print(CNT);
Serial.print("");
Serial.println();
Serial.print(cm);
Serial.print("");
Serial.println();
if (cm == 1)
{
digitalWrite(ledPin, HIGH); // sets the LED on
if (countLand < attemptsL)
{
countLand ++;
}
else if (IsGearDown == false)
{
myservo.writeMicroseconds(pos1);
countLand = 0;
countTakeOff = 0;
IsGearDown = true; //Gear is Out - Landing
}
delay(15);
}
else if (cm == 0)
{
digitalWrite(ledPin, LOW);
digitalWrite(LED_1, millis()>>9 &1); // for circa about 2Hz blinking
digitalWrite(LED_2, millis()>>9 &1);
digitalWrite(LED_3, millis()>>9 &1);
digitalWrite(LED_4, millis()>>9 &1);
if (countTakeOff < attemptsTO)
{
countTakeOff ++;
}
else if (IsGearDown == true)
{
myservo.writeMicroseconds(pos3);
countTakeOff = 0;
countLand = 0;
delay(3000);
IsGearDown = false; //Gear is retracted - in the air
}
delay(15);
}
delay(15);
}
long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the sensor, there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See:
http://www.parallax.com/dl/docs/prod/ac ... G-v1.3.pdf
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}