finalized code of LUTR v1
Dependencies: PM2_Libary Adafruit_GFX BME680
Diff: main.cpp
- Revision:
- 16:7536b5d2365c
- Parent:
- 15:d14167e8c555
- Child:
- 17:80e441d2b10a
--- a/main.cpp Tue May 11 17:20:21 2021 +0000 +++ b/main.cpp Mon May 17 12:23:31 2021 +0000 @@ -23,7 +23,7 @@ /* BME680 */ -I2C i2c(I2C_SDA, I2C_SCL); // I2C Zuweisung Used inside the BME680 Mbed Lib. +I2C i2c(I2C_SDA, I2C_SCL); //I2C Zuweisung Used inside the BME680 Mbed Lib. BME680 bme680(0x77 << 1); //Object erstellen (<<1 für mbed 8bit Adresse) /* OLED */ @@ -55,6 +55,9 @@ /* Methodendeklaration */ void servoPos(int pos,int wait1, int wait2); void checkExtreme(float temp, float hum, int press, int voc, bool reset); +void setAverage(float temp, float hum, int press, int voc, int arraynr); +int getAverageI(int array[]); +float getAverageF(float array[]); /* Zeitmanagement */ bool executeMainTask = false; @@ -63,6 +66,13 @@ int mode = 1; int counter = 0; +/* Array für Mittelwerte*/ +float tempAr[30], humAr[30]; +int pressAr[30], vocAr[30]; +int arrayNr = 0; +int firstThirty = 0; +bool firstLap = true; + /* Sonstige Parameter */ bool maxOneTime = true; //identische Position maximal ein mal einstellen bool resetExtreme = true; //reset @@ -91,7 +101,7 @@ oled.splashCustomLogo(); //Logo Anzeigen lassen oled.display(); thread_sleep_for(1000); - set_time(1620477341); // Set RTC time to Wed, 21. April 2021 19:28:30 https://www.epochconverter.com/ + set_time(1620477341); //Set RTC time to Wed, 21. April 2021 19:28:30 https://www.epochconverter.com/ thread_sleep_for(1000); oled.clearDisplay(); if (!bme680.begin()) { //begin() startet Sensor: Vorheizen usw... @@ -131,24 +141,35 @@ }else{ oled.printf("Failed to perform reading :(\n"); } - + /* Mittelwerte */ + if(counter%3600==0||((counter%120==0)&& firstLap)){ //erste 30min: 1 mal in der Minute; danach alle 30 min + setAverage(temp, hum, press, voc, arrayNr); + (arrayNr==29)? arrayNr= 0: arrayNr++; + } /* Extremwerte */ checkExtreme(temp, hum, press, voc, resetExtreme); /* Anzeige */ switch (mode){ - case 1: oled.printf("Temperatur: %.2f C\r\n",temp); + case 1: oled.printf("Aktuellwerte\r\n"); + oled.printf("Temperatur: %.2f C\r\n",temp); oled.printf("Luftf.: %.2f %%\r\n",hum); oled.printf("Luftdr.: %d hPa\r\n",press); oled.printf("VOC: %d kOhm\r\n",voc); oled.display(); break; - case 2: oled.printf("Mittelwerte"); + case 2: oled.printf("Mittelwerte\r\n"); + oled.printf("Temperatur: %.2f C\r\n",getAverageF(tempAr)); + oled.printf("Luftf.: %.2f %%\r\n",getAverageF(humAr)); + oled.printf("Luftdr.: %d hPa\r\n",getAverageI(pressAr)); + oled.printf("VOC: %d kOhm\r\n",getAverageI(vocAr)); + oled.printf("count: %d, fT: %d",counter,firstThirty); oled.display(); break; - case 3: oled.printf("Temp: %.1f %.1f C\r\n",minTemp, maxTemp); + case 3: oled.printf("Extremwerte\r\n"); + oled.printf("Temp: %.1f %.1f C\r\n",minTemp, maxTemp); oled.printf("Luftf.: %.1f %.1f %%\r\n",minHum, maxHum); oled.printf("Luftd.: %d %d hPa\r\n",minPress, maxPress); oled.printf("VOC: %d %d kOhm\r\n",minVoc, maxVoc); @@ -180,7 +201,12 @@ int dT_loop_ms = Ts_ms - T_loop_ms; if(dT_loop_ms>=0 && dT_loop_ms<=Ts_ms)thread_sleep_for(dT_loop_ms); oled.clearDisplay(); - (counter==10000) ? counter=0 : counter++; + if(counter==3600){ + counter = 0; + firstLap = false; + }else{ + counter++; + } }else{ oled.clearDisplay(); @@ -230,6 +256,31 @@ (mode!=3) ? mode++ : mode=1; } } + +/* Mittelwerte */ +void setAverage(float temp, float hum, int press, int voc, int arrayNr){ + tempAr[arrayNr] = temp; + humAr[arrayNr] = hum; + pressAr[arrayNr] = press; + vocAr[arrayNr] = voc; + if(firstThirty<29)firstThirty++; +} +int getAverageI(int array[]){ + int sum = 0; + for(int j=0; j<firstThirty;j++){ + sum+=array[j]; + } + return sum/(firstThirty+1); +} + +float getAverageF(float array[]){ + float sum = 0; + for(int j=0; j<firstThirty;j++){ + sum+=array[j]; + } + return sum/firstThirty; +} + /* Extemwerte */ void checkExtreme(float temp, float hum, int press, int voc, bool reset){ if(reset){ //reset