Altitude Calculations

My attitude is a little low, and it changes to much depending on pressure, is it because I am inside, or is the code wrong?

#include <Wire.h>    //Allows for IC2 communication
#include <MS5xxx.h>  //Allows for easy setup of altimeter
#include <math.h>
MS5xxx sensor(&Wire);  //Not really sure

float rawTemp = 0;     //Stores raw temp data
float pressurePascal =0;  //Stores raw pressure data in pascals

float tempF = 0;          //Stores temp in fahrenheit
float tempC = 0;
float tempK = 0;          //Stores temp in Kelvin
float pressureInch = 0;   //Stores pressure in Inches of mercury
float pressureTorr = 0;   //Stores pressure in torr
float pressureMg = 0;     //Stores pressure in inches mercury
float pressureLast = 0;   //Stores previous pressure in Torr

//Altitude calculation variables
float altitude = 0;
float stdPressure = 101320.75;
float varA = 0.19022256;
float varB = .0065;
float varC = .5555555;
float varPressure = stdPressure;
double Variable1 = 0;
float Variable2 = 0;
double test1 = 0;
double test2 = 0;

//SETUP LOOP/////////////////////////////
void setup() {
  //Begin serial communication
  Serial.begin(57600);
  //Connect sensor
  sensor.connect();
  }
//////////////////////////////////////////

//MAIN LOOP////////////////////////////////////////////////////////////
void loop() {
  // Read sensor data
  sensor.ReadProm();
  sensor.Readout();

  //Call temp and pressure functions
  tempData();     //Temp
  pressureData(); //Pressure
  altitudeCalc(); //Altitude

  //Print Data
  printData();
}
//////////////////////////////////////////////////////////////////////////

//Perform temp calculations//////////
void tempData(){
  rawTemp = sensor.GetTemp();

  //Convert to degrees fahrenheit
  tempF = (rawTemp*.01)*1.8+32; 

  //Convert to degrees Celcius
  tempC = (tempF-32)*varC;

  //convert to kelvin
  tempK = tempC + 273.15;
}

//Perform pressure calculations//////////
void pressureData(){
  //Store last pressure
  pressureLast = pressureTorr;

  //Read new data
  pressurePascal = sensor.GetPres();

  //Convert pressure into torr
  pressureTorr = pressurePascal*0.00750061683;
  //Convert pressure into inches mercury;
  pressureMg = pressurePascal*0.000295299830714;

  if(pressureLast < pressureTorr)
  {
    Serial.println("Altitute is increasing");
  }
  else
  {
    Serial.println("Altitute is decreasing");
  }
}

//Altitude calculations
void altitudeCalc(){
    test1 = (stdPressure/pressurePascal);
    test2 = pow(test1,varA);
    Variable1 = abs(test2 - 1);
    Variable2 = Variable1*tempK;
    altitude = Variable2/.0065;
}
float stdPressure = 101320.75;

The local altitude is calculated assuming some sea level pressure (see above), which can change hourly.

Barometers used as altimeters must be recalibrated frequently, which is often done by calculating the sea level pressure that gives the correct altitude.

Also, be aware that airports and most weather reports report the sea level equivalent of the local air pressure.

it changes to much depending on pressure

How do you know? The reported altitude can change by hundreds of meters, as the air pressure changes.

Weather systems will change the air pressure even if you stay at the same altitude. The pressure also goes up and down twice daily due to the atmosphere being heated and cooled as the Earth rotates.

I am pretty sure NASA published (on the web) data from radar scans giving accurate altitudes for latitudes and longitudes over most of the globe.

I really phrased the question poorly, I did know about pressure an attitude I just couldn't think how to work it into the equation, I feel really dumb right now.

Using the meter for my nearest airport should fix it up.

Thanks both of you! much appreciated.

Only if that nearest airport is no more than about 10 km from where you are. Otherwise try to find closer values. Pressure

The nearest airport is barely 25 km from where I am (in a straight line). Easily 1-2 mbar pressure difference. Currently for me sea level pressure at the nearest station is 1013.8 mbar, the airport is at 1014.3 mbar, and in the 40 km around me it varies between 1013.5 and 1014.8, with three stations just 5 km away in different directions at 1014.6-1014.7 mbar. My BMP280 sensors give 1012.8 mbar.

jremington:

float stdPressure = 101320.75;

The local altitude is calculated assuming some sea level pressure (see above), which can change hourly.

Barometers used as altimeters must be recalibrated frequently, which is often done by calculating the sea level pressure that gives the correct altitude.

Also, be aware that airports and most weather reports report the sea level equivalent of the local air pressure.
How do you know? The reported altitude can change by hundreds of meters, as the air pressure changes.

And the electronic sensors are non-linear. There should be a note in the data sheet giving the corrections.

I have a customer that makes aircraft instruments and uses an electronic barometer to compute altitude. They use a look-up table to apply the corrections.

Paul