Altimeter code

So I am current working on a project that uses an bmp388 altimeter. I need to calculate the change in altitude over time or max altitude and to do that I was going to do: ChangeInAltitude = currentAltitdue - previousAltitude.
I was wondering if there was a way to get the previous altitude. Or if there was a more efficient way to solve the problem.

Isn't the "previous altitude" just the "current altitude" from some time in the past?

johnwasser:
Isn't the "previous altitude" just the "current altitude" from some time in the past?

That is true, but how would that translate to code?

Measure the altitude, set previous_altitude equal to the result.

Wait, or do something else for a while.

Measure the altitude, set current_altitude equal to the result.

change_in_altitude = current_altitude - previous_altitude

With a little thought, in the loop function you need measure only once per loop. Simply copy current_altitude to previous_altitude before making the new measurement of current_altitude.