Using BMP280 sensor to control servos. Need some guidance with coding

Hi @mikami2020

You just need to sample the altitude at your launch site and subtract this from your altitude measurements when the rocket is launched.

To represent the various phases of rocket flight in code, the concept of the state machine might come in handy:

//
// State machine example
//
enum StateMachine { DISARMED,
                    ARMED,
                    LAUNCH,
                    DEPLOY,
                    ABORT } stateMachine = DISARMED;

bool armedInput = false, lauchInput = false, abortInput = false; 
bool servosActivated = false;
float altitude, launchSiteAltitude;
float threshold = 500.0f;   // Altitude above launch site in meters

void setup() 
{  
  initialiseBarometer();
  initialiseServos();
}

void loop() 
{
  switch (stateMachine)
  {
    case DISARMED:
      armedInput = readArmedInput();
      if (armedInput == true)
      {
        stateMachine = ARMED;
      }
      break;
    case ARMED:
      launchInput = readLaunchInput();
      launchSiteAltitude = readBarometerAltitude();
      if (launchInput == true)
      {        
        stateMachine == LAUNCH;
      }
      break;
    case LAUNCH:
      altitude = readBarometerAltitude() - launchSiteAltitude;
      if (altitude > threshold)
      {
        stateMachine = DEPLOY;
      }
      break;
    case DEPLOY:
      if (servosActivated == false)
      {
        activateServos();
        servosActivated = true;
      }
      break;
    case ABORT:
      // Add abort code here...
      break;
    default:
      break;
  }
}