Exluding ZMOD

Dependencies:   iAQ_Core Adafruit_SGP30_mbed mbed BME680

Revision:
2:ef98576cd67b
Parent:
1:08bbc3ecd836
Child:
3:3d51f8870e91
--- a/main.cpp	Tue Jun 04 14:48:16 2019 +0000
+++ b/main.cpp	Fri Jul 12 15:58:11 2019 +0000
@@ -1,28 +1,182 @@
 #include "mbed.h"
-#include <SoftSerial.h>
-
-Serial ble(A4,A5);
-AnalogIn flowIn(A3);
+#include "flow.h"
 
-float flowVal1;
-float flowVal2;
-float Pressure;
-float finalflow;
+/////////////////////////
+// In this version of the program developed for the Breath project, flow and CO2
+// are measured in a separate .h file called: "flow.h" which is included in the
+// main code. Data is streamed using a serial connection (TTL cable or Bluetooth)
+// without any intruption because of loop calculations. 
+// This version is especially suitable to be used for KST.
+// Also, a solenoid would be turned on and off based on calculating the standard deviation in CO2 profile.
+//
+// Generated by: Mehrnaz Javadipour
+////////////////////////// 
 
-float flow()
+Serial ttl(PA_2,PA_3);  //TTL cable TX,RX
+DigitalOut sol(PC_5);   //Solenoid: Digital Output
+
+int main()
 {
-   flowVal1=3.3*flowIn; //Logic level 3.3
-          flowVal2 = 1.5*flowVal1; //5v
-          Pressure =(125*flowVal2)-62.5;
-          finalflow=(0.1989*sqrt(Pressure))+0.0284; //flow in litter per min
-          return finalflow;
-
-}
-
-int main(){       
-ble.baud(9600);
-    while(1){
-        ble.printf("E2.7, %f, 3.3\n", flow());     
-        } 
-
-}
+    ttl.baud(9600); //baudrate for the serial connection
+    flow();         //calling flow from flow.h
+    carbon();       //calling CO2 from flow.h
+    int bf=0;
+    int i=0;
+    float bfArray[4];
+    float sf=0;
+    float fv=0;
+    int myArray[9];
+    int measurement_started=0;
+    unsigned int sum=0;
+    long double var=0.0;
+    float sigma=0.0;
+    int difSum=0;
+    unsigned int sum2=0;
+    long double var2=0.0;
+    float sigma2=0.0;
+    int difSum2=0;
+     int avg2=0;
+    int avg=0;
+    int m=0;
+    int flags=0;
+    int flage=0;
+    int solstart=0;
+    int solend=0;
+    int fin=0;
+    
+    //float CO_2;
+    
+    while(1)
+    {
+        //CO_2=(float)carbon()/10000;
+        ttl.printf("%f,%f\n",flow(), carbon());
+        if (bf==0)
+            {
+              for(i=0; i<4; i++) 
+                  {
+                    bfArray[i]=flow();  
+                    sf+=bfArray[i];                  
+                  }   
+               fv=sf/4;
+               fv=fv+0.2; 
+               //ttl.printf("set\n");
+               bf=1;
+             }
+        
+        if ((flow()>fv) and (measurement_started ==0)) 
+            {        
+               // ttl.printf("ready to detect plateau \n");
+                measurement_started = 1;
+            }
+        
+        if ((measurement_started == 1) and (solstart==0)) 
+            {
+                //ttl.printf("starting...\n");
+        
+                for(m=0;m<9;m++)
+                    {
+                        ttl.printf("%f,%f\n",flow(), carbon());
+                        myArray[m]=carbon();
+                    }
+                while(flags==0)
+                    {
+                        for(int m=0;m<9;m++)
+                            { 
+                            ttl.printf("%f,%f\n",flow(), carbon());
+                            sum+=myArray[m]; 
+                            }
+                        avg=sum/9;
+                        for(int m=0;m<9;m++)
+                            {
+                            ttl.printf("%f,%f\n",flow(), carbon()); 
+                            difSum+=(myArray[m]-avg)*(myArray[m]-avg); //Find sum of difference between value X and mean
+                            }
+                            var=difSum/9;
+                            sigma=sqrt(var);
+                        if (sigma<0.02)
+                            {
+                             //ttl.printf("sol didnt turn on\n");
+                            for(int m=0;m<8;m++)
+                             {
+                              ttl.printf("%f,%f\n",flow(), carbon());
+                              myArray[m]=myArray[m+1];  //Shift all carbon to left by 1
+                             }
+                             myArray[8]=carbon();   
+                            }else
+                            {
+                            ttl.printf("%f,%f\n",flow(), carbon());
+                           // ttl.printf("sol on\n");
+                            sol=1;
+                            flags=1;
+                            }
+                     }
+                solend=1;
+                solstart =1;
+            }
+        if ((measurement_started == 1) and (solend==1)) 
+        {
+          // ttl.printf("ending...\n");
+        
+                for(m=0;m<9;m++)
+                    {
+                        ttl.printf("%f,%f\n",flow(), carbon());
+                        myArray[m]=carbon();
+                    }
+                while(flage==0)
+                    {
+                        for(int m=0;m<9;m++)
+                            { 
+                            ttl.printf("%f,%f\n",flow(), carbon());
+                            sum2+=myArray[m]; 
+                            }
+                        avg2=sum2/9;
+                        for(int m=0;m<9;m++)
+                            {
+                            ttl.printf("%f,%f\n",flow(), carbon()); 
+                            difSum2+=(myArray[m]-avg2)*(myArray[m]-avg2); //Find sum of difference between value X and mean
+                            }
+                            var2=difSum2/9;
+                            sigma2=sqrt(var2);
+                        if (sigma2<0.05)
+                            {
+                            // ttl.printf("sol didnt turn on\n");
+                            for(int m=0;m<8;m++)
+                             {
+                              ttl.printf("%f,%f\n",flow(), carbon());
+                              myArray[m]=myArray[m+1];  //Shift all carbon to left by 1
+                             }
+                             myArray[8]=carbon();   
+                            }else
+                            {
+                            ttl.printf("%f,%f\n",flow(), carbon());
+                         //   ttl.printf("sol off\n");
+                            sol=0;
+                            flage=1;
+                            }
+                     }
+        solend =0;
+        bf=0;
+        fin=1;
+        }
+     if((carbon()<10000) and (fin ==1)) 
+            {
+                
+                //ttl.printf("ready\n");
+                measurement_started =0;
+                solstart=0;
+                sum=0;
+                var=0.0;
+                sigma=0.0;
+                difSum=0;
+                 sum2=0;
+                var2=0.0;
+                sigma2=0.0;
+                difSum2=0;
+                avg2=0;
+                avg=0;
+                flags=0;
+                flage=0;
+                fin=0;
+            }
+    }
+}
\ No newline at end of file