67 lines
1.6 KiB
C++
67 lines
1.6 KiB
C++
|
#include "Arduino.h"
|
||
|
#include "barometer.h"
|
||
|
|
||
|
Adafruit_BMP280 bmp; // I2C
|
||
|
|
||
|
|
||
|
float barometer_temp;
|
||
|
float barometer_pressure;
|
||
|
float barometer_alt;
|
||
|
|
||
|
float barometer_alt_calibrationpressure=0; //example: 1002.45
|
||
|
bool barometer_alt_calibrated=false;
|
||
|
|
||
|
void initBarometer() {
|
||
|
unsigned status;
|
||
|
Wire.begin(25,24);
|
||
|
status = bmp.begin(BMP280_ADDRESS_ALT, BMP280_CHIPID);
|
||
|
|
||
|
//status = bmp.begin();
|
||
|
if (!status) {
|
||
|
Serial.println(F("Could not find a valid BMP280 sensor"));
|
||
|
}
|
||
|
|
||
|
/* Default settings from datasheet. */
|
||
|
bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */
|
||
|
Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */
|
||
|
Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */
|
||
|
Adafruit_BMP280::FILTER_X16, /* Filtering. */
|
||
|
Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
|
||
|
|
||
|
}
|
||
|
|
||
|
void loopBarometer(unsigned long millis) {
|
||
|
static unsigned long last_barometerreading = 0;
|
||
|
if (millis - last_barometerreading > BAROMETER_UPDATE_INTERVAL)
|
||
|
{
|
||
|
last_barometerreading=millis;
|
||
|
barometer_temp=bmp.readTemperature();
|
||
|
|
||
|
|
||
|
|
||
|
if (!barometer_alt_calibrated) { //only read pressure for calibration
|
||
|
barometer_pressure=bmp.readPressure();
|
||
|
}
|
||
|
|
||
|
if (!barometer_alt_calibrated && millis>5000) {
|
||
|
|
||
|
barometer_alt_calibrated=true;
|
||
|
barometer_alt_calibrationpressure=barometer_pressure/100.0;
|
||
|
}
|
||
|
|
||
|
if (barometer_alt_calibrated) {
|
||
|
barometer_alt=bmp.readAltitude(barometer_alt_calibrationpressure);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
float getBarometerHeight() {
|
||
|
return barometer_alt;
|
||
|
}
|
||
|
float getBarometerTemperature() {
|
||
|
return barometer_temp;
|
||
|
}
|