So, jetzt aber hoffentlich sauberer Code, mit gleitendem Mittelwert. Das braucht mehr Variablen, deshalb wollte ich mich drücken . Noch Vorschläge?
Code:
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#include <LiquidCrystal.h>
float temperatureArray[16];
float temperatureSum;
float temperature;
float pressureArray[9];
float pressureSum;
float pressure;
float tempCorrection = -0.5; // absolute sensor error correction
byte x; // array index
Adafruit_BMP280 bme; // I2C
const int rs = 5, en = 6, d4 = 9, d5 = 10, d6 = 11, d7 = 12; // Arduino pin connection
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
lcd.begin(16, 2);
bme.begin(0x76);
delay(100);
lcd.clear();
}
void loop() {
getTemperature();
getPressure();
printTemperature();
printPressure();
delay(50);
}
float getTemperature()
{
temperatureSum -= temperatureArray[0]; //oldest temperature reading removed
for(x=0;x<15;x++) temperatureArray[x] = temperatureArray[x+1]; // shift array and lose [0]
temperatureArray[15] = bme.readTemperature(); // new value in array at highest position
temperatureSum += temperatureArray[15]; //newest temperature reading added
temperature = temperatureSum/16.0; // moving average
}
float getPressure()
{
pressureSum -= pressureArray[0]; //oldest pressure reading removed
for(x=0;x<8;x++) pressureArray[x] = pressureArray[x+1]; // shift array and lose [0]
pressureArray[8] = bme.readPressure(); // new value in array at highest position
pressureSum += pressureArray[8]; //newest pressure reading added
pressure = pressureSum/9.0; // moving average
}
void printTemperature() {
lcd.setCursor(0,0);
lcd.print("T: ");
lcd.print(temperature + tempCorrection,3);
lcd.print(" ");
lcd.print((char)223);
lcd.print("C ");
}
void printPressure() {
lcd.setCursor(0,1);
lcd.print("P: ");
lcd.print(pressure/100.0); // display in hPa
lcd.print(" hPa ");
}