BME680 Zambretti calculation going wrong

I've written some preliminary code that will predict the weather using the Zambretti thingo.
However, it doesn't do what it is supposed. It doesn't matter if the pressure rises, falls, or stays steady, it always says "Fine, Possibly showers", and it also does not display the pressure to the second decimal place which it does with the BME680 example codes.

[code]
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"

Adafruit_BME680 bme;

float currPress = (bme.pressure / 100);

float prevPress = 0;

void setup() {
  Serial.begin(9600);
  if (!bme.begin()) {
    Serial.println("Could not find a valid BME680 sensor, check wiring!");
    while (1);
  }
  // Set up oversampling and filter initialization
  bme.setTemperatureOversampling(BME680_OS_16X);
  bme.setHumidityOversampling(BME680_OS_16X);
  bme.setPressureOversampling(BME680_OS_16X);
}

void loop() {
  if (! bme.performReading()) {
    Serial.println("Failed to perform reading");
    return;
  }
  if (currPress < prevPress) { // falling
    Serial.println(bme.pressure / 100);
    float prevPress = currPress;
    if ((bme.pressure / 100) >= 1050) {
      Serial.println("Settled Fine");
    }
    if ((bme.pressure / 100) >= 1040 and (bme.pressure / 100) < 1050) {
      Serial.println("Fine Weather");
    }
    if ((bme.pressure / 100) >= 1024 and (bme.pressure / 100) < 1040) {
      Serial.println("Fine Becoming Less Settled");
    }
    if ((bme.pressure / 100) >= 1018 and (bme.pressure / 100) < 1024) {
      Serial.println("Fairly Fine Showery Later");
    }
    if ((bme.pressure / 100) >= 1010 and (bme.pressure / 100) < 1018) {
      Serial.println("Showery Becoming more unsettled");
    }
    if ((bme.pressure / 100) >= 1004 and (bme.pressure / 100) < 1010) {
      Serial.println("Unsettled, Rain later");
    }
    if ((bme.pressure / 100) >= 998 and (bme.pressure / 100) < 1004) {
      Serial.println("Rain at times, worse later");
    }
    if ((bme.pressure / 100) >= 991 and (bme.pressure / 100) < 998) {
      Serial.println("Rain at times, becoming very unsettled");
    }
    if ((bme.pressure / 100) >= 985 and (bme.pressure / 100) < 991) {
      Serial.println("Very Unsettled, Rain");
    }
  }
  if (currPress == prevPress) { // steady
    Serial.println(bme.pressure / 100);
    float prevPress = currPress;
    if ((bme.pressure / 100) >= 1033) {
      Serial.println("Settled Fine");
    }
    if ((bme.pressure / 100) >= 1023 and (bme.pressure / 100) < 1033) {
      Serial.println("Fine Weather");
    }
    if ((bme.pressure / 100) >= 1014 and (bme.pressure / 100) < 1023) {
      Serial.println("Fine, Possibly showers");
    }
    if ((bme.pressure / 100) >= 1008 and (bme.pressure / 100) < 1014) {
      Serial.println("Fairly Fine , Showers likely");
    }
    if ((bme.pressure / 100) >= 1000 and (bme.pressure / 100) < 1008) {
      Serial.println("Showery Bright Intervals");
    }
    if ((bme.pressure / 100) >= 994 and (bme.pressure / 100) < 1000) {
      Serial.println("Changeable some rain");
    }
    if ((bme.pressure / 100) >= 989 and (bme.pressure / 100) < 994) {
      Serial.println("Unsettled, rain at times");
    }
    if ((bme.pressure / 100) >= 981 and (bme.pressure / 100) < 989) {
      Serial.println("Rain at Frequent Intervals");
    }
    if ((bme.pressure / 100) >= 974 and (bme.pressure / 100) < 981) {
      Serial.println("Very Unsettled, Rain");
    }
    if ((bme.pressure / 100) >= 960 and (bme.pressure / 100) < 974) {
      Serial.println("Stormy, much rain");
    }
  }
  if (currPress > prevPress) { // rising
    Serial.println(bme.pressure / 100) ;
    float prevPress = currPress;
    if ((bme.pressure / 100) >= 1030) {
      Serial.println("Settled Fine");
    }
    if ((bme.pressure / 100) >= 1022 and (bme.pressure / 100) < 1030) {
      Serial.println("Fine Weather");
    }
    if ((bme.pressure / 100) >= 1012 and (bme.pressure / 100) < 1022) {
      Serial.println("Becoming Fine");
    }
    if ((bme.pressure / 100) >= 1007 and (bme.pressure / 100) < 1012) {
      Serial.println("Fairly Fine, Improving");
    }
    if ((bme.pressure / 100) >= 1000 and (bme.pressure / 100) < 1007) {
      Serial.println("Fairly Fine, Possibly showers, early");
    }
    if ((bme.pressure / 100) >= 995 and (bme.pressure / 100) < 1000) {
      Serial.println("Showery Early, Improving");
    }
    if ((bme.pressure / 100) >= 990 and (bme.pressure / 100) < 995) {
      Serial.println("Changeable Mending");
    }
    if ((bme.pressure / 100) >= 984 and (bme.pressure / 100) < 990) {
      Serial.println("Rather Unsettled Clearing Later");
    }
    if ((bme.pressure / 100) >= 978 and (bme.pressure / 100) < 984) {
      Serial.println("Unsettled, Probably Improving");
    }
    if ((bme.pressure / 100) >= 970 and (bme.pressure / 100) < 978) {
      Serial.println("Unsettled, short fine Intervals");
    }
    if ((bme.pressure / 100) >= 965 and (bme.pressure / 100) < 970) {
      Serial.println("Very Unsettled, Finer at times");
    }
    if ((bme.pressure / 100) >= 959 and (bme.pressure / 100) < 965) {
      Serial.println("Stormy, possibly improving");
    }
    if ((bme.pressure / 100) >= 947 and (bme.pressure / 100) < 959) {
      Serial.println("Stormy, much rain");
    }
  }
}
[/code]

Why are these two issues happening? What is wrong with the code?

Could it be an initialisation issue? This code is executed before bme.begin() is called.

Also, this creates a new value called prevPress, it does not update the global variable prevPress (try to drop the word float here).

@outbackhut
if I'm not mistaken, this is the third time when you write something like "library XXX is incorrect"
Doesn't it occur to you that in all cases the problem is not in the library, but in your code, or in the fact that you did not understand something? :slight_smile:

@jfjlaros
I can't tell for sure until it prints to two decimal places, however moving that line to void loop() has made it now only ever display "Becoming Fine". I can't tell if it should be saying that or if it is jammed again because I am getting a whole number with no decimal places at all.

How do I fix this? Will moving it to void loop() fix this error?

@b707

You are entirely correct, however in this topic I have said that this is an error in my code. I am well aware of that fact, it has nothing to do with the library. I am missing something in my code but I don't know what.

and you are right

Your error - multiple definition of prevPress variable, which makes it impossible to track changes in pressure and somehow predict the weather

@b707
It still doesn't print to two decimal places here:

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"

Adafruit_BME680 bme;

float prevPress = 0;

void setup() {
  Serial.begin(9600);
  if (!bme.begin()) {
    Serial.println("Could not find a valid BME680 sensor, check wiring!");
    while (1);
  }
  // Set up oversampling and filter initialization
  bme.setTemperatureOversampling(BME680_OS_16X);
  bme.setHumidityOversampling(BME680_OS_16X);
  bme.setPressureOversampling(BME680_OS_16X);
}

void loop() {
  if (! bme.performReading()) {
    Serial.println("Failed to perform reading");
    return;
  }
  float currPress = (bme.pressure / 100);
  if (currPress < prevPress) { // falling
    Serial.println(bme.pressure / 100);
    prevPress = currPress;
    if ((bme.pressure / 100) >= 1050) {
      Serial.println("Settled Fine");
    }
    if ((bme.pressure / 100) >= 1040 and (bme.pressure / 100) < 1050) {
      Serial.println("Fine Weather");
    }
    if ((bme.pressure / 100) >= 1024 and (bme.pressure / 100) < 1040) {
      Serial.println("Fine Becoming Less Settled");
    }
    if ((bme.pressure / 100) >= 1018 and (bme.pressure / 100) < 1024) {
      Serial.println("Fairly Fine Showery Later");
    }
    if ((bme.pressure / 100) >= 1010 and (bme.pressure / 100) < 1018) {
      Serial.println("Showery Becoming more unsettled");
    }
    if ((bme.pressure / 100) >= 1004 and (bme.pressure / 100) < 1010) {
      Serial.println("Unsettled, Rain later");
    }
    if ((bme.pressure / 100) >= 998 and (bme.pressure / 100) < 1004) {
      Serial.println("Rain at times, worse later");
    }
    if ((bme.pressure / 100) >= 991 and (bme.pressure / 100) < 998) {
      Serial.println("Rain at times, becoming very unsettled");
    }
    if ((bme.pressure / 100) >= 985 and (bme.pressure / 100) < 991) {
      Serial.println("Very Unsettled, Rain");
    }
  }
  if (currPress == prevPress) { // steady
    Serial.println(bme.pressure / 100);
    prevPress = currPress;
    if ((bme.pressure / 100) >= 1033) {
      Serial.println("Settled Fine");
    }
    if ((bme.pressure / 100) >= 1023 and (bme.pressure / 100) < 1033) {
      Serial.println("Fine Weather");
    }
    if ((bme.pressure / 100) >= 1014 and (bme.pressure / 100) < 1023) {
      Serial.println("Fine, Possibly showers");
    }
    if ((bme.pressure / 100) >= 1008 and (bme.pressure / 100) < 1014) {
      Serial.println("Fairly Fine , Showers likely");
    }
    if ((bme.pressure / 100) >= 1000 and (bme.pressure / 100) < 1008) {
      Serial.println("Showery Bright Intervals");
    }
    if ((bme.pressure / 100) >= 994 and (bme.pressure / 100) < 1000) {
      Serial.println("Changeable some rain");
    }
    if ((bme.pressure / 100) >= 989 and (bme.pressure / 100) < 994) {
      Serial.println("Unsettled, rain at times");
    }
    if ((bme.pressure / 100) >= 981 and (bme.pressure / 100) < 989) {
      Serial.println("Rain at Frequent Intervals");
    }
    if ((bme.pressure / 100) >= 974 and (bme.pressure / 100) < 981) {
      Serial.println("Very Unsettled, Rain");
    }
    if ((bme.pressure / 100) >= 960 and (bme.pressure / 100) < 974) {
      Serial.println("Stormy, much rain");
    }
  }
  if (currPress > prevPress) { // rising
    Serial.println(bme.pressure / 100) ;
    prevPress = currPress;
    if ((bme.pressure / 100) >= 1030) {
      Serial.println("Settled Fine");
    }
    if ((bme.pressure / 100) >= 1022 and (bme.pressure / 100) < 1030) {
      Serial.println("Fine Weather");
    }
    if ((bme.pressure / 100) >= 1012 and (bme.pressure / 100) < 1022) {
      Serial.println("Becoming Fine");
    }
    if ((bme.pressure / 100) >= 1007 and (bme.pressure / 100) < 1012) {
      Serial.println("Fairly Fine, Improving");
    }
    if ((bme.pressure / 100) >= 1000 and (bme.pressure / 100) < 1007) {
      Serial.println("Fairly Fine, Possibly showers, early");
    }
    if ((bme.pressure / 100) >= 995 and (bme.pressure / 100) < 1000) {
      Serial.println("Showery Early, Improving");
    }
    if ((bme.pressure / 100) >= 990 and (bme.pressure / 100) < 995) {
      Serial.println("Changeable Mending");
    }
    if ((bme.pressure / 100) >= 984 and (bme.pressure / 100) < 990) {
      Serial.println("Rather Unsettled Clearing Later");
    }
    if ((bme.pressure / 100) >= 978 and (bme.pressure / 100) < 984) {
      Serial.println("Unsettled, Probably Improving");
    }
    if ((bme.pressure / 100) >= 970 and (bme.pressure / 100) < 978) {
      Serial.println("Unsettled, short fine Intervals");
    }
    if ((bme.pressure / 100) >= 965 and (bme.pressure / 100) < 970) {
      Serial.println("Very Unsettled, Finer at times");
    }
    if ((bme.pressure / 100) >= 959 and (bme.pressure / 100) < 965) {
      Serial.println("Stormy, possibly improving");
    }
    if ((bme.pressure / 100) >= 947 and (bme.pressure / 100) < 959) {
      Serial.println("Stormy, much rain");
    }
  }
}

Printing to two decimal places:

Serial.println(bme.pressure / 100, 2);

step by step. You solved the first issue? - good

In which line you supposed to print two decimal places?

@b707

Lines 29, 60, and 94.

I have no idea if I have or not, because I have no way of checking if the pressure has risen, until it rises a full hPa. That's because it isn't printing to two decimal places.

@jfjlaros
That returns this:

1111110111
Fine, Possibly showers

I have no idea what 1111110111 even is.

yes, but printing two decimal places is default for Serial.print()

try

Serial.println(bme.pressure / 100.0);

That worked!
Why does it automatically do that in the example code, but not here?

Also, it still doesn't change when the barometric pressure does. It drops 0.01 hPa and doesn't swap to the 'falling' if statement like it should. It only prints data from the 'steady' if statement.

I did not look closely at the code, look for other errors.

Sorry, but I have to go, we have a morning and a working day here :slight_smile:

Ah, so bme.pressure is an integer apparently, I missed that.

In that case, Serial.print prints the value in base-2 (binary), 1111110111 binary is 1015 decimal.

Anyway, if the original question is answered, please mark the most helpful post as the solution, it prevents helpers spending time on a solved issue and it will lead people with the same question to the right answer directly.

@jfjlaros
Yes, one of the original questions has been answered. However, the weather data still doesn't change when the barometric pressure does. The barometric pressure drops 0.01 hPa and doesn't swap to the 'falling' if statement like it should. It only prints data from the 'steady' if statement.

Maybe you should introduce a delay(10000), or even far more....
You are comparing current pressure to previous pressure multiple times per second. Chances are big that pressure does not change that fast. If it changes, it will print the if < or if > only once, and a split second later it will be equal again.
Also, you will be measuring noise here... a change of .01 is hardly significant...
Maybe you need two moving averages 10 minutes apart... and then allow for some bandwith to go into the equal case

float bandwith = 5.0;
if (abs(curPress-prevPress) < bandwith) {
   //pressure is almost equal...
}

@build_1971
In the future I am wanting to take ten measurements and see if it is consistently going up or down or staying about the same, with a bandwidth of 0.1 hPa (which is quite a change when it comes to pressure)

And very unlikely to happen in a split second...