Saving first readout and comparing to it

Hello everyone,

I'm using arduino nano with BMP280 sensor and a RGB led.
I want the color of the LED to change according to the pressure readout.
I'm using this code which has 3 parameters for the readout to change the color.

#include <Adafruit_BMP280.h>

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

Adafruit_BMP280 bmp;

int redPin   = 2;
int greenPin = 3;
int bluePin  = 4;

void setup() {
  Serial.begin(9600);
  Serial.println(F("BMP280 sensor"));

  if (!bmp.begin(0x76)) {
    Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));
    while (1);
  }
  
  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     /* Operating Mode. */
                  Adafruit_BMP280::SAMPLING_X2,     /* Temp. oversampling */
                  Adafruit_BMP280::SAMPLING_X16,    /* Pressure oversampling */
                  Adafruit_BMP280::FILTER_X16,      /* Filtering. */
                  Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
}

void setColor(int redValue, int greenValue, int blueValue) {
  analogWrite(redPin, redValue);
  analogWrite(greenPin, greenValue);
  analogWrite(bluePin, blueValue);

}

void loop() {

    Serial.print(F("Pressure = "));
    Serial.print(bmp.readPressure());
    Serial.println(" Pa");

       if (bmp.readPressure() > 100000){
       setColor(255, 0, 0); 

       }else if (bmp.readPressure() < 100000 && bmp.readPressure() > 80000){
       setColor(0, 0, 255); 
       delay (500);
       setColor(0, 0, 0);
       delay (500);       
       
       }else if (bmp.readPressure() < 80000){
       setColor(0, 255, 0); 
       }

}

My question is, if i want to save the first readout and make the color change when the readout drops in X number of units from the first saved readout, how do i do it?

To make thing more clear:
First readout = X // color is red
when X-5000 units // color is blue
when X-20000 units // color is green

Thank you :slight_smile:

Read the pressure into a variable once at the start of loop() then use it throughout loop() as required. If you want to save the value under some circumstances then copy it to a second variable

Thank you for your reply,
I’m new at this and have no clue on how to do it… :frowning:
can you please show me how to read it into a variable and use it later?

unsigned long  currentPressure = bmp.readPressure();
if (currentPressure > 100000)
{
  //do whatever you need to when pressure greater than 100000
}

The thing is how do i tell it to change color when the readout drops in 5000 units from the variable? and not when it's bigger or smaller than a fix number?

Sorry, as i said...
I'm a noob!

Let's suppose that you have saved the previous pressure in a variable named previousPressure and you have the current pressure in a variable named currentPressure how would you determine that the value had changed by 5000 units ?

Previouse - current... but how do i write it in code?

Really ?
Have a wild guess

Hi, I tried doing this...

void loop() {

    Serial.print(F("Pressure = "));
    Serial.print(bmp.readPressure());
    Serial.println(" Pa");

    unsigned long  FirstRead = bmp.readPressure(); 
    
         if (FirstRead - bmp.readPressure() < 5000) {
         setColor(0, 255, 0); }
    
         else if (FirstRead - bmp.readPressure() > 5000) {
         setColor(0, 0, 255); }
     
    

But for some reason it's not changing the color to green...
Can you point me to my mistake?

*to blue

You need to do something like this

start of loop()
  read currentValue
  if currentValue - previousValue >= 5000
    //the value has changed by at least 5000
    //time to do soemthing
  previouValue = currentValue //save value for next comparison
end of loop()

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.