finalized code of LUTR v1

Dependencies:   PM2_Libary Adafruit_GFX BME680

Revision:
17:80e441d2b10a
Parent:
16:7536b5d2365c
Child:
18:5019da899a41
diff -r 7536b5d2365c -r 80e441d2b10a main.cpp
--- a/main.cpp	Mon May 17 12:23:31 2021 +0000
+++ b/main.cpp	Sun May 23 11:53:32 2021 +0000
@@ -18,6 +18,8 @@
 #define BME_MOSI 11
 #define BME_CS 10
 
+#define ARRAYLENGTH 60
+
   
 using namespace std::chrono;                                                    //Namespce für printf usw...
   
@@ -50,6 +52,7 @@
 int servoPeriod_mus = 20000;                                                    //Zeitperiode
 int servoMax = 580, servoMin = 350;                                             //2300: 180 ; 300: 0
 float a = -2.3, b = 810;                                                        //Gas -> Rotation
+float tempA = 0.8369, tempB = -0.6236;                                          //Temperaturkompensation
 
 
 /* Methodendeklaration */
@@ -62,19 +65,19 @@
 /* Zeitmanagement */
 bool           executeMainTask = false;
 Timer          power_button_timer,mode_button_timer, loop_timer;
-int            Ts_ms = 500;                                                     //Durchlaufzeit
-int            mode = 1;
-int            counter = 0;
+int            Ts_ms = 500;                                                     //Durchlaufzeit -> 2Hz
+int            mode = 1;                                                        //Anzeigemodus: Start auf aktuellen Werten
+int            counter = 0;                                                     //Zähler für verschiedene Frequnzen
 
-/* Array für Mittelwerte*/
-float          tempAr[30], humAr[30];
-int            pressAr[30], vocAr[30];
+/* Arrays für Mittelwerte*/
+float          tempAr[ARRAYLENGTH], humAr[ARRAYLENGTH];
+int            pressAr[ARRAYLENGTH], vocAr[ARRAYLENGTH];
 int            arrayNr = 0;
-int            firstThirty = 0;
+int            firstRound = 0;
 bool           firstLap = true;
 
 /* Sonstige Parameter */
-bool maxOneTime = true;                                                         //identische Position maximal ein mal einstellen
+bool maxOneTime = true;                                                         //identische Position maximal ein Mal einstellen
 bool resetExtreme = true;                                                       //reset
 float temp, maxTemp, minTemp, hum, maxHum, minHum;
 int press, maxPress, minPress, voc, maxVoc, minVoc;
@@ -106,6 +109,8 @@
     oled.clearDisplay();
     if (!bme680.begin()) {                                                      //begin() startet Sensor: Vorheizen usw...
         oled.printf("BME680 Begin failed \r\n");                                //Fehlermeldung
+    }else{
+        bme680.performReading();                                                //Nullwerte abfangen
     }
     servoPos(servoMax,1000,1000);                                               //Endpositionen anfahren
     servoPos(servoMin,1000,1000);
@@ -134,7 +139,7 @@
             
             /* Werte auslesen */
             if (bme680.performReading()) {                          
-                temp = bme680.getTemperature()-4.0;
+                temp = tempA*bme680.getTemperature()+TempB;                     //Temperaturkompensation
                 hum = bme680.getHumidity();
                 press = static_cast<int>(bme680.getPressure()/100);
                 voc = static_cast<int>(bme680.getGasResistance()/1000.0);
@@ -142,9 +147,9 @@
                 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
+            if(((counter%(24*3600/ARRAYLENGTH)==0)&& !firstLap)||((counter%(3600/ARRAYLENGTH)==0)&& firstLap)){  //erste 30min: array füllen; danach alle 30 min einen Wert eintragen
                 setAverage(temp, hum, press, voc, arrayNr);
-                (arrayNr==29)? arrayNr= 0: arrayNr++;
+                (arrayNr==(ARRAYLENGTH-1))? arrayNr= 0: arrayNr++;
             }
             /* Extremwerte */
             checkExtreme(temp, hum, press, voc, resetExtreme);
@@ -164,7 +169,6 @@
                         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;
                     
@@ -178,7 +182,7 @@
             }
             
             /* Servo */
-            if(counter%10==0){
+            if(counter%10==0){                                                  //Nur alle 5s Position ändern
                 int output = static_cast<int>(a*(bme680.getGasResistance()/1000.0)+b);
                 if(output>=servoMin && output<=servoMax){
                     servoPos(output,250,0);
@@ -200,18 +204,21 @@
             int T_loop_ms = duration_cast<milliseconds>(loop_timer.elapsed_time()).count();
             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();
-            if(counter==3600){
-                counter = 0;
+            
+            (counter==1440000)?counter=0: counter++;                            //Zähler um 1 erhöhen und nach 720000s wiederholen
+            if(counter==3600){                                                  //Erste Runde um Array zu füllen
                 firstLap = false;
-            }else{
-                counter++;
             }
             
         }else{
             oled.clearDisplay();
             oled.display();
-            servoPos(servoMax,Ts_ms,0);
+            if(maxOneTime){
+                servoPos(servoMax,Ts_ms,0);
+                maxOneTime = false;                                             
+            }
         }
     }
 }
@@ -263,22 +270,23 @@
     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);
+    if(firstRound<ARRAYLENGTH)firstRound++;                                     //Arraylänge für Durchschnitt
 }
 
-float getAverageF(float array[]){
+int getAverageI(int array[]){                                                   //int als Rückgabewert
+    int sum = 0;
+    for(int j=0; j<firstRound;j++){    
+        if(array[j]!=0)sum+=array[j];
+    }
+    return sum/firstRound;                              
+}
+
+float getAverageF(float array[]){                                               //float als Rückgabewert
     float sum = 0;
-    for(int j=0; j<firstThirty;j++){
-        sum+=array[j];
+    for(int j=0; j<firstRound;j++){
+        if(array[j]!=0)sum+=array[j];
     }
-    return sum/firstThirty;
+    return sum/firstRound;
 }
     
 /* Extemwerte */