Updating Var to only store max value

In my project i am using the bmp180 barometric module to take an altitude reading, as the altitude increases I want to store the highest altitude reached in a variable.

From here if the highest altitude is less than the current altitude i want to update it and if the current altitude is less than the highest altitude - 2.5 then I want to +1 to a third variable.

Below is the code i’m using to do this:

The altitude updates correctly each time the loop runs, I just cant get C to print. There’s definitely something i’m missing here.

#include <SFE_BMP180.h> 
#include <Wire.h> 

SFE_BMP180 pressure;


double baseline; 

double Y = 0; //Should store highest alti reached
double C = 0; //Count each time altitude decreases 2.5 less than highest alti


void setup()
{
  Serial.begin(9600);
  
  
  if (pressure.begin()){
    Serial.println("BMP180 init success");
  } else  {
    Serial.println("BMP180 init fail\n\n");
    while(1);
  }

baseline = getPressure();
  
  Serial.print("baseline pressure: ");
  Serial.print(baseline);
  Serial.println(" mb");  
}

double getPressure()
{
  char status;
  double T,P,p0,a;

  status = pressure.startTemperature();
  if (status != 0)
  {

    delay(status);

    status = pressure.getTemperature(T);
    if (status != 0)
    {
     status = pressure.startPressure(3);
      if (status != 0)
      {
        delay(status);

        status = pressure.getPressure(P,T);
        if (status != 0)
        {
          return(P);
        }
        else Serial.println("error retrieving pressure measurement\n");
      }
      else Serial.println("error starting pressure measurement\n");
    }
    else Serial.println("error retrieving temperature measurement\n");
  }
  else Serial.println("error starting temperature measurement\n");
}



void loop()
{
        
        double a,P,x,Z;
        
       
        P = getPressure(); 
        a = pressure.altitude(P,baseline);
        x = a; // current altitude
        Z = Y - 2.5; // max altitude less 2.5
          
          if (Y <= x) Y = x; //If max alti is less than or equal to curr alti then max alti equals curr alti
          if (x <= Z) C = C + 1; // If curr alti is less than or equal to max alti-2.5 plus 1 to count

          if(C >= 1){
                    Serial.println(C); // if count is equal to or greater than 1 then print count
          }
}

Define a global double variable, say maxAlt, to keep track of the largest value found. In your loop write:

...
double maxAlt = 0;
...
loop() {
        ...
        P = getPressure(); 
        a = pressure.altitude(P,baseline);
        x = a; // current altitude
        if( a > maxAlt ) maxAlt = a;              // ensure maxAlt stores the largest altitude value.
        Z = Y - 2.5; // max altitude less 2.5
        ...

This will cause maxAlt to always contain the largest altitude found so far.

Not sure why you are using two variables (x and a) to store the exact same thing. Doing this is more than just a waste of memory, it also requires that you keep their values synchronized or else there will be ambiguities between those parts of the code that use ‘a’, and those that use ‘x’. I’d get rid of the ‘x’ and just use ‘a’ everywhere, if I were you.

Thanks for the reply Axe Murderer

I think i may have phrased the title of my question incorrectly... The max altitude is counting correctly, running up my stairs and back down it prints values from 0 up to 4.8 then back down to 0, the Z value however ALWAYS prints -2.5.

It seems that the line 'Z = Y - 2.5;' just gives Z = -2.5

AxeMurderer, you're solution of adding the extra var worked perfectly, I just replaced Z=Y-2.5 with Z=maxAlt-2.5 and it worked perfectly. I've also removed the x=a, i'm not too sure where that came from!

Thanks a lot

@AxeMurderer: please mind your language.(I deleted your last post)

double Y = 0; //Should store highest alti reached

No, it shouldn't. There is no reason to use meaningless names like that.