POC Breath using SMD commercial sensors

Dependencies:   iAQ_Core Adafruit_SGP30_mbed mbed BME680

Committer:
christodoulos
Date:
Sun Jun 14 15:13:58 2020 +0000
Revision:
9:81d1b4833516
Parent:
8:976c405a1da5
Child:
10:aad246b57873
CO2 SD in 5 samples

Who changed what in which revision?

UserRevisionLine numberNew contents of line
christodoulos 0:d034cdad5b6d 1 #include "mbed.h"
mehrnaz 2:ef98576cd67b 2 #include "flow.h"
christodoulos 5:646a7e58989e 3 #include <math.h>
christodoulos 7:f37620a76a1d 4 #include "iAQ_Core.h"
christodoulos 7:f37620a76a1d 5 #include "Adafruit_SGP30.h"
christodoulos 7:f37620a76a1d 6 #include"BME680.h"
mehrnaz 2:ef98576cd67b 7 /////////////////////////
mehrnaz 3:3d51f8870e91 8 // In this version of the program developed for the Breath project, flow and CO2, as well as 8 channel sensors,
mehrnaz 2:ef98576cd67b 9 // are measured in a separate .h file called: "flow.h" which is included in the
mehrnaz 3:3d51f8870e91 10 // main code. So 10 sets of data is streamed using a serial connection (TTL cable or Bluetooth)
christodoulos 5:646a7e58989e 11 // without any intruption.
mehrnaz 2:ef98576cd67b 12 // This version is especially suitable to be used for KST.
mehrnaz 2:ef98576cd67b 13 // Also, a solenoid would be turned on and off based on calculating the standard deviation in CO2 profile.
mehrnaz 2:ef98576cd67b 14 //
mehrnaz 3:3d51f8870e91 15 // START POINT: calculates SD for 9 samples of CO2, if it's grater than 0.02 it enables the solenoid.
mehrnaz 3:3d51f8870e91 16 // END POINT: calculates SD for 9 samples of CO2, if it's grater thatn 0.05 it disables the solenoid.
mehrnaz 3:3d51f8870e91 17 //
mehrnaz 3:3d51f8870e91 18 // You can easily change the threshold of Standard deviation to detect plateau
mehrnaz 3:3d51f8870e91 19 //
mehrnaz 2:ef98576cd67b 20 // Generated by: Mehrnaz Javadipour
christodoulos 5:646a7e58989e 21 //////////////////////////
christodoulos 0:d034cdad5b6d 22
mehrnaz 3:3d51f8870e91 23 Serial ttl(PC_12,PD_2); //TTL cable TX,RX
mehrnaz 2:ef98576cd67b 24 DigitalOut sol(PC_5); //Solenoid: Digital Output
mehrnaz 3:3d51f8870e91 25 PwmOut led(PB_6);
christodoulos 5:646a7e58989e 26 Timer stream;
mehrnaz 2:ef98576cd67b 27
christodoulos 7:f37620a76a1d 28 ///SENSOR SETUP///
christodoulos 7:f37620a76a1d 29 BME680 myBME680 ( PC_1,PC_0, 400000 ); //BME680
christodoulos 7:f37620a76a1d 30 iAQ_Core myiAQ_Core ( PB_11_ALT0,PB_10_ALT0, iAQ_Core::iAQ_Core_ADDRESS ); //iAQ-Core C
christodoulos 7:f37620a76a1d 31 Adafruit_SGP30 sgp30(PB_14,PB_13); // SGP30
christodoulos 7:f37620a76a1d 32 I2C ZMODtemp(PB_9, PB_8); //ZMOD and Si7050
christodoulos 7:f37620a76a1d 33
christodoulos 7:f37620a76a1d 34 iAQ_Core::iAQ_Core_status_t aux;
christodoulos 7:f37620a76a1d 35 iAQ_Core::iAQ_Core_data_t myiAQ_Core_data;
christodoulos 7:f37620a76a1d 36
christodoulos 7:f37620a76a1d 37 ///BME680 EXTRA FUNCTIONS///
christodoulos 7:f37620a76a1d 38 Ticker newReading;
christodoulos 7:f37620a76a1d 39
christodoulos 7:f37620a76a1d 40 uint32_t myState = 0;
christodoulos 7:f37620a76a1d 41
christodoulos 7:f37620a76a1d 42 //@brief FUNCTION PROTOTYPES
christodoulos 7:f37620a76a1d 43 void changeDATA ( void );
christodoulos 7:f37620a76a1d 44 void user_delay_ms ( uint32_t period );
christodoulos 7:f37620a76a1d 45 int8_t user_i2c_read ( uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len );
christodoulos 7:f37620a76a1d 46 int8_t user_i2c_write ( uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len );
christodoulos 7:f37620a76a1d 47
christodoulos 7:f37620a76a1d 48 void changeDATA ( void )
christodoulos 7:f37620a76a1d 49 {
christodoulos 7:f37620a76a1d 50 myState = 1;
christodoulos 7:f37620a76a1d 51 }
christodoulos 7:f37620a76a1d 52
christodoulos 7:f37620a76a1d 53 void user_delay_ms ( uint32_t period )
christodoulos 7:f37620a76a1d 54 {
christodoulos 7:f37620a76a1d 55 wait( 0.01 );
christodoulos 7:f37620a76a1d 56 }
christodoulos 7:f37620a76a1d 57
christodoulos 7:f37620a76a1d 58 int8_t user_i2c_read ( uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len )
christodoulos 7:f37620a76a1d 59 {
christodoulos 7:f37620a76a1d 60 int8_t rslt = 0; // Return 0 for Success, non-zero for failure
christodoulos 7:f37620a76a1d 61
christodoulos 7:f37620a76a1d 62 uint32_t aux = 0;
christodoulos 7:f37620a76a1d 63 aux = myBME680._i2c.write ( dev_id, (char*)&reg_addr, 1, true );
christodoulos 7:f37620a76a1d 64 aux = myBME680._i2c.read ( dev_id, (char*)&reg_data[0], len );
christodoulos 7:f37620a76a1d 65
christodoulos 7:f37620a76a1d 66
christodoulos 7:f37620a76a1d 67
christodoulos 7:f37620a76a1d 68 if ( aux == 0 ) {
christodoulos 7:f37620a76a1d 69 rslt = 0;
christodoulos 7:f37620a76a1d 70 } else {
christodoulos 7:f37620a76a1d 71 rslt = 0xFF;
christodoulos 7:f37620a76a1d 72 }
christodoulos 7:f37620a76a1d 73
christodoulos 7:f37620a76a1d 74
christodoulos 7:f37620a76a1d 75 return rslt;
christodoulos 7:f37620a76a1d 76 }
christodoulos 7:f37620a76a1d 77
christodoulos 7:f37620a76a1d 78 int8_t user_i2c_write ( uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len )
christodoulos 7:f37620a76a1d 79 {
christodoulos 7:f37620a76a1d 80 int8_t rslt = 0; // Return 0 for Success, non-zero for failure
christodoulos 7:f37620a76a1d 81
christodoulos 7:f37620a76a1d 82 uint32_t aux = 0;
christodoulos 7:f37620a76a1d 83 char cmd[16] = { 0 };
christodoulos 7:f37620a76a1d 84 uint32_t i = 0;
christodoulos 7:f37620a76a1d 85
christodoulos 7:f37620a76a1d 86 // Prepare the data to be sent
christodoulos 7:f37620a76a1d 87 cmd[0] = reg_addr;
christodoulos 7:f37620a76a1d 88 for ( i = 1; i <= len; i++ ) {
christodoulos 7:f37620a76a1d 89 cmd[i] = reg_data[i - 1];
christodoulos 7:f37620a76a1d 90 }
christodoulos 7:f37620a76a1d 91
christodoulos 7:f37620a76a1d 92 // Write data
christodoulos 7:f37620a76a1d 93 aux = myBME680._i2c.write ( dev_id, &cmd[0], len + 1, false );
christodoulos 7:f37620a76a1d 94
christodoulos 7:f37620a76a1d 95
christodoulos 7:f37620a76a1d 96
christodoulos 7:f37620a76a1d 97 if ( aux == 0 ) {
christodoulos 7:f37620a76a1d 98 rslt = 0;
christodoulos 7:f37620a76a1d 99 } else {
christodoulos 7:f37620a76a1d 100 rslt = 0xFF;
christodoulos 7:f37620a76a1d 101 }
christodoulos 7:f37620a76a1d 102
christodoulos 7:f37620a76a1d 103
christodoulos 7:f37620a76a1d 104 return rslt;
christodoulos 7:f37620a76a1d 105 }
christodoulos 7:f37620a76a1d 106 ///END SENSOR SETUP///
christodoulos 7:f37620a76a1d 107
mehrnaz 2:ef98576cd67b 108 int main()
christodoulos 0:d034cdad5b6d 109 {
christodoulos 5:646a7e58989e 110 ttl.baud(115200); //baudrate for the serial connection, 9600 for hc05 115200 for rn
christodoulos 5:646a7e58989e 111
christodoulos 5:646a7e58989e 112 ttl.printf("$");//enter command mode only for rn
christodoulos 7:f37620a76a1d 113 wait(0.5);
christodoulos 5:646a7e58989e 114 ttl.printf("$$");//enter command mode
christodoulos 5:646a7e58989e 115 wait(0.5);
christodoulos 8:976c405a1da5 116 ttl.printf("SN,POC Breath v3\r");//set new name
christodoulos 5:646a7e58989e 117 wait(0.5);
christodoulos 5:646a7e58989e 118 ttl.printf("SS,C0\r");//set transparent uart
christodoulos 5:646a7e58989e 119 wait(0.5);
christodoulos 7:f37620a76a1d 120 ttl.printf("&,86DA9DA1FBA2\r");//Assign mac
christodoulos 6:f6faf142e5fc 121 wait(0.5);
christodoulos 5:646a7e58989e 122 ttl.printf("---\r");//enter data mode
christodoulos 5:646a7e58989e 123 wait(0.5);
christodoulos 5:646a7e58989e 124
christodoulos 7:f37620a76a1d 125 ///SENSOR INITIALISATION///
christodoulos 7:f37620a76a1d 126
christodoulos 7:f37620a76a1d 127 ///IAQ DOESN'T REQUIRE INITIALISATION///
christodoulos 7:f37620a76a1d 128
christodoulos 7:f37620a76a1d 129 ///SGP30///
christodoulos 7:f37620a76a1d 130 sgp30.begin();//begin sgp30
christodoulos 7:f37620a76a1d 131 sgp30.IAQinit();//initialise sgp30
christodoulos 7:f37620a76a1d 132
christodoulos 7:f37620a76a1d 133
christodoulos 7:f37620a76a1d 134
christodoulos 7:f37620a76a1d 135 ///BME680//
christodoulos 7:f37620a76a1d 136 struct bme680_dev gas_sensor;
christodoulos 7:f37620a76a1d 137
christodoulos 7:f37620a76a1d 138 gas_sensor.dev_id = BME680_I2C_ADDR_PRIMARY;
christodoulos 7:f37620a76a1d 139 gas_sensor.intf = BME680_I2C_INTF;
christodoulos 7:f37620a76a1d 140 gas_sensor.read = user_i2c_read;
christodoulos 7:f37620a76a1d 141 gas_sensor.write = user_i2c_write;
christodoulos 7:f37620a76a1d 142 gas_sensor.delay_ms = user_delay_ms;
christodoulos 7:f37620a76a1d 143 // amb_temp can be set to 25 prior to configuring the gas sensor
christodoulos 7:f37620a76a1d 144 // or by performing a few temperature readings without operating the gas sensor.
christodoulos 7:f37620a76a1d 145 gas_sensor.amb_temp = 25;
christodoulos 7:f37620a76a1d 146
christodoulos 7:f37620a76a1d 147
christodoulos 7:f37620a76a1d 148 int8_t rslt = BME680_OK;
christodoulos 7:f37620a76a1d 149 rslt = myBME680.bme680_init ( &gas_sensor );
christodoulos 7:f37620a76a1d 150
christodoulos 7:f37620a76a1d 151
christodoulos 7:f37620a76a1d 152 uint8_t set_required_settings;
christodoulos 7:f37620a76a1d 153
christodoulos 7:f37620a76a1d 154 // Set the temperature, pressure and humidity settings
christodoulos 7:f37620a76a1d 155 gas_sensor.tph_sett.os_hum = BME680_OS_2X;
christodoulos 7:f37620a76a1d 156 gas_sensor.tph_sett.os_pres = BME680_OS_4X;
christodoulos 7:f37620a76a1d 157 gas_sensor.tph_sett.os_temp = BME680_OS_8X;
christodoulos 7:f37620a76a1d 158 gas_sensor.tph_sett.filter = BME680_FILTER_SIZE_3;
christodoulos 7:f37620a76a1d 159
christodoulos 7:f37620a76a1d 160 // Set the remaining gas sensor settings and link the heating profile
christodoulos 7:f37620a76a1d 161 gas_sensor.gas_sett.run_gas = BME680_ENABLE_GAS_MEAS;
christodoulos 7:f37620a76a1d 162 // Create a ramp heat waveform in 3 steps
christodoulos 7:f37620a76a1d 163 gas_sensor.gas_sett.heatr_temp = 320; // degree Celsius
christodoulos 7:f37620a76a1d 164 gas_sensor.gas_sett.heatr_dur = 150; // milliseconds
christodoulos 7:f37620a76a1d 165
christodoulos 7:f37620a76a1d 166 // Select the power mode
christodoulos 7:f37620a76a1d 167 // Must be set before writing the sensor configuration
christodoulos 7:f37620a76a1d 168 gas_sensor.power_mode = BME680_FORCED_MODE;
christodoulos 7:f37620a76a1d 169
christodoulos 7:f37620a76a1d 170 // Set the required sensor settings needed
christodoulos 7:f37620a76a1d 171 set_required_settings = BME680_OST_SEL | BME680_OSP_SEL | BME680_OSH_SEL | BME680_FILTER_SEL | BME680_GAS_SENSOR_SEL;
christodoulos 7:f37620a76a1d 172
christodoulos 7:f37620a76a1d 173 // Set the desired sensor configuration
christodoulos 7:f37620a76a1d 174 rslt = myBME680.bme680_set_sensor_settings ( set_required_settings, &gas_sensor );
christodoulos 7:f37620a76a1d 175
christodoulos 7:f37620a76a1d 176 // Set the power mode
christodoulos 7:f37620a76a1d 177 rslt = myBME680.bme680_set_sensor_mode ( &gas_sensor );
christodoulos 7:f37620a76a1d 178
christodoulos 7:f37620a76a1d 179
christodoulos 7:f37620a76a1d 180 // Get the total measurement duration so as to sleep or wait till the measurement is complete
christodoulos 7:f37620a76a1d 181 uint16_t meas_period;
christodoulos 7:f37620a76a1d 182 myBME680.bme680_get_profile_dur ( &meas_period, &gas_sensor );
christodoulos 7:f37620a76a1d 183
christodoulos 7:f37620a76a1d 184 struct bme680_field_data data;
christodoulos 7:f37620a76a1d 185
christodoulos 7:f37620a76a1d 186
christodoulos 7:f37620a76a1d 187 newReading.attach( &changeDATA, 1 ); // the address of the function to be attached ( changeDATA ) and the interval ( 1s )
christodoulos 7:f37620a76a1d 188
christodoulos 7:f37620a76a1d 189 //END BME680///
christodoulos 7:f37620a76a1d 190
christodoulos 7:f37620a76a1d 191 ///ZMOD AND TEMP///
christodoulos 7:f37620a76a1d 192 int ZMODTEMPaddr=0x80; //si7050 8bit address
christodoulos 7:f37620a76a1d 193
christodoulos 7:f37620a76a1d 194 char wTemp[1];
christodoulos 7:f37620a76a1d 195 wTemp[0]=0xE3; //Hold master mode
christodoulos 7:f37620a76a1d 196 char rTemp[2]; //Temperature returns MSB and LSB
christodoulos 7:f37620a76a1d 197 //assume MBS in rTemp[0] and LSB in rTemp[1]
christodoulos 7:f37620a76a1d 198
christodoulos 7:f37620a76a1d 199 char wInit[2];
christodoulos 7:f37620a76a1d 200 wInit[0]=0xE6; //User register 1
christodoulos 7:f37620a76a1d 201 wInit[1]=0x00; //Set 14 bit resolution, other read-ony registers shouldn't be affected
christodoulos 7:f37620a76a1d 202
christodoulos 7:f37620a76a1d 203 ZMODtemp.write(ZMODTEMPaddr,wInit,2);
christodoulos 7:f37620a76a1d 204 ///END ZMOD AND TEMP///
christodoulos 7:f37620a76a1d 205
christodoulos 7:f37620a76a1d 206 ///END SENSOR INITIALISATION///
mehrnaz 2:ef98576cd67b 207 flow(); //calling flow from flow.h
mehrnaz 2:ef98576cd67b 208 carbon(); //calling CO2 from flow.h
mehrnaz 3:3d51f8870e91 209 s1(); //calling 8 channels from flow.h
mehrnaz 3:3d51f8870e91 210 s2();
mehrnaz 3:3d51f8870e91 211 s3();
mehrnaz 3:3d51f8870e91 212 s4();
mehrnaz 3:3d51f8870e91 213 s5();
mehrnaz 3:3d51f8870e91 214 s6();
mehrnaz 3:3d51f8870e91 215 s7();
mehrnaz 3:3d51f8870e91 216 s8();
mehrnaz 3:3d51f8870e91 217 getTemp(); //calling Temperature from flow.h
christodoulos 5:646a7e58989e 218
mehrnaz 3:3d51f8870e91 219 //////////////////////////////
mehrnaz 3:3d51f8870e91 220 // I defined a flag for each section of specific functions, so by enabling the
mehrnaz 3:3d51f8870e91 221 // flag the section starts and by disabling the flag it finishes the section.
christodoulos 5:646a7e58989e 222 // at the end of the program, I reset the flags so it would be ready for the next loop.
mehrnaz 3:3d51f8870e91 223 /////////////////////////////
christodoulos 5:646a7e58989e 224
mehrnaz 3:3d51f8870e91 225 int bf=0; //FLAG for detecting base flow
mehrnaz 2:ef98576cd67b 226 int i=0;
christodoulos 5:646a7e58989e 227 float bfArray[4]; //sampling flow for finding the average base flow
mehrnaz 3:3d51f8870e91 228 float sf=0; //sum of flow samples for calculating base flow
mehrnaz 3:3d51f8870e91 229 float fv=0; //final value of base flow
christodoulos 5:646a7e58989e 230
mehrnaz 3:3d51f8870e91 231 int measurement_started=0; //FLAG for starting calculations after detecting breath
christodoulos 5:646a7e58989e 232
mehrnaz 3:3d51f8870e91 233 int solstart=0; //FLAG for starting calculations for detecting plateau
mehrnaz 3:3d51f8870e91 234 int m=0;
christodoulos 9:81d1b4833516 235 int myArray[5]; //sampling 5 values of CO2
christodoulos 9:81d1b4833516 236 unsigned int sum=0; //sum of 5 samples of CO2
christodoulos 9:81d1b4833516 237 int avg=0; //average of 5 samples of CO2
mehrnaz 3:3d51f8870e91 238 int difSum=0; //used for the Standard deviation algorithm
mehrnaz 3:3d51f8870e91 239 long double var=0.0; //used for the Standard deviation algorithm
mehrnaz 3:3d51f8870e91 240 float sigma=0.0; //final value for standar deviation
mehrnaz 3:3d51f8870e91 241 int flags=0; //FLAG for keep taking samples from CO2 profile when it's too early to detect plateau
christodoulos 5:646a7e58989e 242
mehrnaz 3:3d51f8870e91 243 int solend=0; //FLAG for ending calculations for detecting plateau
mehrnaz 3:3d51f8870e91 244 unsigned int sum2=0; //same as before; used for finding standard deviation
mehrnaz 2:ef98576cd67b 245 long double var2=0.0;
mehrnaz 2:ef98576cd67b 246 float sigma2=0.0;
mehrnaz 2:ef98576cd67b 247 int difSum2=0;
mehrnaz 3:3d51f8870e91 248 int avg2=0;
mehrnaz 3:3d51f8870e91 249 int flage=0; //FLAG for keep taking samples from CO2 profile when it's too early to finish plateau
christodoulos 5:646a7e58989e 250
mehrnaz 2:ef98576cd67b 251 int fin=0;
christodoulos 6:f6faf142e5fc 252 wait(1);
christodoulos 6:f6faf142e5fc 253 sol=1;//sol off 1
christodoulos 5:646a7e58989e 254
christodoulos 5:646a7e58989e 255 while(1) {
christodoulos 7:f37620a76a1d 256
christodoulos 6:f6faf142e5fc 257 //led=0.4f; //an LED is fully turned on at the beginning, the brightness will be reduced when the plateau is detected.
christodoulos 5:646a7e58989e 258 //ttl.printf("tt%.2f,ff%.2f,cc%.2f,sa%i,sb%i,sc%i,sd%i,se%i,sf%i,sg%i,sh%i,ti%.2f\n",getTemp(),flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8(),stream.read()); //chemical sensor
christodoulos 7:f37620a76a1d 259
christodoulos 7:f37620a76a1d 260 myiAQ_Core.iAQ_Core_GetNewReading ( &myiAQ_Core_data ); //Measurement from iAQ-Core C
christodoulos 7:f37620a76a1d 261 wait(0.1);
christodoulos 7:f37620a76a1d 262 sgp30.IAQmeasure();//Measurement from SGP30
christodoulos 7:f37620a76a1d 263 rslt = myBME680.bme680_get_sensor_data ( &data, &gas_sensor ); //Measurement for BME680
christodoulos 7:f37620a76a1d 264 ZMODtemp.write(ZMODTEMPaddr,wInit,2); //maybe?
christodoulos 7:f37620a76a1d 265 ZMODtemp.write(ZMODTEMPaddr,wTemp,1);
christodoulos 7:f37620a76a1d 266 ZMODtemp.read(ZMODTEMPaddr,rTemp,2); //Returns 2 bytes
christodoulos 7:f37620a76a1d 267 float temp_code=(rTemp[0]<<8)+rTemp[1];
christodoulos 7:f37620a76a1d 268 float SiTemp=((175.72*temp_code)/65536)-46.85;
christodoulos 7:f37620a76a1d 269 ttl.printf("tt%.2f,ff%.2f,cc%.2f,sa%i,sb%i,sc%i,sd%i,se%i,sf%i,sg%i,sh%i,ti%.2f,ic%d,iv%d,ir%d,gc%d,gv%d,br%d,bt%.2f,bh%.2f,bp%.2f,zi%f,zh%f,zc%f,st%.2f\n",0.00,flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8(),stream.read(),myiAQ_Core_data.pred, myiAQ_Core_data.Tvoc, myiAQ_Core_data.resistance, sgp30.eCO2,sgp30.TVOC, data.gas_resistance,( data.temperature/100.0f ), ( data.humidity / 1000.0f ), ( data.pressure / 100.0f ),0.00,0.00,0.00,SiTemp); //Results after 5 mins of ON
christodoulos 7:f37620a76a1d 270 rslt = myBME680.bme680_set_sensor_mode ( &gas_sensor ); //BME680 reset
christodoulos 8:976c405a1da5 271 wait(0.1);
christodoulos 8:976c405a1da5 272
christodoulos 5:646a7e58989e 273 if (bf==0) { //finding base flow before breath
christodoulos 5:646a7e58989e 274 for(i=0; i<4; i++) {
christodoulos 5:646a7e58989e 275 bfArray[i]=flow();
christodoulos 5:646a7e58989e 276 sf+=bfArray[i];
christodoulos 5:646a7e58989e 277 }
christodoulos 5:646a7e58989e 278 fv=sf/4;
christodoulos 6:f6faf142e5fc 279 //fv=fv+0.2;
christodoulos 8:976c405a1da5 280 fv=0.4;
christodoulos 5:646a7e58989e 281 //ttl.printf("set\n");
christodoulos 5:646a7e58989e 282 bf=1;
christodoulos 5:646a7e58989e 283 }
christodoulos 5:646a7e58989e 284
christodoulos 5:646a7e58989e 285 //Starts calculations when it detects breathing into the device:
christodoulos 5:646a7e58989e 286
christodoulos 5:646a7e58989e 287 if ((flow()>fv) and (measurement_started ==0)) {
christodoulos 7:f37620a76a1d 288 stream.start();
christodoulos 5:646a7e58989e 289 measurement_started = 1;
christodoulos 5:646a7e58989e 290 }
christodoulos 5:646a7e58989e 291
christodoulos 5:646a7e58989e 292 //Starts detecting plateau:
christodoulos 5:646a7e58989e 293
christodoulos 5:646a7e58989e 294 if ((measurement_started == 1) and (solstart==0)) {
mehrnaz 3:3d51f8870e91 295
christodoulos 5:646a7e58989e 296 //Takes 9 samples of CO2:
christodoulos 5:646a7e58989e 297 //I have also included printing the values inside the loops so we don't loose any data during calculatins.
christodoulos 9:81d1b4833516 298 for(m=0; m<4; m++) {
christodoulos 5:646a7e58989e 299 //ttl.printf("tt%.2f,ff%.2f,cc%.2f,sa%i,sb%i,sc%i,sd%i,se%i,sf%i,sg%i,sh%i,ti%.2f\n",getTemp(),flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8(),stream.read()); //chemical sensor
christodoulos 7:f37620a76a1d 300 myiAQ_Core.iAQ_Core_GetNewReading ( &myiAQ_Core_data ); //Measurement from iAQ-Core C
christodoulos 7:f37620a76a1d 301 wait(0.1);
christodoulos 7:f37620a76a1d 302 sgp30.IAQmeasure();//Measurement from SGP30
christodoulos 7:f37620a76a1d 303 rslt = myBME680.bme680_get_sensor_data ( &data, &gas_sensor ); //Measurement for BME680
christodoulos 7:f37620a76a1d 304 ZMODtemp.write(ZMODTEMPaddr,wInit,2); //maybe?
christodoulos 7:f37620a76a1d 305 ZMODtemp.write(ZMODTEMPaddr,wTemp,1);
christodoulos 7:f37620a76a1d 306 ZMODtemp.read(ZMODTEMPaddr,rTemp,2); //Returns 2 bytes
christodoulos 7:f37620a76a1d 307 float temp_code=(rTemp[0]<<8)+rTemp[1];
christodoulos 7:f37620a76a1d 308 float SiTemp=((175.72*temp_code)/65536)-46.85;
christodoulos 7:f37620a76a1d 309 ttl.printf("tt%.2f,ff%.2f,cc%.2f,sa%i,sb%i,sc%i,sd%i,se%i,sf%i,sg%i,sh%i,ti%.2f,ic%d,iv%d,ir%d,gc%d,gv%d,br%d,bt%.2f,bh%.2f,bp%.2f,zi%f,zh%f,zc%f,st%.2f\n",0.00,flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8(),stream.read(),myiAQ_Core_data.pred, myiAQ_Core_data.Tvoc, myiAQ_Core_data.resistance, sgp30.eCO2,sgp30.TVOC, data.gas_resistance,( data.temperature/100.0f ), ( data.humidity / 1000.0f ), ( data.pressure / 100.0f ),0.00,0.00,0.00,SiTemp); //Results after 5 mins of ON
christodoulos 7:f37620a76a1d 310 rslt = myBME680.bme680_set_sensor_mode ( &gas_sensor ); //BME680 reset
christodoulos 8:976c405a1da5 311 wait(0.1);
christodoulos 8:976c405a1da5 312
christodoulos 5:646a7e58989e 313 myArray[m]=carbon();
christodoulos 5:646a7e58989e 314 wait(0.1);
mehrnaz 2:ef98576cd67b 315 }
christodoulos 5:646a7e58989e 316 while(flags==0) {
christodoulos 5:646a7e58989e 317 //While "flags" is enabled, keeps calculating the standard deviation.
christodoulos 9:81d1b4833516 318 for(int m=0; m<4; m++) {
christodoulos 5:646a7e58989e 319 //ttl.printf("tt%.2f,ff%.2f,cc%.2f,sa%i,sb%i,sc%i,sd%i,se%i,sf%i,sg%i,sh%i,ti%.2f\n",getTemp(),flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8(),stream.read()); //chemical sensor
christodoulos 7:f37620a76a1d 320 myiAQ_Core.iAQ_Core_GetNewReading ( &myiAQ_Core_data ); //Measurement from iAQ-Core C
christodoulos 7:f37620a76a1d 321 wait(0.1);
christodoulos 7:f37620a76a1d 322 sgp30.IAQmeasure();//Measurement from SGP30
christodoulos 7:f37620a76a1d 323 rslt = myBME680.bme680_get_sensor_data ( &data, &gas_sensor ); //Measurement for BME680
christodoulos 7:f37620a76a1d 324 ZMODtemp.write(ZMODTEMPaddr,wInit,2); //maybe?
christodoulos 7:f37620a76a1d 325 ZMODtemp.write(ZMODTEMPaddr,wTemp,1);
christodoulos 7:f37620a76a1d 326 ZMODtemp.read(ZMODTEMPaddr,rTemp,2); //Returns 2 bytes
christodoulos 7:f37620a76a1d 327 float temp_code=(rTemp[0]<<8)+rTemp[1];
christodoulos 7:f37620a76a1d 328 float SiTemp=((175.72*temp_code)/65536)-46.85;
christodoulos 7:f37620a76a1d 329 ttl.printf("tt%.2f,ff%.2f,cc%.2f,sa%i,sb%i,sc%i,sd%i,se%i,sf%i,sg%i,sh%i,ti%.2f,ic%d,iv%d,ir%d,gc%d,gv%d,br%d,bt%.2f,bh%.2f,bp%.2f,zi%f,zh%f,zc%f,st%.2f\n",0.00,flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8(),stream.read(),myiAQ_Core_data.pred, myiAQ_Core_data.Tvoc, myiAQ_Core_data.resistance, sgp30.eCO2,sgp30.TVOC, data.gas_resistance,( data.temperature/100.0f ), ( data.humidity / 1000.0f ), ( data.pressure / 100.0f ),0.00,0.00,0.00,SiTemp); //Results after 5 mins of ON
christodoulos 7:f37620a76a1d 330 rslt = myBME680.bme680_set_sensor_mode ( &gas_sensor ); //BME680 reset
christodoulos 8:976c405a1da5 331 wait(0.1);
christodoulos 5:646a7e58989e 332 sum+=myArray[m];
christodoulos 5:646a7e58989e 333 wait(0.1);
christodoulos 5:646a7e58989e 334
christodoulos 5:646a7e58989e 335 }
christodoulos 9:81d1b4833516 336 avg=sum/4;
christodoulos 9:81d1b4833516 337 for(int m=0; m<4; m++) {
christodoulos 5:646a7e58989e 338 //ttl.printf("tt%.2f,ff%.2f,cc%.2f,sa%i,sb%i,sc%i,sd%i,se%i,sf%i,sg%i,sh%i,ti%.2f\n",getTemp(),flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8(),stream.read()); //chemical sensor
christodoulos 7:f37620a76a1d 339 myiAQ_Core.iAQ_Core_GetNewReading ( &myiAQ_Core_data ); //Measurement from iAQ-Core C
christodoulos 7:f37620a76a1d 340 wait(0.1);
christodoulos 7:f37620a76a1d 341 sgp30.IAQmeasure();//Measurement from SGP30
christodoulos 7:f37620a76a1d 342 rslt = myBME680.bme680_get_sensor_data ( &data, &gas_sensor ); //Measurement for BME680
christodoulos 7:f37620a76a1d 343 ZMODtemp.write(ZMODTEMPaddr,wInit,2); //maybe?
christodoulos 7:f37620a76a1d 344 ZMODtemp.write(ZMODTEMPaddr,wTemp,1);
christodoulos 7:f37620a76a1d 345 ZMODtemp.read(ZMODTEMPaddr,rTemp,2); //Returns 2 bytes
christodoulos 7:f37620a76a1d 346 float temp_code=(rTemp[0]<<8)+rTemp[1];
christodoulos 7:f37620a76a1d 347 float SiTemp=((175.72*temp_code)/65536)-46.85;
christodoulos 7:f37620a76a1d 348 ttl.printf("tt%.2f,ff%.2f,cc%.2f,sa%i,sb%i,sc%i,sd%i,se%i,sf%i,sg%i,sh%i,ti%.2f,ic%d,iv%d,ir%d,gc%d,gv%d,br%d,bt%.2f,bh%.2f,bp%.2f,zi%f,zh%f,zc%f,st%.2f\n",0.00,flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8(),stream.read(),myiAQ_Core_data.pred, myiAQ_Core_data.Tvoc, myiAQ_Core_data.resistance, sgp30.eCO2,sgp30.TVOC, data.gas_resistance,( data.temperature/100.0f ), ( data.humidity / 1000.0f ), ( data.pressure / 100.0f ),0.00,0.00,0.00,SiTemp); //Results after 5 mins of ON
christodoulos 7:f37620a76a1d 349 rslt = myBME680.bme680_set_sensor_mode ( &gas_sensor ); //BME680 reset
christodoulos 8:976c405a1da5 350 wait(0.1);
christodoulos 5:646a7e58989e 351 difSum+=(myArray[m]-avg)*(myArray[m]-avg); //Find sum of difference between value X and mean
christodoulos 5:646a7e58989e 352 wait(0.1);
christodoulos 5:646a7e58989e 353 }
christodoulos 9:81d1b4833516 354 var=difSum/4;
christodoulos 5:646a7e58989e 355 sigma=sqrt(var);
christodoulos 5:646a7e58989e 356 if (sigma<0.02) {
christodoulos 5:646a7e58989e 357
christodoulos 5:646a7e58989e 358 //if SD is less than 0.02 it means that it is too early to start the plateau
christodoulos 5:646a7e58989e 359 //So we shift all but the first sample and define the new set of arrays:
christodoulos 5:646a7e58989e 360
christodoulos 9:81d1b4833516 361 for(int m=0; m<4; m++) {
christodoulos 5:646a7e58989e 362 //ttl.printf("tt%.2f,ff%.2f,cc%.2f,sa%i,sb%i,sc%i,sd%i,se%i,sf%i,sg%i,sh%i,ti%.2f\n",getTemp(),flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8(),stream.read()); //chemical sensor
christodoulos 7:f37620a76a1d 363 myiAQ_Core.iAQ_Core_GetNewReading ( &myiAQ_Core_data ); //Measurement from iAQ-Core C
christodoulos 7:f37620a76a1d 364 wait(0.1);
christodoulos 7:f37620a76a1d 365 sgp30.IAQmeasure();//Measurement from SGP30
christodoulos 7:f37620a76a1d 366 rslt = myBME680.bme680_get_sensor_data ( &data, &gas_sensor ); //Measurement for BME680
christodoulos 7:f37620a76a1d 367 ZMODtemp.write(ZMODTEMPaddr,wInit,2); //maybe?
christodoulos 7:f37620a76a1d 368 ZMODtemp.write(ZMODTEMPaddr,wTemp,1);
christodoulos 7:f37620a76a1d 369 ZMODtemp.read(ZMODTEMPaddr,rTemp,2); //Returns 2 bytes
christodoulos 7:f37620a76a1d 370 float temp_code=(rTemp[0]<<8)+rTemp[1];
christodoulos 7:f37620a76a1d 371 float SiTemp=((175.72*temp_code)/65536)-46.85;
christodoulos 7:f37620a76a1d 372 ttl.printf("tt%.2f,ff%.2f,cc%.2f,sa%i,sb%i,sc%i,sd%i,se%i,sf%i,sg%i,sh%i,ti%.2f,ic%d,iv%d,ir%d,gc%d,gv%d,br%d,bt%.2f,bh%.2f,bp%.2f,zi%f,zh%f,zc%f,st%.2f\n",0.00,flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8(),stream.read(),myiAQ_Core_data.pred, myiAQ_Core_data.Tvoc, myiAQ_Core_data.resistance, sgp30.eCO2,sgp30.TVOC, data.gas_resistance,( data.temperature/100.0f ), ( data.humidity / 1000.0f ), ( data.pressure / 100.0f ),0.00,0.00,0.00,SiTemp); //Results after 5 mins of ON
christodoulos 7:f37620a76a1d 373 rslt = myBME680.bme680_set_sensor_mode ( &gas_sensor ); //BME680 reset
christodoulos 8:976c405a1da5 374 wait(0.1);
christodoulos 5:646a7e58989e 375 myArray[m]=myArray[m+1]; //Shift all CO2 values to the left by 1 value
christodoulos 5:646a7e58989e 376 wait(0.1);
christodoulos 5:646a7e58989e 377
mehrnaz 2:ef98576cd67b 378 }
christodoulos 9:81d1b4833516 379 myArray[4]=carbon(); //assign a new value for the 9th sample
christodoulos 5:646a7e58989e 380 }
christodoulos 5:646a7e58989e 381 //The new set of arrays are now generated and is sent back to be used for preveious SD calculations.
christodoulos 5:646a7e58989e 382 //If sigma for the new set is still small, a newer set will be generated and replaced
christodoulos 5:646a7e58989e 383 //Otherwise, it's accepted and will turn on the solenoid:
christodoulos 5:646a7e58989e 384 else {
christodoulos 6:f6faf142e5fc 385 sol=0; //Solenoid is ON 0
christodoulos 6:f6faf142e5fc 386 //ttl.printf("tt%.2f,ff%.2f,cc%.2f,sa%i,sb%i,sc%i,sd%i,se%i,sf%i,sg%i,sh%i,ti%.2f\n",getTemp(),flow(), 100.00,s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8(),stream.read()); //chemical sensor
christodoulos 7:f37620a76a1d 387 myiAQ_Core.iAQ_Core_GetNewReading ( &myiAQ_Core_data ); //Measurement from iAQ-Core C
christodoulos 7:f37620a76a1d 388 wait(0.1);
christodoulos 7:f37620a76a1d 389 sgp30.IAQmeasure();//Measurement from SGP30
christodoulos 7:f37620a76a1d 390 rslt = myBME680.bme680_get_sensor_data ( &data, &gas_sensor ); //Measurement for BME680
christodoulos 7:f37620a76a1d 391 ZMODtemp.write(ZMODTEMPaddr,wInit,2); //maybe?
christodoulos 7:f37620a76a1d 392 ZMODtemp.write(ZMODTEMPaddr,wTemp,1);
christodoulos 7:f37620a76a1d 393 ZMODtemp.read(ZMODTEMPaddr,rTemp,2); //Returns 2 bytes
christodoulos 7:f37620a76a1d 394 float temp_code=(rTemp[0]<<8)+rTemp[1];
christodoulos 7:f37620a76a1d 395 float SiTemp=((175.72*temp_code)/65536)-46.85;
christodoulos 8:976c405a1da5 396 ttl.printf("tt%.2f,ff%.2f,cc%.2f,sa%i,sb%i,sc%i,sd%i,se%i,sf%i,sg%i,sh%i,ti%.2f,ic%d,iv%d,ir%d,gc%d,gv%d,br%d,bt%.2f,bh%.2f,bp%.2f,zi%f,zh%f,zc%f,st%.2f\n",0.00,flow(), 0.00,s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8(),stream.read(),myiAQ_Core_data.pred, myiAQ_Core_data.Tvoc, myiAQ_Core_data.resistance, sgp30.eCO2,sgp30.TVOC, data.gas_resistance,( data.temperature/100.0f ), ( data.humidity / 1000.0f ), ( data.pressure / 100.0f ),0.00,0.00,0.00,SiTemp); //Results after 5 mins of ON
christodoulos 8:976c405a1da5 397 rslt = myBME680.bme680_set_sensor_mode ( &gas_sensor ); //BME680 reset
christodoulos 8:976c405a1da5 398 wait(0.1);
christodoulos 8:976c405a1da5 399
christodoulos 6:f6faf142e5fc 400 //led=0.4f; //The brightness is reduced to half during the plateau
christodoulos 5:646a7e58989e 401 wait(0.1);
christodoulos 5:646a7e58989e 402 flags=1; //breakes the while loop
christodoulos 5:646a7e58989e 403 }
mehrnaz 2:ef98576cd67b 404 }
christodoulos 5:646a7e58989e 405 solend=1; //prepares the next section for finishing the plateau
christodoulos 5:646a7e58989e 406 solstart =1;
christodoulos 5:646a7e58989e 407 }
christodoulos 5:646a7e58989e 408 if ((measurement_started == 1) and (solend==1)) {
christodoulos 5:646a7e58989e 409 // same process happens for finishing the plateau:
christodoulos 5:646a7e58989e 410
christodoulos 9:81d1b4833516 411 for(m=0; m<4; m++) {
christodoulos 5:646a7e58989e 412 //ttl.printf("tt%.2f,ff%.2f,cc%.2f,sa%i,sb%i,sc%i,sd%i,se%i,sf%i,sg%i,sh%i,ti%.2f\n",getTemp(),flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8(),stream.read()); //chemical sensor
christodoulos 7:f37620a76a1d 413 myiAQ_Core.iAQ_Core_GetNewReading ( &myiAQ_Core_data ); //Measurement from iAQ-Core C
christodoulos 7:f37620a76a1d 414 wait(0.1);
christodoulos 7:f37620a76a1d 415 sgp30.IAQmeasure();//Measurement from SGP30
christodoulos 7:f37620a76a1d 416 rslt = myBME680.bme680_get_sensor_data ( &data, &gas_sensor ); //Measurement for BME680
christodoulos 7:f37620a76a1d 417 ZMODtemp.write(ZMODTEMPaddr,wInit,2); //maybe?
christodoulos 7:f37620a76a1d 418 ZMODtemp.write(ZMODTEMPaddr,wTemp,1);
christodoulos 7:f37620a76a1d 419 ZMODtemp.read(ZMODTEMPaddr,rTemp,2); //Returns 2 bytes
christodoulos 7:f37620a76a1d 420 float temp_code=(rTemp[0]<<8)+rTemp[1];
christodoulos 7:f37620a76a1d 421 float SiTemp=((175.72*temp_code)/65536)-46.85;
christodoulos 7:f37620a76a1d 422 ttl.printf("tt%.2f,ff%.2f,cc%.2f,sa%i,sb%i,sc%i,sd%i,se%i,sf%i,sg%i,sh%i,ti%.2f,ic%d,iv%d,ir%d,gc%d,gv%d,br%d,bt%.2f,bh%.2f,bp%.2f,zi%f,zh%f,zc%f,st%.2f\n",0.00,flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8(),stream.read(),myiAQ_Core_data.pred, myiAQ_Core_data.Tvoc, myiAQ_Core_data.resistance, sgp30.eCO2,sgp30.TVOC, data.gas_resistance,( data.temperature/100.0f ), ( data.humidity / 1000.0f ), ( data.pressure / 100.0f ),0.00,0.00,0.00,SiTemp); //Results after 5 mins of ON
christodoulos 7:f37620a76a1d 423 rslt = myBME680.bme680_set_sensor_mode ( &gas_sensor ); //BME680 reset
christodoulos 8:976c405a1da5 424 wait(0.1);
christodoulos 5:646a7e58989e 425 myArray[m]=carbon();
christodoulos 5:646a7e58989e 426 wait(0.1);
christodoulos 5:646a7e58989e 427 }
christodoulos 5:646a7e58989e 428 while(flage==0) {
christodoulos 9:81d1b4833516 429 for(int m=0; m<4; m++) {
christodoulos 5:646a7e58989e 430 //ttl.printf("tt%.2f,ff%.2f,cc%.2f,sa%i,sb%i,sc%i,sd%i,se%i,sf%i,sg%i,sh%i,ti%.2f\n",getTemp(),flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8(),stream.read()); //chemical sensor
christodoulos 7:f37620a76a1d 431 myiAQ_Core.iAQ_Core_GetNewReading ( &myiAQ_Core_data ); //Measurement from iAQ-Core C
christodoulos 7:f37620a76a1d 432 wait(0.1);
christodoulos 7:f37620a76a1d 433 sgp30.IAQmeasure();//Measurement from SGP30
christodoulos 7:f37620a76a1d 434 rslt = myBME680.bme680_get_sensor_data ( &data, &gas_sensor ); //Measurement for BME680
christodoulos 7:f37620a76a1d 435 ZMODtemp.write(ZMODTEMPaddr,wInit,2); //maybe?
christodoulos 7:f37620a76a1d 436 ZMODtemp.write(ZMODTEMPaddr,wTemp,1);
christodoulos 7:f37620a76a1d 437 ZMODtemp.read(ZMODTEMPaddr,rTemp,2); //Returns 2 bytes
christodoulos 7:f37620a76a1d 438 float temp_code=(rTemp[0]<<8)+rTemp[1];
christodoulos 7:f37620a76a1d 439 float SiTemp=((175.72*temp_code)/65536)-46.85;
christodoulos 7:f37620a76a1d 440 ttl.printf("tt%.2f,ff%.2f,cc%.2f,sa%i,sb%i,sc%i,sd%i,se%i,sf%i,sg%i,sh%i,ti%.2f,ic%d,iv%d,ir%d,gc%d,gv%d,br%d,bt%.2f,bh%.2f,bp%.2f,zi%f,zh%f,zc%f,st%.2f\n",0.00,flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8(),stream.read(),myiAQ_Core_data.pred, myiAQ_Core_data.Tvoc, myiAQ_Core_data.resistance, sgp30.eCO2,sgp30.TVOC, data.gas_resistance,( data.temperature/100.0f ), ( data.humidity / 1000.0f ), ( data.pressure / 100.0f ),0.00,0.00,0.00,SiTemp); //Results after 5 mins of ON
christodoulos 7:f37620a76a1d 441 rslt = myBME680.bme680_set_sensor_mode ( &gas_sensor ); //BME680 reset
christodoulos 8:976c405a1da5 442 wait(0.1);
christodoulos 5:646a7e58989e 443 sum2+=myArray[m];
christodoulos 5:646a7e58989e 444 wait(0.1);
christodoulos 5:646a7e58989e 445 }
christodoulos 9:81d1b4833516 446 avg2=sum2/4;
christodoulos 9:81d1b4833516 447 for(int m=0; m<4; m++) {
christodoulos 5:646a7e58989e 448 //ttl.printf("tt%.2f,ff%.2f,cc%.2f,sa%i,sb%i,sc%i,sd%i,se%i,sf%i,sg%i,sh%i,ti%.2f\n",getTemp(),flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8(),stream.read()); //chemical sensor
christodoulos 7:f37620a76a1d 449 myiAQ_Core.iAQ_Core_GetNewReading ( &myiAQ_Core_data ); //Measurement from iAQ-Core C
christodoulos 7:f37620a76a1d 450 wait(0.1);
christodoulos 7:f37620a76a1d 451 sgp30.IAQmeasure();//Measurement from SGP30
christodoulos 7:f37620a76a1d 452 rslt = myBME680.bme680_get_sensor_data ( &data, &gas_sensor ); //Measurement for BME680
christodoulos 7:f37620a76a1d 453 ZMODtemp.write(ZMODTEMPaddr,wInit,2); //maybe?
christodoulos 7:f37620a76a1d 454 ZMODtemp.write(ZMODTEMPaddr,wTemp,1);
christodoulos 7:f37620a76a1d 455 ZMODtemp.read(ZMODTEMPaddr,rTemp,2); //Returns 2 bytes
christodoulos 7:f37620a76a1d 456 float temp_code=(rTemp[0]<<8)+rTemp[1];
christodoulos 7:f37620a76a1d 457 float SiTemp=((175.72*temp_code)/65536)-46.85;
christodoulos 7:f37620a76a1d 458 ttl.printf("tt%.2f,ff%.2f,cc%.2f,sa%i,sb%i,sc%i,sd%i,se%i,sf%i,sg%i,sh%i,ti%.2f,ic%d,iv%d,ir%d,gc%d,gv%d,br%d,bt%.2f,bh%.2f,bp%.2f,zi%f,zh%f,zc%f,st%.2f\n",0.00,flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8(),stream.read(),myiAQ_Core_data.pred, myiAQ_Core_data.Tvoc, myiAQ_Core_data.resistance, sgp30.eCO2,sgp30.TVOC, data.gas_resistance,( data.temperature/100.0f ), ( data.humidity / 1000.0f ), ( data.pressure / 100.0f ),0.00,0.00,0.00,SiTemp); //Results after 5 mins of ON
christodoulos 7:f37620a76a1d 459 rslt = myBME680.bme680_set_sensor_mode ( &gas_sensor ); //BME680 reset
christodoulos 8:976c405a1da5 460 wait(0.1);
christodoulos 5:646a7e58989e 461 difSum2+=(myArray[m]-avg2)*(myArray[m]-avg2);
christodoulos 5:646a7e58989e 462 wait(0.1);
christodoulos 5:646a7e58989e 463 }
christodoulos 9:81d1b4833516 464 var2=difSum2/4;
christodoulos 5:646a7e58989e 465 sigma2=sqrt(var2);
christodoulos 5:646a7e58989e 466 if (sigma2<0.05) {
christodoulos 5:646a7e58989e 467 // here we defined the end threshold to be 0.05, it can be changed later based on experiment results
christodoulos 9:81d1b4833516 468 for(int m=0; m<4; m++) {
christodoulos 5:646a7e58989e 469 //ttl.printf("tt%.2f,ff%.2f,cc%.2f,sa%i,sb%i,sc%i,sd%i,se%i,sf%i,sg%i,sh%i,ti%.2f\n",getTemp(),flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8(),stream.read()); //chemical sensor
christodoulos 7:f37620a76a1d 470 myiAQ_Core.iAQ_Core_GetNewReading ( &myiAQ_Core_data ); //Measurement from iAQ-Core C
christodoulos 7:f37620a76a1d 471 wait(0.1);
christodoulos 7:f37620a76a1d 472 sgp30.IAQmeasure();//Measurement from SGP30
christodoulos 7:f37620a76a1d 473 rslt = myBME680.bme680_get_sensor_data ( &data, &gas_sensor ); //Measurement for BME680
christodoulos 7:f37620a76a1d 474 ZMODtemp.write(ZMODTEMPaddr,wInit,2); //maybe?
christodoulos 7:f37620a76a1d 475 ZMODtemp.write(ZMODTEMPaddr,wTemp,1);
christodoulos 7:f37620a76a1d 476 ZMODtemp.read(ZMODTEMPaddr,rTemp,2); //Returns 2 bytes
christodoulos 7:f37620a76a1d 477 float temp_code=(rTemp[0]<<8)+rTemp[1];
christodoulos 7:f37620a76a1d 478 float SiTemp=((175.72*temp_code)/65536)-46.85;
christodoulos 7:f37620a76a1d 479 ttl.printf("tt%.2f,ff%.2f,cc%.2f,sa%i,sb%i,sc%i,sd%i,se%i,sf%i,sg%i,sh%i,ti%.2f,ic%d,iv%d,ir%d,gc%d,gv%d,br%d,bt%.2f,bh%.2f,bp%.2f,zi%f,zh%f,zc%f,st%.2f\n",0.00,flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8(),stream.read(),myiAQ_Core_data.pred, myiAQ_Core_data.Tvoc, myiAQ_Core_data.resistance, sgp30.eCO2,sgp30.TVOC, data.gas_resistance,( data.temperature/100.0f ), ( data.humidity / 1000.0f ), ( data.pressure / 100.0f ),0.00,0.00,0.00,SiTemp); //Results after 5 mins of ON
christodoulos 7:f37620a76a1d 480 rslt = myBME680.bme680_set_sensor_mode ( &gas_sensor ); //BME680 reset
christodoulos 8:976c405a1da5 481 wait(0.1);
christodoulos 5:646a7e58989e 482 myArray[m]=myArray[m+1];
christodoulos 5:646a7e58989e 483 wait(0.1);
christodoulos 5:646a7e58989e 484
mehrnaz 2:ef98576cd67b 485 }
christodoulos 9:81d1b4833516 486 myArray[4]=carbon();
christodoulos 5:646a7e58989e 487 } else {
christodoulos 6:f6faf142e5fc 488 sol=1; //Solenoid is OFF 1
christodoulos 6:f6faf142e5fc 489 //ttl.printf("tt%.2f,ff%.2f,cc%.2f,sa%i,sb%i,sc%i,sd%i,se%i,sf%i,sg%i,sh%i,ti%.2f\n",getTemp(),flow(), 100.00,s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8(),stream.read()); //chemical sensor
christodoulos 7:f37620a76a1d 490 myiAQ_Core.iAQ_Core_GetNewReading ( &myiAQ_Core_data ); //Measurement from iAQ-Core C
christodoulos 5:646a7e58989e 491 wait(0.1);
christodoulos 7:f37620a76a1d 492 sgp30.IAQmeasure();//Measurement from SGP30
christodoulos 7:f37620a76a1d 493 rslt = myBME680.bme680_get_sensor_data ( &data, &gas_sensor ); //Measurement for BME680
christodoulos 7:f37620a76a1d 494 ZMODtemp.write(ZMODTEMPaddr,wInit,2); //maybe?
christodoulos 7:f37620a76a1d 495 ZMODtemp.write(ZMODTEMPaddr,wTemp,1);
christodoulos 7:f37620a76a1d 496 ZMODtemp.read(ZMODTEMPaddr,rTemp,2); //Returns 2 bytes
christodoulos 7:f37620a76a1d 497 float temp_code=(rTemp[0]<<8)+rTemp[1];
christodoulos 7:f37620a76a1d 498 float SiTemp=((175.72*temp_code)/65536)-46.85;
christodoulos 7:f37620a76a1d 499 ttl.printf("tt%.2f,ff%.2f,cc%.2f,sa%i,sb%i,sc%i,sd%i,se%i,sf%i,sg%i,sh%i,ti%.2f,ic%d,iv%d,ir%d,gc%d,gv%d,br%d,bt%.2f,bh%.2f,bp%.2f,zi%f,zh%f,zc%f,st%.2f\n",0.00,flow(), 0.00,s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8(),stream.read(),myiAQ_Core_data.pred, myiAQ_Core_data.Tvoc, myiAQ_Core_data.resistance, sgp30.eCO2,sgp30.TVOC, data.gas_resistance,( data.temperature/100.0f ), ( data.humidity / 1000.0f ), ( data.pressure / 100.0f ),0.00,0.00,0.00,SiTemp); //Results after 5 mins of ON
christodoulos 7:f37620a76a1d 500 rslt = myBME680.bme680_set_sensor_mode ( &gas_sensor ); //BME680 reset
christodoulos 7:f37620a76a1d 501 wait(0.1);
christodoulos 7:f37620a76a1d 502
christodoulos 8:976c405a1da5 503
christodoulos 5:646a7e58989e 504 flage=1; //breakes the loop
christodoulos 5:646a7e58989e 505 }
christodoulos 5:646a7e58989e 506 }
christodoulos 6:f6faf142e5fc 507
christodoulos 6:f6faf142e5fc 508 //ttl.printf("tt%.2f,ff%.2f,cc%.2f,sa%i,sb%i,sc%i,sd%i,se%i,sf%i,sg%i,sh%i,ti%.2f\n",getTemp(),flow(),carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8(),stream.read()); //chemical sensor
christodoulos 7:f37620a76a1d 509 myiAQ_Core.iAQ_Core_GetNewReading ( &myiAQ_Core_data ); //Measurement from iAQ-Core C
christodoulos 7:f37620a76a1d 510 wait(0.1);
christodoulos 7:f37620a76a1d 511 sgp30.IAQmeasure();//Measurement from SGP30
christodoulos 7:f37620a76a1d 512 rslt = myBME680.bme680_get_sensor_data ( &data, &gas_sensor ); //Measurement for BME680
christodoulos 7:f37620a76a1d 513 ZMODtemp.write(ZMODTEMPaddr,wInit,2); //maybe?
christodoulos 7:f37620a76a1d 514 ZMODtemp.write(ZMODTEMPaddr,wTemp,1);
christodoulos 7:f37620a76a1d 515 ZMODtemp.read(ZMODTEMPaddr,rTemp,2); //Returns 2 bytes
christodoulos 7:f37620a76a1d 516 float temp_code=(rTemp[0]<<8)+rTemp[1];
christodoulos 7:f37620a76a1d 517 float SiTemp=((175.72*temp_code)/65536)-46.85;
christodoulos 7:f37620a76a1d 518 ttl.printf("tt%.2f,ff%.2f,cc%.2f,sa%i,sb%i,sc%i,sd%i,se%i,sf%i,sg%i,sh%i,ti%.2f,ic%d,iv%d,ir%d,gc%d,gv%d,br%d,bt%.2f,bh%.2f,bp%.2f,zi%f,zh%f,zc%f,st%.2f\n",0.00,flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8(),stream.read(),myiAQ_Core_data.pred, myiAQ_Core_data.Tvoc, myiAQ_Core_data.resistance, sgp30.eCO2,sgp30.TVOC, data.gas_resistance,( data.temperature/100.0f ), ( data.humidity / 1000.0f ), ( data.pressure / 100.0f ),0.00,0.00,0.00,SiTemp); //Results after 5 mins of ON
christodoulos 7:f37620a76a1d 519 rslt = myBME680.bme680_set_sensor_mode ( &gas_sensor ); //BME680 reset
christodoulos 8:976c405a1da5 520 wait(0.1);
christodoulos 8:976c405a1da5 521 //led=0.4f; //LED is back to full brightness
christodoulos 5:646a7e58989e 522 bf=0; //reset the detecting base flow flag
christodoulos 5:646a7e58989e 523 fin=1; //enables the next section flag
christodoulos 5:646a7e58989e 524
christodoulos 5:646a7e58989e 525
christodoulos 6:f6faf142e5fc 526
mehrnaz 2:ef98576cd67b 527 }
christodoulos 5:646a7e58989e 528
christodoulos 6:f6faf142e5fc 529 if(carbon()<2.00 && fin ==1) {
christodoulos 8:976c405a1da5 530 //User has to wait for the CO2 level to drop less than 1% before testing again.
christodoulos 8:976c405a1da5 531 //Once it is less than 1%, all the flags and parameters used in calculations are reset
christodoulos 7:f37620a76a1d 532
christodoulos 7:f37620a76a1d 533 //ttl.printf("tt%.2f,ff%.2f,cc%.2f,sa%i,sb%i,sc%i,sd%i,se%i,sf%i,sg%i,sh%i,ti%.2f\n",getTemp(),flow(),carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8(),stream.read()); //chemical sensor
christodoulos 7:f37620a76a1d 534 myiAQ_Core.iAQ_Core_GetNewReading ( &myiAQ_Core_data ); //Measurement from iAQ-Core C
christodoulos 7:f37620a76a1d 535 wait(0.1);
christodoulos 7:f37620a76a1d 536 sgp30.IAQmeasure();//Measurement from SGP30
christodoulos 7:f37620a76a1d 537 rslt = myBME680.bme680_get_sensor_data ( &data, &gas_sensor ); //Measurement for BME680
christodoulos 7:f37620a76a1d 538 ZMODtemp.write(ZMODTEMPaddr,wInit,2); //maybe?
christodoulos 7:f37620a76a1d 539 ZMODtemp.write(ZMODTEMPaddr,wTemp,1);
christodoulos 7:f37620a76a1d 540 ZMODtemp.read(ZMODTEMPaddr,rTemp,2); //Returns 2 bytes
christodoulos 7:f37620a76a1d 541 float temp_code=(rTemp[0]<<8)+rTemp[1];
christodoulos 7:f37620a76a1d 542 float SiTemp=((175.72*temp_code)/65536)-46.85;
christodoulos 7:f37620a76a1d 543 ttl.printf("tt%.2f,ff%.2f,cc%.2f,sa%i,sb%i,sc%i,sd%i,se%i,sf%i,sg%i,sh%i,ti%.2f,ic%d,iv%d,ir%d,gc%d,gv%d,br%d,bt%.2f,bh%.2f,bp%.2f,zi%f,zh%f,zc%f,st%.2f\n",0.00,flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8(),stream.read(),myiAQ_Core_data.pred, myiAQ_Core_data.Tvoc, myiAQ_Core_data.resistance, sgp30.eCO2,sgp30.TVOC, data.gas_resistance,( data.temperature/100.0f ), ( data.humidity / 1000.0f ), ( data.pressure / 100.0f ),0.00,0.00,0.00,SiTemp); //Results after 5 mins of ON
christodoulos 7:f37620a76a1d 544 rslt = myBME680.bme680_set_sensor_mode ( &gas_sensor ); //BME680 reset
christodoulos 8:976c405a1da5 545 wait(0.1);
christodoulos 7:f37620a76a1d 546 stream.reset();
christodoulos 7:f37620a76a1d 547 stream.stop();
christodoulos 7:f37620a76a1d 548 measurement_started =0;
christodoulos 7:f37620a76a1d 549 solstart=0;
christodoulos 7:f37620a76a1d 550 sum=0;
christodoulos 7:f37620a76a1d 551 var=0.0;
christodoulos 7:f37620a76a1d 552 sigma=0.0;
christodoulos 7:f37620a76a1d 553 difSum=0;
christodoulos 7:f37620a76a1d 554 sum2=0;
christodoulos 7:f37620a76a1d 555 var2=0.0;
christodoulos 7:f37620a76a1d 556 sigma2=0.0;
christodoulos 7:f37620a76a1d 557 difSum2=0;
christodoulos 7:f37620a76a1d 558 avg2=0;
christodoulos 7:f37620a76a1d 559 avg=0;
christodoulos 7:f37620a76a1d 560 flags=0;
christodoulos 7:f37620a76a1d 561 flage=0;
christodoulos 7:f37620a76a1d 562 fin=0;
christodoulos 7:f37620a76a1d 563 }
christodoulos 5:646a7e58989e 564
mehrnaz 2:ef98576cd67b 565 }
mehrnaz 2:ef98576cd67b 566 }