Previous and current sensor values, calculating the difference.

I am running a sensor (BMP280) that allows me to read an altitude in meters. The example code works great however there is something that I’d like to do with these values.

I want to read the values that come through to the serial monitor and store the initial reading, then the next value that comes through I’d like to know the difference from the initial. So then dividing by the seconds it took between these readings I’d be able to calculate the vertical velocity (rise) at each reading.

Here is the example code I am using:

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>

#define BMP_SCK 13
#define BMP_MISO 12
#define BMP_MOSI 11
#define BMP_CS 10

Adafruit_BMP280 bme; // I2C
//Adafruit_BMP280 bme(BMP_CS); // hardware SPI
//Adafruit_BMP280 bme(BMP_CS, BMP_MOSI, BMP_MISO, BMP_SCK);

void setup() {
Serial.begin(9600);
Serial.println(F(“BMP280 test”));

if (!bme.begin()) {
Serial.println(“Could not find a valid BMP280 sensor, check wiring!”);
while (1);
}
}

void loop()
{
Serial.print("[ Temperature = “);
Serial.print(bme.readTemperature());
Serial.print(” *C ]");

Serial.print("[ Pressure = “);
Serial.print(bme.readPressure());
Serial.print(” Pa ]");

Serial.print("[ Approx altitude = “);
Serial.print(bme.readAltitude(1017.1)); // this should be adjusted to your local forcase
Serial.print(” m ]");

Serial.println();
delay(10);
}

You've now posted this question (equally badly) THREE TIMES.

Please use your brief forum timeout to acquaint yourself with the forum posting guidelines.

Simplest (but not recommended) approach:

float alt0=bme.readAltitude(1017.1);
delay(1000);
float velocity=bme.readAltitude(1017.1) - alt0; //output in (distance units)/second