#include <Wire.h>
#include <MPU6050.h>
#include <Servo.h>

MPU6050 mpu;
Servo parachuteServo;

const int servoPin = 9; // Pin connected to the servo

void setup() {
  Serial.begin(9600);
  Wire.begin();
  mpu.initialize();
  
  parachuteServo.attach(servoPin); // Attach servo to the specified pin
  
  // Uncomment the following lines to calibrate the sensor
  // mpu.calibrateAccel();
  // mpu.calibrateGyro();
  // mpu.printCalibrationResults();
}

void loop() {
  float ax, ay, az;
  float gx, gy, gz;
  
  mpu.getAcceleration(&ax, &ay, &az);
  mpu.getRotation(&gx, &gy, &gz);

  // Calculate total acceleration (magnitude)
  float totalAcceleration = sqrt(ax * ax + ay * ay + az * az);

  // Check for free fall condition based on accelerometer and gyroscope data
  if (totalAcceleration < 1.2 && (abs(gx) > 300 || abs(gy) > 300 || abs(gz) > 300)) {
    Serial.println("Free fall detected! Deploying parachute.");
    deployParachute();
    delay(1000); // Add a delay to keep the parachute deployed for a certain duration
    retractParachute();
  }

  Serial.print("Total Accel: ");
  Serial.println(totalAcceleration);
  Serial.print("Gyro X: ");
  Serial.print(gx);
  Serial.print(", Y: ");
  Serial.print(gy);
  Serial.print(", Z: ");
  Serial.println(gz);

  delay(100); // Adjust delay according to your requirement
}

void deployParachute() {
  parachuteServo.write(90); // Rotate the servo to deploy the parachute
}

void retractParachute() {
  parachuteServo.write(0); // Retract the parachute by rotating the servo to initial position
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
servo1:GND
servo1:V+
servo1:PWM
imu1:INT
imu1:AD0
imu1:XCL
imu1:XDA
imu1:SDA
imu1:SCL
imu1:GND
imu1:VCC