hand held final version used for coolterm

Dependencies:   mbed SoftSerial

Files at this revision

API Documentation at this revision

Comitter:
mehrnaz
Date:
Fri Jul 12 15:59:34 2019 +0000
Parent:
1:08bbc3ecd836
Commit message:
handheld final version used for coolterm

Changed in this revision

flow.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flow.h	Fri Jul 12 15:59:34 2019 +0000
@@ -0,0 +1,79 @@
+#include "mbed.h"
+
+AnalogIn flowIn(PA_0);
+Serial co2(PC_10,PC_11); //USED IN MAIN
+//Serial ttl(A1,A0);
+
+float finalflow;
+float flowVal1;
+float flowVal2;
+float P1;
+int flag=0;
+int o=0;
+float bpArray[10];
+float fp;float sp;
+float FPressure;
+int value;
+//
+float flow()
+{
+    while(1) 
+    {
+        wait(0.01);
+        flowVal1=3.3*flowIn; //Logic level 3.3
+        flowVal2 = 1.5*flowVal1; //5v
+        P1 =(125*flowVal2)-62.5;
+        if(flag==0)
+        {
+            finalflow=0;
+            bpArray[o]=P1;  
+            sp+=bpArray[o];
+            o=o+1;
+                    if (o=9)
+                    {
+                        fp=sp/10;
+                        flag=1;
+                    }            
+        }
+        if (flag==1)
+        {
+            FPressure=P1-fp;
+          finalflow=(0.24*sqrt(FPressure)); //flow in litter per min
+          //ttl.printf("Flow: %f\n",finalflow);
+          return finalflow;
+        }
+    }
+}
+
+
+int carbon()
+{
+    bool allow = false;
+    char c;
+    char co2_measure[5];
+    int count=0;
+
+    while(1) 
+    {
+        c = co2.getc();
+        if(c=='Z') {
+            allow = true;
+        }
+
+        if(allow) {
+            if(c>=48 && c<=57) {
+                co2_measure[count]=c;
+                count++;
+            }
+        
+            if(count>=6) { //NOT SURE IF 5 OR 6
+                value = ((co2_measure[0]-'0')*100000+co2_measure[1]-'0')*10000+(co2_measure[2]-'0')*1000+(co2_measure[3]-'0')*100; 
+                //ttl.printf("CO2: %d\n",value);
+                count=0;
+                allow=false;
+                return value;
+            }
+        }
+    }
+}
+
--- a/main.cpp	Tue Jun 04 14:48:16 2019 +0000
+++ b/main.cpp	Fri Jul 12 15:59:34 2019 +0000
@@ -1,28 +1,171 @@
 #include "mbed.h"
-#include <SoftSerial.h>
-
-Serial ble(A4,A5);
-AnalogIn flowIn(A3);
+#include "flow.h"
 
-float flowVal1;
-float flowVal2;
-float Pressure;
-float finalflow;
+Serial ttl(PA_2,PA_3);
+DigitalOut sol(PC_5);
 
-float flow()
+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);
+    flow();
+    carbon();
+    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("flow: %f, CO2: %d \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("Flow: %f, CO2: %d\n",flow(), carbon());
+                        myArray[m]=carbon();
+                    }
+                while(flags==0)
+                    {
+                        for(int m=0;m<9;m++)
+                            { 
+                            ttl.printf("Flow: %f, CO2: %d\n",flow(), carbon());
+                            sum+=myArray[m]; 
+                            }
+                        avg=sum/9;
+                        for(int m=0;m<9;m++)
+                            {
+                            ttl.printf("Flow: %f, CO2: %d\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("Flow: %f, CO2: %d\n",flow(), carbon());
+                              myArray[m]=myArray[m+1];  //Shift all carbon to left by 1
+                             }
+                             myArray[8]=carbon();   
+                            }else
+                            {
+                            ttl.printf("Flow: %f, CO2: %d\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("Flow: %f, CO2: %d\n",flow(), carbon());
+                        myArray[m]=carbon();
+                    }
+                while(flage==0)
+                    {
+                        for(int m=0;m<9;m++)
+                            { 
+                            ttl.printf("Flow: %f, CO2: %d\n",flow(), carbon());
+                            sum2+=myArray[m]; 
+                            }
+                        avg2=sum2/9;
+                        for(int m=0;m<9;m++)
+                            {
+                            ttl.printf("Flow: %f, CO2: %d\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("Flow: %f, CO2: %d\n",flow(), carbon());
+                              myArray[m]=myArray[m+1];  //Shift all carbon to left by 1
+                             }
+                             myArray[8]=carbon();   
+                            }else
+                            {
+                            ttl.printf("Flow: %f, CO2: %d\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