assignment of read-only variable

Hello everyone,
I would like that when my GPS calculates an altitude, it keeps it in memory as a constant to calculate the QNH function (constant). With the code I made I have this error: assignment of read-only variable 'QNH'.
As long as the GPS does not find a satellite, it prints 0 and I would like that as soon as it prints something different from 0, it keeps in memory this value to calculate the QNH.
Thank you :slight_smile:

int32_t pressure_init;
const int32_t QNH; //Calcul du QNH en fonction de l'alti du GPS

void setup()
{
  
  // Read pressure pour calculer le QNH (Pascal)
  pressure_init = (baro.getPressure());
  pushAvg(pressure_init);
  pressure_init=getAvg(movavg_buff, MOVAVG_SIZE);

  return pressure_init;

  delay(1000);
}
 
void loop()
{
  
  if (GPS.altitude!=0){
  QNH = sea_pressure(pressure_init,GPS.altitude);
}

You've declared QNH const (constant) - meaning it will never change (and inviting the compiler to assume that while optimizing)

And then what do you do in loop? Try to change it!

Remove the const keyword in your declaration of QNH.

const should be used when you have a constant that you might want to change in the future, but will reference from multiple places (or even just one place, buried in your code where it's hard to find) - you'll always want to initialize them with a value when you declare them.

Thank you for your reply. My goal is that my QNH is constant once the gps has found an altitude different from 0. If I remove the "const" it compiles well but my QNH will not be constant because "GPS.altitude" varies with time.

Set a boolean to false. Read the QNH value only when the boolean is false and save it to another variable then set the boolean to true. Use the value in the second variable as the QNH in subsequent code

Variables don't change value on their own. The best way to assign a value to QNH and have it remain constant is to note that you've assigned it and then check the note before assigning anything else to it.

Basically you're asking for a read-only value that you can write something to. You'll have to arrange that illogical thing yourself.

Steve

A trick I often use is to initialize the variable to an impossible value, and test it against that - if it's not that value, it's been set to something. If zero is an impossible value, you can just do

if (!variable)...

boolean altitudeCaptured = false;

int32_t pressure_init;
const int32_t QNH; //Calcul du QNH en fonction de l'alti du GPS

void setup()
{

// Read pressure pour calculer le QNH (Pascal)
pressure_init = (baro.getPressure());
pushAvg(pressure_init);
pressure_init=getAvg(movavg_buff, MOVAVG_SIZE);

return pressure_init;

delay(1000);
}

void loop()
{

if (GPS.altitude!=0 && altitudeCaptured == false){  
QNH = sea_pressure(pressure_init,GPS.altitude);
altitudeCaptured = true;  // set altitudeCaptured to true so the if statement will be false on next loop and QNH won't be updated
}

edit: couldn't see the ! in front of the =. Need new glasses.
edit 2: forgot I quoted and was thinking I put in the code tags. Need new brain.

void setup()
{
 
  // Read pressure pour calculer le QNH (Pascal)
  pressure_init = (baro.getPressure());
  pushAvg(pressure_init);
  pressure_init=getAvg(movavg_buff, MOVAVG_SIZE);

  return pressure_init;

  delay(1000);
}

Didn't the compiler complain about that return?

Hello, thank you all for your answers.
The technique with the Boolean works perfectly it's great!

AWOL:
Didn't the compiler complain about that return?

No I have no problem to compile but I think to remove this calculation in the setup and put it in the loop by using a Boolean equally.