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