SGP30 (used in POCBreath_V2_smd_commercial)

Dependencies:   Adafruit_SGP30_mbed mbed

Files at this revision

API Documentation at this revision

Comitter:
christodoulos
Date:
Fri May 01 14:32:18 2020 +0000
Parent:
4:54dc2a95c130
Commit message:
SGP30 (used in POCBreath_V2_smd_commercial)

Changed in this revision

Adafruit_SGP30_mbed.lib Show annotated file Show diff for this revision Revisions of this file
flow.h 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/Adafruit_SGP30_mbed.lib	Fri May 01 14:32:18 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/ndrs_cwt/code/Adafruit_SGP30_mbed/#41a622cdd86d
--- a/flow.h	Fri Jul 26 09:31:58 2019 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,179 +0,0 @@
-#include "mbed.h"
-
-// First the inputs and pins are defined
-
-AnalogIn flowIn(PA_0);
-Serial co2(PC_10,PC_11);
-AnalogIn sensor1(PC_0);
-AnalogIn sensor2(PC_1);
-AnalogIn sensor3(PC_2);
-AnalogIn sensor4(PC_3);
-AnalogIn sensor5(PA_4);
-AnalogIn sensor6(PA_5);
-AnalogIn sensor7(PA_6);
-AnalogIn sensor8(PA_7);
-AnalogIn temp(PA_1);
-
-///////////////////// FLOW LOOP /////////////////////////
-float finalflow;
-float flowVal1;
-float flowVal2;
-float P1;
-int flag=0;
-int o=0;
-float bpArray[10];
-float fp;float sp;
-float FPressure;
-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; //Pressure
-        //making the value of pressure positive inside the SQRT function:
-        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 second
-          return finalflow;
-        }
-    }
-}
-
-///////////////////// CO2 LOOP /////////////////////////
-int value;
-float carbon()
-{
-    bool allow = false;
-    char c;
-    char co2_measure[5];
-    int count=0;
-
-    while(1) 
-    {
-        c = co2.getc();
-        //based on the user manual PDF for the CO2 sensor, the value starts with "Z"
-        //and we need to extract the right number of CO2 value
-        if(c=='Z') {
-            allow = true;
-        }
-
-        if(allow) {
-            if(c>=48 && c<=57) {
-                co2_measure[count]=c;
-                count++;
-            }
-        
-            if(count>=6) { 
-                value = ((co2_measure[0]-'0')*100000+co2_measure[1]-'0')*10000+(co2_measure[2]-'0')*1000+(co2_measure[3]-'0')*100; 
-                float CAR;
-                CAR=(float)value/10000;
-                count=0;
-                allow=false;
-                return CAR;
-            }
-        }
-    }
-}
-
-///////////////////// TEMPERATURE LOOP /////////////////////////
-
-float t2Cel;
-
-float getTemp()
-{
-    while(1) {
-        float B = 3478; //Define thermistor constant
-        float rRef=10e3; // Define reference resistance
-        float r1=10e3; // Define thermistor resistance at 25 C
-        float t1=25+273; // Define thermistor initial temperature s 25C in Kelvin
-        float x = temp.read(); //Measure input voltage at pin A0 in bits
-        float v = 3.3*x; //Convert bits into voltage
-        float r2 = (3.3*rRef/v)-rRef; //Convert voltage into thermistor resistance
-        float t2 = (B*t1)/(B-t1*log(r1/r2)); //Convert thermistor resistance into temperature in Kelvin (log means natural logarithm ln)
-        t2Cel = t2-273; //Convert temperature from Kelvin to Celcius
-        return t2Cel;
-//  printf("Temp: %f\n", t2Cel);
-    }
-}
-
-///////////////////// 8-CHANNEL SENSOR LOOP /////////////////////////
-
-float s1()
-{
-    float sen1;
-    while(1){
-            sen1=sensor1*3.3;
-            return sen1;
-            }
-}
-float s2()
-{
-    float sen2;
-    while(1){
-            sen2=sensor2*3.3;
-            return sen2;
-            }
-}
-float s3()
-{
-    float sen3;
-    while(1){
-            sen3=sensor3*3.3;
-            return sen3;
-            }
-}
-float s4()
-{
-    float sen4;
-    while(1){
-            sen4=sensor4*3.3;
-            return sen4;
-            }
-}
-float s5()
-{
-        float sen5;
-        while(1){
-                sen5=sensor5*3.3;
-                return sen5;
-                }
-}
-float s6()
-{
-        float sen6;
-        while(1){
-                sen6=sensor6*3.3;
-                return sen6;
-                }
-}
-float s7()
-{
-        float sen7;
-        while(1){
-                sen7=sensor7*3.3;
-                return sen7;
-                }
-}
-float s8()
-{
-        float sen8;
-        while(1){
-                sen8=sensor8*3.3;
-                return sen8;
-                }
-}
\ No newline at end of file
--- a/main.cpp	Fri Jul 26 09:31:58 2019 +0000
+++ b/main.cpp	Fri May 01 14:32:18 2020 +0000
@@ -1,222 +1,22 @@
 #include "mbed.h"
-#include "flow.h"
+#include "Adafruit_SGP30.h"
 
-/////////////////////////
-// In this version of the program developed for the Breath project, flow and CO2, as well as 8 channel sensors,
-// are measured in a separate .h file called: "flow.h" which is included in the
-// main code. So 10 sets of data is streamed using a serial connection (TTL cable or Bluetooth)
-// without any intruption. 
-// 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.
-//
-// START POINT: calculates SD for 9 samples of CO2, if it's grater than 0.02 it enables the solenoid.
-// END POINT: calculates SD for 9 samples of CO2, if it's grater thatn 0.05 it disables the solenoid.
-//
-// You can easily change the threshold of Standard deviation to detect plateau
-//
-// Generated by: Mehrnaz Javadipour
-////////////////////////// 
-
-Serial ttl(PC_12,PD_2);  //TTL cable TX,RX
-DigitalOut sol(PC_5);   //Solenoid: Digital Output
-PwmOut led(PB_6);
-
-
+Serial ble(PC_12,PD_2);
+Adafruit_SGP30 sgp30(PB_14,PB_13);
 
 int main()
 {
-    ttl.baud(9600); //baudrate for the serial connection
-    flow();         //calling flow from flow.h
-    carbon();       //calling CO2 from flow.h
-    s1();           //calling 8 channels from flow.h
-    s2();
-    s3();
-    s4();
-    s5();
-    s6();
-    s7();
-    s8();
-    getTemp();      //calling Temperature from flow.h
-    
-    //////////////////////////////
-    // I defined a flag for each section of specific functions, so by enabling the
-    // flag the section starts and by disabling the flag it finishes the section.
-    // at the end of the program, I reset the flags so it would be ready for the next loop.   
-    /////////////////////////////
-    
-    int bf=0;         //FLAG for detecting base flow
-    int i=0;
-    float bfArray[4]; //sampling flow for finding the average base flow          
-    float sf=0;       //sum of flow samples for calculating base flow
-    float fv=0;       //final value of base flow
-    
-    int measurement_started=0; //FLAG for starting calculations after detecting breath
+    ble.baud(9600);
+    sgp30.begin();
+//    uint16_t *TVOC;
+//    uint16_t *eCO2;
+    sgp30.IAQinit();
     
-    int solstart=0;         //FLAG for starting calculations for detecting plateau
-    int m=0;
-    int myArray[9];         //sampling 9 values of CO2
-    unsigned int sum=0;     //sum of 9 samples of CO2
-    int avg=0;              //average of 9 samples of CO2
-    int difSum=0;           //used for the Standard deviation algorithm
-    long double var=0.0;    //used for the Standard deviation algorithm
-    float sigma=0.0;        //final value for standar deviation
-    int flags=0;            //FLAG for keep taking samples from CO2 profile when it's too early to detect plateau
-    
-    int solend=0;           //FLAG for ending calculations for detecting plateau
-    unsigned int sum2=0;    //same as before; used for finding standard deviation
-    long double var2=0.0;
-    float sigma2=0.0;
-    int difSum2=0;
-    int avg2=0;
-    int flage=0;            //FLAG for keep taking samples from CO2 profile when it's too early to finish plateau
-    
-    int fin=0;
-    
-
-    
-    while(1)
-    {
-        led=1.00f;      //an LED is fully turned on at the beginning, the brightness will be reduced when the plateau is detected.
-        ttl.printf("%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n",getTemp(),flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8());
+    while(1){
+        sgp30.IAQmeasure();
+        wait(0.1);
+        ble.printf("eco2: %dppm, tvoc: %dppb\n",sgp30.eCO2,sgp30.TVOC);
+        wait(0.1);
+    }
 
-        if (bf==0)      //finding base flow before breath
-            {
-              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;
-             }
-        
-        //Starts calculations when it detects breathing into the device:
-        
-        if ((flow()>fv) and (measurement_started ==0)) 
-            {        
-                measurement_started = 1;
-            }
-        
-        //Starts detecting plateau:
-        
-        if ((measurement_started == 1) and (solstart==0)) 
-            {
-                //Takes 9 samples of CO2:
-                //I have also included printing the values inside the loops so we don't loose any data during calculatins.
-                for(m=0;m<9;m++)
-                    {
-                        ttl.printf("%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n",getTemp(),flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8());
-                        myArray[m]=carbon();
-                    }
-                while(flags==0)
-                    {
-                        //While "flags" is enabled, keeps calculating the standard deviation.
-                        for(int m=0;m<9;m++)
-                            { 
-                            ttl.printf("%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n",getTemp(),flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8());
-                            sum+=myArray[m]; 
-                            }
-                        avg=sum/9;
-                        for(int m=0;m<9;m++)
-                            {
-                            ttl.printf("%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n",getTemp(),flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8());
-                            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)
-                            {
-                            
-                            //if SD is less than 0.02 it means that it is too early to start the plateau
-                            //So we shift all but the first sample and define the new set of arrays:
-                            
-                            for(int m=0;m<8;m++)
-                             {
-                              ttl.printf("%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n",getTemp(),flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8());
-                              myArray[m]=myArray[m+1];  //Shift all CO2 values to the left by 1 value
-                             }
-                             myArray[8]=carbon();  //assign a new value for the 9th sample
-                            }
-                             //The new set of arrays are now generated and is sent back to be used for preveious SD calculations.
-                             //If sigma for the new set is still small, a newer set will be generated and replaced
-                             //Otherwise, it's accepted and will turn on the solenoid:
-                            else
-                            {
-                            ttl.printf("%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n",getTemp(),flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8());
-                            sol=1;      //Solenoid is ON
-                            led=0.50f;  //The brightness is reduced to half during the plateau
-                            flags=1;    //breakes the while loop
-                            }
-                     }
-                solend=1;   //prepares the next section for finishing the plateau
-                solstart =1;
-            }
-        if ((measurement_started == 1) and (solend==1)) 
-        {
-          // same process happens for finishing the plateau:
-        
-                for(m=0;m<9;m++)
-                    {
-                        ttl.printf("%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n",getTemp(),flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8());
-                        myArray[m]=carbon();
-                    }
-                while(flage==0)
-                    {
-                        for(int m=0;m<9;m++)
-                            { 
-                            ttl.printf("%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n",getTemp(),flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8());
-                            sum2+=myArray[m]; 
-                            }
-                        avg2=sum2/9;
-                        for(int m=0;m<9;m++)
-                            {
-                            ttl.printf("%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n",getTemp(),flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8());
-                            difSum2+=(myArray[m]-avg2)*(myArray[m]-avg2); 
-                            }
-                            var2=difSum2/9;
-                            sigma2=sqrt(var2);
-                        if (sigma2<0.05)
-                            {
-                            // here we defined the end threshold to be 0.05, it can be changed later based on experiment results
-                            for(int m=0;m<8;m++)
-                             {
-                              ttl.printf("%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n",getTemp(),flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8());
-                              myArray[m]=myArray[m+1];  
-                             }
-                             myArray[8]=carbon();   
-                            }else
-                            {
-                            ttl.printf("%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n",getTemp(),flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8());
-                            sol=0;  //Solenoid is OFF
-                            flage=1;    //breakes the loop
-                            }
-                     }
-        solend =0;  //end of this section
-        led=1.00f;  //LED is back to full brightness
-        bf=0;       //reset the detecting base flow flag
-        fin=1;      //enables the next section flag
-        }
-     if((carbon()<10000) and (fin ==1)) 
-            {
-                //User has to wait for the CO2 level to drop less than 1% before testing again.
-                //Once it is less than 1%, all the flags and parameters used in calculations are reset
-                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