jremington:
Simple code to calculate altitude in feet or meters from local temperature in Kelvin and pressure in Pa. Calibration function included
//globals
volatile unsigned long int pressure; //pressure reading in Pa from SCP1000
volatile signed int temperature; //temperature in degrees C*10 from SCP1000
char buffer[13]; //temporary formatting buffer
signed long P0=101325; //standard atmosphere in Pa, updated when altitude is set
float alt_m, dTdH=0.0065, T0=288.15; //Assume 15 degrees Celcius; 0 C = 273.15 K
signed long tav,pav,t,p; //temp variables, temperature and pressure
signed long alt_f=0,new_alt; //altitude in feet
uint8_t i,key; //loop variables
double fact;
// main loop
while(1) {
tav=0; pav=0; key=0;
// loop to average pressure and temperature data
for(i=0; i<SAMPLES; i++) {
scp_read(); //read pressure and temperature from SCP1000
tav+=temperature;
pav+=pressure;
}
//
// user input to change altitude? ...key pressed?
// Note that GetNum returns only integer values
//
if(key && (p>0)) { //but only if we already have t & p
new_alt = alt_f; //save old altitude
new_alt = GetNum(new_alt); //display current altitude and input new altitude from user
// calculate P0 from new altitude
// dTdH and T0 are kept as variables, in case user implements temperature correction
alt_m= new_alt/METERS_TO_FEET; //back to meters, if necessary
fact = 1.0 - alt_m*dTdH/T0;
fact = pow(fact,5.256);
P0 = ((double) p)/fact + 0.5;
}
else { //display measured altitude
t=(tav+SAMPLES/2)/SAMPLES; //round and average temperature measurements
p=(pav+SAMPLES/2)/SAMPLES; //round and average pressure measurements
print_decimal(t, 1, 2); //display temperature
LCD_puts(buffer); //on LCD
//
// calculate current altitude from standard atmosphere model (see application
// note #33 from VTI): "SCP1000 Pressure Sensor as Barometer and Altimeter"
// dTdH and T0 are kept as variables, in case user implements temperature correction
//
fact= 1.0 - pow(((double) p)/((double) P0),0.19026);
alt_m = (T0/dTdH)fact;
alt_f = alt_mMETERS_TO_FEET + 0.5; //convert units
print_decimal(alt_f, 0,1);
LCD_puts(buffer); //print derived altitude
} //end if else
} //end while (1)
This is for the SCP1000 not the BMP180 sensor, so this makes it....