Trouble calculating difference in 2 numbers

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);
}
}

I think you are dreaming. My former company had a customer that had an aviation related product. It had an expensive barometer on their circuit board. The best they could detect was 15 feet change in altitude. I doubt your device can do that well.
Paul

Is that on a rocket or something? How much altitude change can you get in 3 seconds?

By hand?

What does "good readings" mean? If you can get good readings you should be able to subtract them.

I don't know anything about this thing but I'd be surprised if you can measure a change of a few feet (or a few meters).

Not sure what all the extraneous parenthesis are for.

You are taking the pressure reading at the start of loop, then saving the calculated altitude as the previous altitude, waiting three seconds, and comparing the same calculated altitude with that you just stored as the previous altitude. Of course it doesn't change, you did not take a 2nd pressure reading.

Thanks a lot!
That did it, thanks to everyone who replied and yes I am using this on a model rocket!

(Why) the (parentheses) here, (and) not (other) declarations?

I've detected a foot or two change with some MEMS barometers... I think it was the SCP1000-D1, which has (had) a resolution of 1.5Pa (12cm height) and a 19bit ADC.

Hi,
When you are measuring real pressure values, do they change as

Tom... :grinning: :+1: :coffee: :australia:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.