#include <Servo.h>
// Pin Definitions
const int servoPin = 18; // PWM output to the landing gear servo
const int pwmInputPin = 34; // PWM signal input from the flight controller
Servo landingGearServo; // Servo object for controlling landing gear
// PWM Input Configuration
int pwmInputValue = 0; // Value read from the flight controller's PWM signal
int pwmThreshold = 1500; // Threshold value for deploying/retracting gear
// Servo positions (adjust as per your landing gear's requirement)
int deployPosition = 180; // Servo position for deployed gear (in degrees)
int retractPosition = 0; // Servo position for retracted gear (in degrees)
void setup() {
// Initialize Serial Monitor (optional, for debugging)
Serial.begin(115200);
// Attach the servo to the corresponding GPIO pin
landingGearServo.attach(servoPin);
// Initialize the servo to the retracted position
landingGearServo.write(retractPosition);
// Set the PWM input pin as an input
pinMode(pwmInputPin, INPUT);
}
void loop() {
// Read PWM input from the flight controller (analogRead or digitalRead)
// For better accuracy, use pulseIn to measure the PWM pulse width
pwmInputValue = pulseIn(pwmInputPin, HIGH, 25000); // Measure pulse width (up to 25ms)
// Debugging info (optional)
Serial.print("PWM Input: ");
Serial.println(pwmInputValue);
// Check if PWM value is above the threshold to deploy landing gear
if (pwmInputValue > pwmThreshold) {
deployLandingGear();
}
else if (pwmInputValue < pwmThreshold) {
retractLandingGear();
}
// Small delay for stabilization
delay(20);
}
void deployLandingGear() {
// Move the servo to the deploy position
Serial.println("Deploying landing gear...");
landingGearServo.write(deployPosition);
}
void retractLandingGear() {
// Move the servo to the retract position
Serial.println("Retracting landing gear...");
landingGearServo.write(retractPosition);
}