Hello,
I am new to Arduino and am working on an altimeter. I got myself a Spark Fun MS8607 temp, pressure, and humidity sensor. I am getting good altitude readings but I want to calculate altitude difference as well. I do this by finding the difference between the present altitude and the altitude from 3 seconds ago. I wrote some code that shows this below. I move the sensor up and down between readings and whenever I print values the present and past altitude readings are always the same and thus the difference is always zero.
I have been trouble shooting for the past few days and any help would be greatly appreciated!
Code
#include <SparkFun_PHT_MS8607_Arduino_Library.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
// Click here to get the library: http://librarymanager/All#SparkFun_PHT_MS8607
int chipSelect=2;
MS8607 barometricSensor;
File mysensordata;
float Velocity_past=0;
float startingPressure=0;
int Velocity = 0;
float TimeKeeper=0;
float Altitude_current=0;
void setup(void)
{
pinMode (10, OUTPUT);
SD.begin(chipSelect);
Serial.begin(115200);
Serial.println("Qwiic PHT Sensor MS8607 Example");
Wire.begin();
if (barometricSensor.begin() == false)
{
Serial.println("MS8607 sensor did not respond. Trying again...");
if (barometricSensor.begin() == false)
{
Serial.println("MS8607 sensor did not respond. Please check wiring.");
while (1)
;
}
}
//Set the resolution of the sensor to the highest level of resolution: 0.016 mbar
barometricSensor.set_pressure_resolution(MS8607_pressure_resolution_osr_8192);
//Take 16 readings and average them
startingPressure = 0.0;
for (int x = 0; x < 16; x++)
startingPressure += barometricSensor.getPressure();
startingPressure /= (float)16;
Serial.print("Starting pressure=");
Serial.print(startingPressure);
Serial.println("hPa");
}
void loop(void)
{
float currentPressure = barometricSensor.getPressure();
int altitudeDelta = barometricSensor.altitudeChange(currentPressure, startingPressure);
int (alt_past) = (altitudeDelta);
Serial.println("alt_past");
Serial.println(alt_past);
{
delay(3000);
int altitude_difference = 0;
int (alt_present) = (altitudeDelta);
Serial.println(alt_present);
altitude_difference = alt_present - alt_past;
Serial.println(altitude_difference);
}
}