Programming an Autopilot with Arduino
In this project we are required to program a scenario in which a helicopter pivoted to a potentiometer is being controlled by Arduino and motor driver module L298N. There are some other elements like the height limit switch and a light sensor to help out the flight control. The use is required to follow the following flow in to achieve the required functionality.

Circuit Diagram
The following circuit diagram is used to program the scenario.

Arduino Code
Following is the code for this project
// This code is a sketch for controlling a device such as a drone or similar airborne apparatus to achieve a target height using altitude sensors and an autopilot system.
// Define integer variables to represent pin numbers and other parameters
int enA = 9; // Motor driver enable pin
int in1 = 6; // Motor driver control pin 1
int in2 = 10; // Motor driver control pin 2
int Lt_Switch = 7; // Limit switch pin
unsigned long Time = 0; // Variable to hold elapsed time
double currentMillis; // Variable to hold current time in milliseconds
unsigned long previousMillis = 0; // Variable to hold the previous time in milliseconds
float Target_Height = 0.2; // Target height in meters
float Take_off_Height = 0; // Variable to hold the take-off height
int LDR_Status; // Variable to hold the status of the light-dependent resistor (LDR)
int If_Status = 0; // Status flag for tracking if conditions
int Limit_Switch; // Variable to hold the status of the limit switch
float Vmax = 748; // Maximum voltage reading from altitude sensor
float M = 0; // Slope of the calibration curve
float Theta = 38; // Angle of elevation
float C = 0; // Intercept of the calibration curve
int Duty_Cycle = 51; // Duty cycle for motor speed control (20%)
// Setup function runs once when the microcontroller starts
void setup() {
Serial.begin(9600); // Initialize serial communication
pinMode(enA, OUTPUT); // Set motor enable pin as output
pinMode(in1, OUTPUT); // Set motor control pin 1 as output
pinMode(in2, OUTPUT); // Set motor control pin 2 as output
pinMode(2, OUTPUT); // Set output pin for LED 1
pinMode(3, OUTPUT); // Set output pin for LED 2
pinMode(4, OUTPUT); // Set output pin for LED 3
pinMode(5, OUTPUT); // Set output pin for LED 4
pinMode(Lt_Switch, INPUT); // Set limit switch pin as input
digitalWrite(in1, LOW); // Set initial state for motor control pin 1
digitalWrite(in2, LOW); // Set initial state for motor control pin 2
previousMillis = millis(); // Record the initial time
}
// Loop function continuously executes the main program logic
void loop() {
//Get_Height(); // Commented out; function not currently used
Take_off(); // Execute the take-off procedure
}
// Function to obtain target height from user input (not currently used)
void Get_Height() {
Serial.print("Please Enter Target Height: ");
for (;;) {
if (Serial.available() > 0) {
Target_Height = Serial.read();
if (Target_Height > 0 && Target_Height < 10)
break;
}
}
}
