CCS811 (NOT USED in POCBreath_V2_smd_commercial)

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
christodoulos
Date:
Fri May 01 14:30:55 2020 +0000
Parent:
4:54dc2a95c130
Commit message:
CCS811 (NOT USED in POCBreath_V2_smd_commercial)

Changed in this revision

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
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- 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:30:55 2020 +0000
@@ -1,222 +1,79 @@
 #include "mbed.h"
-#include "flow.h"
+ 
+Serial pc(PC_12, PD_2);
+I2C i2c(PC_1,PC_0);
+DigitalOut wake(PB_2);
 
-/////////////////////////
-// 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
-////////////////////////// 
+uint16_t eco2, tvoc;
 
-Serial ttl(PC_12,PD_2);  //TTL cable TX,RX
-DigitalOut sol(PC_5);   //Solenoid: Digital Output
-PwmOut led(PB_6);
-
-
-
+ 
 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
+    int addr=0xB4;
+    wake=0;
+    // FIRST NEED TO INIT TO APP_START 
+    //CHECK SYSTEM 1, PUT IN APP MODE, CECK SYSTEM 2, SET MODE
     
-    //////////////////////////////
-    // 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
+    char wID[1];
+    wID[0]=0x20;
+    char rID[1];
     
-    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
+    i2c.write(addr,wID,1);
+    i2c.read(addr,rID,1);
+    pc.printf("ID: %x\n",rID[0]);
+    wait(1);
+
+    char wS[1];
+    wS[0]=0x00;
+    char rS[1];
     
-    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());
+    i2c.write(addr,wS,1);
+    i2c.read(addr,rS,1);
+    pc.printf("Status before init: %x\n",rS[0]);
+    wait(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;
-            }
+    char wInit[2];
+    wInit[0]=0xF4;
+//    wInit[1]=0x00;
+//    char rInit[1];
+    
+    i2c.write(addr,wInit,1);
+    wait(1);
+    
+    i2c.write(addr,wS,1);
+    i2c.read(addr,rS,1);
+    pc.printf("Status after init: %x\n",rS[0]);
+    wait(2);
+    
+    char wMode[2];
+    wMode[0]=0x01;
+    wMode[1]=0x40;
+    char rMode[1];
+    
+    i2c.write(addr,wMode,2);
+    i2c.read(addr,rMode,1);
+    pc.printf("Mode: %x\n",rMode[0]);
+    wait(1);
+    
+    char wE[1];
+    wE[0]=0xE0;
+    char rE[1];
+    
+    char wData[1];
+    wData[0]=0x02;
+    char rData[6];
+    
+    
+    while(1){
+        i2c.write(addr,wData,1); //co2 and tvoc
+        i2c.read(addr,rData,6);
+                
+        int CO2=(rData[0]<<8)+rData[1];
+        int TVOC=(rData[2]<<8)+rData[3];
+
+
+        pc.printf("CO2: %i ppm, TVOC: %i ppb,Status: %x, Error: %x\n",CO2,TVOC,rData[4],rData[5]);
+        wait(0.1);
     }
+    
 }
\ No newline at end of file
--- a/mbed.bld	Fri Jul 26 09:31:58 2019 +0000
+++ b/mbed.bld	Fri May 01 14:30:55 2020 +0000
@@ -1,1 +1,1 @@
-https://os.mbed.com/users/mbed_official/code/mbed/builds/3a7713b1edbc
\ No newline at end of file
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file