Exluding ZMOD

Dependencies:   iAQ_Core Adafruit_SGP30_mbed mbed BME680

main.cpp

Committer:
mehrnaz
Date:
2019-07-12
Revision:
2:ef98576cd67b
Parent:
1:08bbc3ecd836
Child:
3:3d51f8870e91

File content as of revision 2:ef98576cd67b:

#include "mbed.h"
#include "flow.h"

/////////////////////////
// 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
////////////////////////// 

Serial ttl(PA_2,PA_3);  //TTL cable TX,RX
DigitalOut sol(PC_5);   //Solenoid: Digital Output

int main()
{
    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;
            }
    }
}