Erdin and Pito...
there are two small "problems".
I'm writing down a complete personalized and different library, right now, (thank you all again!
) but...
the Adafruit job is not optimal, instantiating the RawPressure function three times instead of instantiate it only once for each measure taken, and the SparkFun has a small bug on the compensation function that gives wrong final pressure information (of some 4 mB).
The Adafuit library instantiates the function readRawPressure each time ReadPressure is called, and it means that it is called 3 times instead of one, the first for the pressure calculation, the second and the third for the altitude calculation. it is not needed at all, just think that each of them take 8 samples... moreover, the pressure taken for the three measures are not the same (of course). The difference is minimal, but... it is not correct, talking in terms of optimization, in any case.
To solve that problem it is just needed to instantiate the object just once, the prototypes of all of them (in the .h file) have to be changed, and their calls too:
the appropriate section of the .h file has to be changed in:
class Adafruit_BMP085 {
public:
Adafruit_BMP085();
boolean begin(uint8_t mode = BMP085_ULTRAHIGHRES); // by default go highres
float readTemperature(uint16_t);
int32_t readPressure(float, uint32_t);
float readAltitude(uint32_t, float sealevelPressure = 101325); // std atmosphere
uint16_t readRawTemperature(void);
uint32_t readRawPressure(void);
...CUT
the "main" has to be changed in:
Adafruit_BMP085 bmp;
int MyAltitude = 445;
float Temperature =0;
uint32_t Rpressure=0;
uint16_t RTemperature=0;
int32_t CPressure=0;
float p0=0;
void setup() {
Serial.begin(115200);
if (!bmp.begin()) {
Serial.println("Could not find a valid BMP085 sensor, check wiring!");
while (1) {}
}
}
void loop() {
Serial.println("C++ version, Adafruit");
Serial.print("Temperature = ");
RTemperature = bmp.readRawTemperature();
Serial.print(bmp.readTemperature(RTemperature));
Serial.println(" *C");
Rpressure=bmp.readRawPressure();
CPressure = bmp.readPressure(RTemperature, Rpressure);
p0 = CPressure/pow((1.0 - ( MyAltitude/44330.0 )), 5.255);
Serial.print("Pressure = ");
Serial.print(p0/100);
// Serial.print(bmp.readPressure());
Serial.println(" mB");
// Calculate altitude assuming 'standard' barometric
// pressure of 1013.25 millibar = 101325 Pascal
Serial.print("Altitude = ");
Serial.print(bmp.readAltitude(CPressure));
Serial.println(" meters");
// you can get a more precise measurement of altitude
// if you know the current sea level pressure which will
// vary with weather and such. If it is 1015 millibars
// that is equal to 101500 Pascals.
Serial.print("Real altitude = ");
Serial.print(bmp.readAltitude(CPressure,101500));
Serial.println(" meters");
...CUT
and of course the header of each function has to be changed in the .cpp file, deleting the further
declaration of each variable passed to the function, in order to avoid the usual "declaration of 'XXXXXX' shadows a parameter" error.
// readPressure
int32_t Adafruit_BMP085::readPressure(float UT, uint32_t UP) {
int32_t B3, B5, B6, X1, X2, X3, p;
uint32_t B4, B7;
// UT = readRawTemperature();
// UP = readRawPressure();
...CUT
****************************************************************************
// readTemperature
float Adafruit_BMP085::readTemperature(uint16_t UT) {
int32_t X1, X2, B5; // following ds convention
float temp;
...CUT
****************************************************************************
float Adafruit_BMP085::readAltitude(uint32_t RPressure, float sealevelPressure) {
float altitude;
altitude = 44330 * (1.0 - pow(RPressure /sealevelPressure,0.1903));
return altitude;
}
****************************************************************************