# altitude pressure calculation per arduino function (till 80 km)

For everybody who's toying around with weather balloons...
Have fun!

``````/* *** Pressure altitude calculation ***
* Original equation by Steve Randall (http://randomaerospace.com):
* a [m] = ln(p0 [Pa] / p [Pa]) * 7238,3 [m]
* a [m]:Â   altitude;Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  unit: Meter
* p0 [Pa]: pressure at sea level;Â  Â  Â  Â  Â  Â   unit: Pascal
* p [Pa]:Â  pressure at altitude to calculate; unit: Pascal
* 7238,3:Â  correcture factor, found by Steve; unit: must be in Meter to do it mathematically correct.
* In this sketch equation is adapted to Arduino's language.
* You always have to use long-variables because integer will fail at altitudes above 32.768 km resp. pressure less than 1096 Pa.
* Suggested pressure sensor: MS5607 ... MS5611 Series
*/

long pressureInPascal;
long druckhoehe;

void setup() {
Â  Serial.begin(9600);
Â  delay(2000);
Â  Serial.println("Demonstration der Druckhoehenkalkulation.\nDie hier verwendete Gleichung ist bis 80 km gueltig.");
}

void loop() {
Â  Serial.println("Eingabe (Druck in Pascal):");
Â  pressureInPascal = longInput();
Â  druckhoehe = calcPressureAltitude(pressureInPascal);
Â  Serial.println("Der Druck von " + String(pressureInPascal) + " Pa entspricht einer Hoehe von " + String(druckhoehe) + " Metern.");
}

// Function calculation of pressure altitude
long calcPressureAltitude(long _PressureInPascal) {
Â  long _Temp = (log(101325.0 / _PressureInPascal) * 72383.0);
Â  long _PressureAltitude = 0;
Â  if (_Temp % 10 > 5) {
Â  Â  _PressureAltitude = 1;
Â  }
Â  _PressureAltitude = _PressureAltitude + long(_Temp / 10);
Â  return _PressureAltitude;
}

// input function
long longInput() {
Â  long _Result;
Â  char _Space = ' ';Â
Â  while (Serial.available() == 0); {
Â  Â  delay(3);
Â  Â  _Result = Serial.parseFloat();
Â  Â  while (Serial.available() > 0) {
Â  Â  Â  _Space = Serial.read();
Â  Â  };
Â  };
Â  return _Result;
};
``````
``````Serial.println("Demonstration der Druckhoehenkalkulation.\nDie hier verwendete Gleichung ist bis 80 km gueltig.");
``````

sp.`Serial.println(F("Demonstration der Druckhoehenkalkulation.\nDie hier verwendete Gleichung ist bis 80 km gueltig."));`

Sorry, I already have seen it too.

Okay, translated...

``````/* *** Pressure altitude calculation ***
* Original equation by Steve Randall (http://randomaerospace.com):
* a [m] = ln(p0 [Pa] / p [Pa]) * 7238,3 [m]
* a [m]:Â   altitude;Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  unit: Meter
* p0 [Pa]: pressure at sea level;Â  Â  Â  Â  Â  Â   unit: Pascal
* p [Pa]:Â  pressure at altitude to calculate; unit: Pascal
* 7238,3:Â  correcture factor, found by Steve; unit: must be in Meter to do it mathematically correct.
* In this sketch equation is adapted to Arduino's language.
* You always have to use long-variables because integer will fail at altitudes above 32.768 km resp. pressure less than 1096 Pa.
*/

long pressureInPascal;
long altitudePressure;

void setup() {
Â  Serial.begin(9600);
Â  delay(2000);
Â  Serial.println("Demonstration of pressure altitude calkulation.\nEquation used here is valid until 80 km.");
}

void loop() {
Â  Serial.println("Input (Pressure in Pascal):");
Â  pressureInPascal = longInput();
Â  altitudePressure = calcPressureAltitude(pressureInPascal);
Â  Serial.println("A pressure of " + String(pressureInPascal) + " Pa equals to an altitude of " + String(altitudePressure) + " Meters.");
}

// Function calculation of pressure altitude
long calcPressureAltitude(long _PressureInPascal) {
Â  long _Temp = (log(101325.0 / _PressureInPascal) * 72383.0);
Â  long _PressureAltitude = 0;
Â  if (_Temp % 10 > 5) {
Â  Â  _PressureAltitude = 1;
Â  }
Â  _PressureAltitude = _PressureAltitude + long(_Temp / 10);
Â  return _PressureAltitude;
}

// input function
long longInput() {
Â  long _Result;
Â  char _Space = ' ';Â
Â  while (Serial.available() == 0); {
Â  Â  delay(3);
Â  Â  _Result = Serial.parseFloat();
Â  Â  while (Serial.available() > 0) {
Â  Â  Â  _Space = Serial.read();
Â  Â  };
Â  };
Â  return _Result;
};
``````

No, you missed my RAM-saving addition

Sorry I'm just a beginner.
What means that "F(blabla)"?

Thank you AWOL.

Final Version: Feel free to modify/optimize...

``````/* *** Pressure altitude calculation ***
* Original equation by Steve Randall (http://randomaerospace.com):
* a [m] = ln(p0 [Pa] / p [Pa]) * 7238,3 [m]
* a [m]:Â   altitude;Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  unit: Meter
* p0 [Pa]: pressure at sea level;Â  Â  Â  Â  Â  Â   unit: Pascal
* p [Pa]:Â  pressure at altitude to calculate; unit: Pascal
* 7238,3:Â  correcture factor, found by Steve; unit: must be in Meter to do it mathematically correct.
* In this sketch equation is adapted to Arduino's language.
* You always have to use long-variables because integer will fail at altitudes above 32.768 km resp. pressure less than 1096 Pa.
* Suggested pressure sensor: MS5607 ... MS5611 Series.
*/

long pressureInPascal;
long altitudePressure;

void setup() {
Â  Serial.begin(9600);
Â  delay(2000);
Â  Serial.println(F("Demonstration of pressure altitude calkulation.\nEquation used here is valid until 80 km."));
}

void loop() {
Â  Serial.println(F("Input (Pressure in Pascal):"));
Â  pressureInPascal = longInput();
Â  altitudePressure = calcPressureAltitude(pressureInPascal);
Â  Serial.println("A pressure of " + String(pressureInPascal) + " Pa equals to an altitude of " + String(altitudePressure) + " Meters.");
}

// Function calculation of pressure altitude
long calcPressureAltitude(long _PressureInPascal) {
Â  long _Temp = (log(101325.0 / _PressureInPascal) * 72383.0);
Â  long _PressureAltitude = 0;
Â  if (_Temp % 10 > 5) {
Â  Â  _PressureAltitude = 1;
Â  }
Â  _PressureAltitude = _PressureAltitude + long(_Temp / 10);
Â  return _PressureAltitude;
}

// input function
long longInput() {
Â  long _Result;
Â  char _Space = ' ';Â
Â  while (Serial.available() == 0); {
Â  Â  delay(3);
Â  Â  _Result = Serial.parseFloat();
Â  Â  while (Serial.available() > 0) {
Â  Â  Â  _Space = Serial.read();
Â  Â  };
Â  };
Â  return _Result;
};
``````