POC Breath using SMD commercial sensors
Dependencies: iAQ_Core Adafruit_SGP30_mbed mbed BME680
main.cpp@7:f37620a76a1d, 2020-05-29 (annotated)
- Committer:
- christodoulos
- Date:
- Fri May 29 15:41:58 2020 +0000
- Revision:
- 7:f37620a76a1d
- Parent:
- 6:f6faf142e5fc
- Child:
- 8:976c405a1da5
UPDATED EXCEPT ZMOD
Who changed what in which revision?
User | Revision | Line number | New 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*)®_addr, 1, true ); |
christodoulos | 7:f37620a76a1d | 64 | aux = myBME680._i2c.read ( dev_id, (char*)®_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 | 6:f6faf142e5fc | 116 | ttl.printf("SN,POC Breath\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; |
mehrnaz | 3:3d51f8870e91 | 235 | int myArray[9]; //sampling 9 values of CO2 |
mehrnaz | 3:3d51f8870e91 | 236 | unsigned int sum=0; //sum of 9 samples of CO2 |
mehrnaz | 3:3d51f8870e91 | 237 | int avg=0; //average of 9 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 | |
christodoulos | 7:f37620a76a1d | 264 | rslt = myBME680.bme680_get_sensor_data ( &data, &gas_sensor ); //Measurement for BME680 |
christodoulos | 7:f37620a76a1d | 265 | |
christodoulos | 7:f37620a76a1d | 266 | ZMODtemp.write(ZMODTEMPaddr,wInit,2); //maybe? |
christodoulos | 7:f37620a76a1d | 267 | ZMODtemp.write(ZMODTEMPaddr,wTemp,1); |
christodoulos | 7:f37620a76a1d | 268 | ZMODtemp.read(ZMODTEMPaddr,rTemp,2); //Returns 2 bytes |
christodoulos | 7:f37620a76a1d | 269 | float temp_code=(rTemp[0]<<8)+rTemp[1]; |
christodoulos | 7:f37620a76a1d | 270 | float SiTemp=((175.72*temp_code)/65536)-46.85; |
christodoulos | 7:f37620a76a1d | 271 | 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 | 272 | rslt = myBME680.bme680_set_sensor_mode ( &gas_sensor ); //BME680 reset |
christodoulos | 7:f37620a76a1d | 273 | |
christodoulos | 5:646a7e58989e | 274 | wait(0.01); |
christodoulos | 5:646a7e58989e | 275 | if (bf==0) { //finding base flow before breath |
christodoulos | 5:646a7e58989e | 276 | for(i=0; i<4; i++) { |
christodoulos | 5:646a7e58989e | 277 | bfArray[i]=flow(); |
christodoulos | 5:646a7e58989e | 278 | sf+=bfArray[i]; |
christodoulos | 5:646a7e58989e | 279 | } |
christodoulos | 5:646a7e58989e | 280 | fv=sf/4; |
christodoulos | 6:f6faf142e5fc | 281 | //fv=fv+0.2; |
christodoulos | 7:f37620a76a1d | 282 | fv=0.1; |
christodoulos | 5:646a7e58989e | 283 | //ttl.printf("set\n"); |
christodoulos | 5:646a7e58989e | 284 | bf=1; |
christodoulos | 5:646a7e58989e | 285 | } |
christodoulos | 5:646a7e58989e | 286 | |
christodoulos | 5:646a7e58989e | 287 | //Starts calculations when it detects breathing into the device: |
christodoulos | 5:646a7e58989e | 288 | |
christodoulos | 5:646a7e58989e | 289 | if ((flow()>fv) and (measurement_started ==0)) { |
christodoulos | 7:f37620a76a1d | 290 | stream.start(); |
christodoulos | 5:646a7e58989e | 291 | measurement_started = 1; |
christodoulos | 5:646a7e58989e | 292 | } |
christodoulos | 5:646a7e58989e | 293 | |
christodoulos | 5:646a7e58989e | 294 | //Starts detecting plateau: |
christodoulos | 5:646a7e58989e | 295 | |
christodoulos | 5:646a7e58989e | 296 | if ((measurement_started == 1) and (solstart==0)) { |
mehrnaz | 3:3d51f8870e91 | 297 | |
christodoulos | 5:646a7e58989e | 298 | //Takes 9 samples of CO2: |
christodoulos | 5:646a7e58989e | 299 | //I have also included printing the values inside the loops so we don't loose any data during calculatins. |
christodoulos | 5:646a7e58989e | 300 | for(m=0; m<9; m++) { |
christodoulos | 5:646a7e58989e | 301 | //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 | 302 | myiAQ_Core.iAQ_Core_GetNewReading ( &myiAQ_Core_data ); //Measurement from iAQ-Core C |
christodoulos | 7:f37620a76a1d | 303 | wait(0.1); |
christodoulos | 7:f37620a76a1d | 304 | sgp30.IAQmeasure();//Measurement from SGP30 |
christodoulos | 7:f37620a76a1d | 305 | |
christodoulos | 7:f37620a76a1d | 306 | rslt = myBME680.bme680_get_sensor_data ( &data, &gas_sensor ); //Measurement for BME680 |
christodoulos | 7:f37620a76a1d | 307 | |
christodoulos | 7:f37620a76a1d | 308 | ZMODtemp.write(ZMODTEMPaddr,wInit,2); //maybe? |
christodoulos | 7:f37620a76a1d | 309 | ZMODtemp.write(ZMODTEMPaddr,wTemp,1); |
christodoulos | 7:f37620a76a1d | 310 | ZMODtemp.read(ZMODTEMPaddr,rTemp,2); //Returns 2 bytes |
christodoulos | 7:f37620a76a1d | 311 | float temp_code=(rTemp[0]<<8)+rTemp[1]; |
christodoulos | 7:f37620a76a1d | 312 | float SiTemp=((175.72*temp_code)/65536)-46.85; |
christodoulos | 7:f37620a76a1d | 313 | |
christodoulos | 7:f37620a76a1d | 314 | 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 | 315 | rslt = myBME680.bme680_set_sensor_mode ( &gas_sensor ); //BME680 reset |
christodoulos | 7:f37620a76a1d | 316 | |
christodoulos | 5:646a7e58989e | 317 | myArray[m]=carbon(); |
christodoulos | 5:646a7e58989e | 318 | wait(0.1); |
mehrnaz | 2:ef98576cd67b | 319 | } |
christodoulos | 5:646a7e58989e | 320 | while(flags==0) { |
christodoulos | 5:646a7e58989e | 321 | //While "flags" is enabled, keeps calculating the standard deviation. |
christodoulos | 5:646a7e58989e | 322 | for(int m=0; m<9; m++) { |
christodoulos | 5:646a7e58989e | 323 | //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 | 324 | |
christodoulos | 7:f37620a76a1d | 325 | myiAQ_Core.iAQ_Core_GetNewReading ( &myiAQ_Core_data ); //Measurement from iAQ-Core C |
christodoulos | 7:f37620a76a1d | 326 | wait(0.1); |
christodoulos | 7:f37620a76a1d | 327 | sgp30.IAQmeasure();//Measurement from SGP30 |
christodoulos | 7:f37620a76a1d | 328 | |
christodoulos | 7:f37620a76a1d | 329 | rslt = myBME680.bme680_get_sensor_data ( &data, &gas_sensor ); //Measurement for BME680 |
christodoulos | 7:f37620a76a1d | 330 | |
christodoulos | 7:f37620a76a1d | 331 | ZMODtemp.write(ZMODTEMPaddr,wInit,2); //maybe? |
christodoulos | 7:f37620a76a1d | 332 | ZMODtemp.write(ZMODTEMPaddr,wTemp,1); |
christodoulos | 7:f37620a76a1d | 333 | ZMODtemp.read(ZMODTEMPaddr,rTemp,2); //Returns 2 bytes |
christodoulos | 7:f37620a76a1d | 334 | float temp_code=(rTemp[0]<<8)+rTemp[1]; |
christodoulos | 7:f37620a76a1d | 335 | float SiTemp=((175.72*temp_code)/65536)-46.85; |
christodoulos | 7:f37620a76a1d | 336 | 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 | 337 | rslt = myBME680.bme680_set_sensor_mode ( &gas_sensor ); //BME680 reset |
christodoulos | 7:f37620a76a1d | 338 | |
christodoulos | 5:646a7e58989e | 339 | sum+=myArray[m]; |
christodoulos | 5:646a7e58989e | 340 | wait(0.1); |
christodoulos | 5:646a7e58989e | 341 | |
christodoulos | 5:646a7e58989e | 342 | } |
christodoulos | 5:646a7e58989e | 343 | avg=sum/9; |
christodoulos | 5:646a7e58989e | 344 | for(int m=0; m<9; m++) { |
christodoulos | 5:646a7e58989e | 345 | //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 | 346 | myiAQ_Core.iAQ_Core_GetNewReading ( &myiAQ_Core_data ); //Measurement from iAQ-Core C |
christodoulos | 7:f37620a76a1d | 347 | wait(0.1); |
christodoulos | 7:f37620a76a1d | 348 | sgp30.IAQmeasure();//Measurement from SGP30 |
christodoulos | 7:f37620a76a1d | 349 | |
christodoulos | 7:f37620a76a1d | 350 | rslt = myBME680.bme680_get_sensor_data ( &data, &gas_sensor ); //Measurement for BME680 |
christodoulos | 7:f37620a76a1d | 351 | |
christodoulos | 7:f37620a76a1d | 352 | ZMODtemp.write(ZMODTEMPaddr,wInit,2); //maybe? |
christodoulos | 7:f37620a76a1d | 353 | ZMODtemp.write(ZMODTEMPaddr,wTemp,1); |
christodoulos | 7:f37620a76a1d | 354 | ZMODtemp.read(ZMODTEMPaddr,rTemp,2); //Returns 2 bytes |
christodoulos | 7:f37620a76a1d | 355 | float temp_code=(rTemp[0]<<8)+rTemp[1]; |
christodoulos | 7:f37620a76a1d | 356 | float SiTemp=((175.72*temp_code)/65536)-46.85; |
christodoulos | 7:f37620a76a1d | 357 | 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 | 358 | rslt = myBME680.bme680_set_sensor_mode ( &gas_sensor ); //BME680 reset |
christodoulos | 7:f37620a76a1d | 359 | |
christodoulos | 5:646a7e58989e | 360 | difSum+=(myArray[m]-avg)*(myArray[m]-avg); //Find sum of difference between value X and mean |
christodoulos | 5:646a7e58989e | 361 | wait(0.1); |
christodoulos | 5:646a7e58989e | 362 | } |
christodoulos | 5:646a7e58989e | 363 | var=difSum/9; |
christodoulos | 5:646a7e58989e | 364 | sigma=sqrt(var); |
christodoulos | 5:646a7e58989e | 365 | if (sigma<0.02) { |
christodoulos | 5:646a7e58989e | 366 | |
christodoulos | 5:646a7e58989e | 367 | //if SD is less than 0.02 it means that it is too early to start the plateau |
christodoulos | 5:646a7e58989e | 368 | //So we shift all but the first sample and define the new set of arrays: |
christodoulos | 5:646a7e58989e | 369 | |
christodoulos | 5:646a7e58989e | 370 | for(int m=0; m<8; m++) { |
christodoulos | 5:646a7e58989e | 371 | //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 | 372 | myiAQ_Core.iAQ_Core_GetNewReading ( &myiAQ_Core_data ); //Measurement from iAQ-Core C |
christodoulos | 7:f37620a76a1d | 373 | wait(0.1); |
christodoulos | 7:f37620a76a1d | 374 | sgp30.IAQmeasure();//Measurement from SGP30 |
christodoulos | 7:f37620a76a1d | 375 | |
christodoulos | 7:f37620a76a1d | 376 | rslt = myBME680.bme680_get_sensor_data ( &data, &gas_sensor ); //Measurement for BME680 |
christodoulos | 7:f37620a76a1d | 377 | |
christodoulos | 7:f37620a76a1d | 378 | ZMODtemp.write(ZMODTEMPaddr,wInit,2); //maybe? |
christodoulos | 7:f37620a76a1d | 379 | ZMODtemp.write(ZMODTEMPaddr,wTemp,1); |
christodoulos | 7:f37620a76a1d | 380 | ZMODtemp.read(ZMODTEMPaddr,rTemp,2); //Returns 2 bytes |
christodoulos | 7:f37620a76a1d | 381 | float temp_code=(rTemp[0]<<8)+rTemp[1]; |
christodoulos | 7:f37620a76a1d | 382 | float SiTemp=((175.72*temp_code)/65536)-46.85; |
christodoulos | 7:f37620a76a1d | 383 | 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 | 384 | rslt = myBME680.bme680_set_sensor_mode ( &gas_sensor ); //BME680 reset |
christodoulos | 7:f37620a76a1d | 385 | |
christodoulos | 5:646a7e58989e | 386 | myArray[m]=myArray[m+1]; //Shift all CO2 values to the left by 1 value |
christodoulos | 5:646a7e58989e | 387 | wait(0.1); |
christodoulos | 5:646a7e58989e | 388 | |
mehrnaz | 2:ef98576cd67b | 389 | } |
christodoulos | 5:646a7e58989e | 390 | myArray[8]=carbon(); //assign a new value for the 9th sample |
christodoulos | 5:646a7e58989e | 391 | } |
christodoulos | 5:646a7e58989e | 392 | //The new set of arrays are now generated and is sent back to be used for preveious SD calculations. |
christodoulos | 5:646a7e58989e | 393 | //If sigma for the new set is still small, a newer set will be generated and replaced |
christodoulos | 5:646a7e58989e | 394 | //Otherwise, it's accepted and will turn on the solenoid: |
christodoulos | 5:646a7e58989e | 395 | else { |
christodoulos | 6:f6faf142e5fc | 396 | sol=0; //Solenoid is ON 0 |
christodoulos | 6:f6faf142e5fc | 397 | //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 | 398 | myiAQ_Core.iAQ_Core_GetNewReading ( &myiAQ_Core_data ); //Measurement from iAQ-Core C |
christodoulos | 7:f37620a76a1d | 399 | wait(0.1); |
christodoulos | 7:f37620a76a1d | 400 | sgp30.IAQmeasure();//Measurement from SGP30 |
christodoulos | 7:f37620a76a1d | 401 | |
christodoulos | 7:f37620a76a1d | 402 | rslt = myBME680.bme680_get_sensor_data ( &data, &gas_sensor ); //Measurement for BME680 |
christodoulos | 7:f37620a76a1d | 403 | |
christodoulos | 7:f37620a76a1d | 404 | ZMODtemp.write(ZMODTEMPaddr,wInit,2); //maybe? |
christodoulos | 7:f37620a76a1d | 405 | ZMODtemp.write(ZMODTEMPaddr,wTemp,1); |
christodoulos | 7:f37620a76a1d | 406 | ZMODtemp.read(ZMODTEMPaddr,rTemp,2); //Returns 2 bytes |
christodoulos | 7:f37620a76a1d | 407 | float temp_code=(rTemp[0]<<8)+rTemp[1]; |
christodoulos | 7:f37620a76a1d | 408 | float SiTemp=((175.72*temp_code)/65536)-46.85; |
christodoulos | 7:f37620a76a1d | 409 | 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 | 410 | rslt = myBME680.bme680_set_sensor_mode ( &gas_sensor ); //BME680 reset |
christodoulos | 7:f37620a76a1d | 411 | |
christodoulos | 6:f6faf142e5fc | 412 | //led=0.4f; //The brightness is reduced to half during the plateau |
christodoulos | 5:646a7e58989e | 413 | wait(0.1); |
christodoulos | 5:646a7e58989e | 414 | flags=1; //breakes the while loop |
christodoulos | 5:646a7e58989e | 415 | } |
mehrnaz | 2:ef98576cd67b | 416 | } |
christodoulos | 5:646a7e58989e | 417 | solend=1; //prepares the next section for finishing the plateau |
christodoulos | 5:646a7e58989e | 418 | solstart =1; |
christodoulos | 5:646a7e58989e | 419 | } |
christodoulos | 5:646a7e58989e | 420 | if ((measurement_started == 1) and (solend==1)) { |
christodoulos | 5:646a7e58989e | 421 | // same process happens for finishing the plateau: |
christodoulos | 5:646a7e58989e | 422 | |
christodoulos | 5:646a7e58989e | 423 | for(m=0; m<9; m++) { |
christodoulos | 5:646a7e58989e | 424 | //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 | 425 | myiAQ_Core.iAQ_Core_GetNewReading ( &myiAQ_Core_data ); //Measurement from iAQ-Core C |
christodoulos | 7:f37620a76a1d | 426 | wait(0.1); |
christodoulos | 7:f37620a76a1d | 427 | sgp30.IAQmeasure();//Measurement from SGP30 |
christodoulos | 7:f37620a76a1d | 428 | |
christodoulos | 7:f37620a76a1d | 429 | rslt = myBME680.bme680_get_sensor_data ( &data, &gas_sensor ); //Measurement for BME680 |
christodoulos | 7:f37620a76a1d | 430 | |
christodoulos | 7:f37620a76a1d | 431 | ZMODtemp.write(ZMODTEMPaddr,wInit,2); //maybe? |
christodoulos | 7:f37620a76a1d | 432 | ZMODtemp.write(ZMODTEMPaddr,wTemp,1); |
christodoulos | 7:f37620a76a1d | 433 | ZMODtemp.read(ZMODTEMPaddr,rTemp,2); //Returns 2 bytes |
christodoulos | 7:f37620a76a1d | 434 | float temp_code=(rTemp[0]<<8)+rTemp[1]; |
christodoulos | 7:f37620a76a1d | 435 | float SiTemp=((175.72*temp_code)/65536)-46.85; |
christodoulos | 7:f37620a76a1d | 436 | 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 | 437 | rslt = myBME680.bme680_set_sensor_mode ( &gas_sensor ); //BME680 reset |
christodoulos | 7:f37620a76a1d | 438 | |
christodoulos | 5:646a7e58989e | 439 | myArray[m]=carbon(); |
christodoulos | 5:646a7e58989e | 440 | wait(0.1); |
christodoulos | 5:646a7e58989e | 441 | } |
christodoulos | 5:646a7e58989e | 442 | while(flage==0) { |
christodoulos | 5:646a7e58989e | 443 | for(int m=0; m<9; m++) { |
christodoulos | 5:646a7e58989e | 444 | //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 | 445 | myiAQ_Core.iAQ_Core_GetNewReading ( &myiAQ_Core_data ); //Measurement from iAQ-Core C |
christodoulos | 7:f37620a76a1d | 446 | wait(0.1); |
christodoulos | 7:f37620a76a1d | 447 | sgp30.IAQmeasure();//Measurement from SGP30 |
christodoulos | 7:f37620a76a1d | 448 | |
christodoulos | 7:f37620a76a1d | 449 | rslt = myBME680.bme680_get_sensor_data ( &data, &gas_sensor ); //Measurement for BME680 |
christodoulos | 7:f37620a76a1d | 450 | |
christodoulos | 7:f37620a76a1d | 451 | ZMODtemp.write(ZMODTEMPaddr,wInit,2); //maybe? |
christodoulos | 7:f37620a76a1d | 452 | ZMODtemp.write(ZMODTEMPaddr,wTemp,1); |
christodoulos | 7:f37620a76a1d | 453 | ZMODtemp.read(ZMODTEMPaddr,rTemp,2); //Returns 2 bytes |
christodoulos | 7:f37620a76a1d | 454 | float temp_code=(rTemp[0]<<8)+rTemp[1]; |
christodoulos | 7:f37620a76a1d | 455 | float SiTemp=((175.72*temp_code)/65536)-46.85; |
christodoulos | 7:f37620a76a1d | 456 | 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 | 457 | rslt = myBME680.bme680_set_sensor_mode ( &gas_sensor ); //BME680 reset |
christodoulos | 7:f37620a76a1d | 458 | |
christodoulos | 5:646a7e58989e | 459 | sum2+=myArray[m]; |
christodoulos | 5:646a7e58989e | 460 | wait(0.1); |
christodoulos | 5:646a7e58989e | 461 | } |
christodoulos | 5:646a7e58989e | 462 | avg2=sum2/9; |
christodoulos | 5:646a7e58989e | 463 | for(int m=0; m<9; m++) { |
christodoulos | 5:646a7e58989e | 464 | //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 | 465 | myiAQ_Core.iAQ_Core_GetNewReading ( &myiAQ_Core_data ); //Measurement from iAQ-Core C |
christodoulos | 7:f37620a76a1d | 466 | wait(0.1); |
christodoulos | 7:f37620a76a1d | 467 | sgp30.IAQmeasure();//Measurement from SGP30 |
christodoulos | 7:f37620a76a1d | 468 | |
christodoulos | 7:f37620a76a1d | 469 | rslt = myBME680.bme680_get_sensor_data ( &data, &gas_sensor ); //Measurement for BME680 |
christodoulos | 7:f37620a76a1d | 470 | |
christodoulos | 7:f37620a76a1d | 471 | ZMODtemp.write(ZMODTEMPaddr,wInit,2); //maybe? |
christodoulos | 7:f37620a76a1d | 472 | ZMODtemp.write(ZMODTEMPaddr,wTemp,1); |
christodoulos | 7:f37620a76a1d | 473 | ZMODtemp.read(ZMODTEMPaddr,rTemp,2); //Returns 2 bytes |
christodoulos | 7:f37620a76a1d | 474 | float temp_code=(rTemp[0]<<8)+rTemp[1]; |
christodoulos | 7:f37620a76a1d | 475 | float SiTemp=((175.72*temp_code)/65536)-46.85; |
christodoulos | 7:f37620a76a1d | 476 | 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 | 477 | rslt = myBME680.bme680_set_sensor_mode ( &gas_sensor ); //BME680 reset |
christodoulos | 7:f37620a76a1d | 478 | |
christodoulos | 5:646a7e58989e | 479 | difSum2+=(myArray[m]-avg2)*(myArray[m]-avg2); |
christodoulos | 5:646a7e58989e | 480 | wait(0.1); |
christodoulos | 5:646a7e58989e | 481 | } |
christodoulos | 5:646a7e58989e | 482 | var2=difSum2/9; |
christodoulos | 5:646a7e58989e | 483 | sigma2=sqrt(var2); |
christodoulos | 5:646a7e58989e | 484 | if (sigma2<0.05) { |
christodoulos | 5:646a7e58989e | 485 | // here we defined the end threshold to be 0.05, it can be changed later based on experiment results |
christodoulos | 5:646a7e58989e | 486 | for(int m=0; m<8; m++) { |
christodoulos | 5:646a7e58989e | 487 | //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 | 488 | myiAQ_Core.iAQ_Core_GetNewReading ( &myiAQ_Core_data ); //Measurement from iAQ-Core C |
christodoulos | 7:f37620a76a1d | 489 | wait(0.1); |
christodoulos | 7:f37620a76a1d | 490 | sgp30.IAQmeasure();//Measurement from SGP30 |
christodoulos | 7:f37620a76a1d | 491 | |
christodoulos | 7:f37620a76a1d | 492 | rslt = myBME680.bme680_get_sensor_data ( &data, &gas_sensor ); //Measurement for BME680 |
christodoulos | 7:f37620a76a1d | 493 | |
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(), 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 | 500 | rslt = myBME680.bme680_set_sensor_mode ( &gas_sensor ); //BME680 reset |
christodoulos | 7:f37620a76a1d | 501 | |
christodoulos | 5:646a7e58989e | 502 | myArray[m]=myArray[m+1]; |
christodoulos | 5:646a7e58989e | 503 | wait(0.1); |
christodoulos | 5:646a7e58989e | 504 | |
mehrnaz | 2:ef98576cd67b | 505 | } |
christodoulos | 5:646a7e58989e | 506 | myArray[8]=carbon(); |
christodoulos | 5:646a7e58989e | 507 | } else { |
christodoulos | 6:f6faf142e5fc | 508 | sol=1; //Solenoid is OFF 1 |
christodoulos | 6:f6faf142e5fc | 509 | //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 | 510 | myiAQ_Core.iAQ_Core_GetNewReading ( &myiAQ_Core_data ); //Measurement from iAQ-Core C |
christodoulos | 5:646a7e58989e | 511 | wait(0.1); |
christodoulos | 7:f37620a76a1d | 512 | sgp30.IAQmeasure();//Measurement from SGP30 |
christodoulos | 7:f37620a76a1d | 513 | |
christodoulos | 7:f37620a76a1d | 514 | rslt = myBME680.bme680_get_sensor_data ( &data, &gas_sensor ); //Measurement for BME680 |
christodoulos | 7:f37620a76a1d | 515 | |
christodoulos | 7:f37620a76a1d | 516 | ZMODtemp.write(ZMODTEMPaddr,wInit,2); //maybe? |
christodoulos | 7:f37620a76a1d | 517 | ZMODtemp.write(ZMODTEMPaddr,wTemp,1); |
christodoulos | 7:f37620a76a1d | 518 | ZMODtemp.read(ZMODTEMPaddr,rTemp,2); //Returns 2 bytes |
christodoulos | 7:f37620a76a1d | 519 | float temp_code=(rTemp[0]<<8)+rTemp[1]; |
christodoulos | 7:f37620a76a1d | 520 | float SiTemp=((175.72*temp_code)/65536)-46.85; |
christodoulos | 7:f37620a76a1d | 521 | 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 | 522 | rslt = myBME680.bme680_set_sensor_mode ( &gas_sensor ); //BME680 reset |
christodoulos | 6:f6faf142e5fc | 523 | |
christodoulos | 7:f37620a76a1d | 524 | wait(0.1); |
christodoulos | 7:f37620a76a1d | 525 | |
christodoulos | 5:646a7e58989e | 526 | flage=1; //breakes the loop |
christodoulos | 5:646a7e58989e | 527 | } |
christodoulos | 5:646a7e58989e | 528 | } |
christodoulos | 6:f6faf142e5fc | 529 | |
christodoulos | 6:f6faf142e5fc | 530 | //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 | 531 | myiAQ_Core.iAQ_Core_GetNewReading ( &myiAQ_Core_data ); //Measurement from iAQ-Core C |
christodoulos | 7:f37620a76a1d | 532 | wait(0.1); |
christodoulos | 7:f37620a76a1d | 533 | sgp30.IAQmeasure();//Measurement from SGP30 |
christodoulos | 7:f37620a76a1d | 534 | |
christodoulos | 7:f37620a76a1d | 535 | rslt = myBME680.bme680_get_sensor_data ( &data, &gas_sensor ); //Measurement for BME680 |
christodoulos | 7:f37620a76a1d | 536 | |
christodoulos | 7:f37620a76a1d | 537 | ZMODtemp.write(ZMODTEMPaddr,wInit,2); //maybe? |
christodoulos | 7:f37620a76a1d | 538 | ZMODtemp.write(ZMODTEMPaddr,wTemp,1); |
christodoulos | 7:f37620a76a1d | 539 | ZMODtemp.read(ZMODTEMPaddr,rTemp,2); //Returns 2 bytes |
christodoulos | 7:f37620a76a1d | 540 | float temp_code=(rTemp[0]<<8)+rTemp[1]; |
christodoulos | 7:f37620a76a1d | 541 | float SiTemp=((175.72*temp_code)/65536)-46.85; |
christodoulos | 7:f37620a76a1d | 542 | 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 | 543 | rslt = myBME680.bme680_set_sensor_mode ( &gas_sensor ); //BME680 reset |
christodoulos | 7:f37620a76a1d | 544 | |
christodoulos | 6:f6faf142e5fc | 545 | //led=0.4f; //LED is back to full brightness |
christodoulos | 5:646a7e58989e | 546 | bf=0; //reset the detecting base flow flag |
christodoulos | 5:646a7e58989e | 547 | fin=1; //enables the next section flag |
christodoulos | 5:646a7e58989e | 548 | |
christodoulos | 5:646a7e58989e | 549 | |
christodoulos | 6:f6faf142e5fc | 550 | |
mehrnaz | 2:ef98576cd67b | 551 | } |
christodoulos | 5:646a7e58989e | 552 | |
christodoulos | 6:f6faf142e5fc | 553 | if(carbon()<2.00 && fin ==1) { |
christodoulos | 7:f37620a76a1d | 554 | //User has to wait for the CO2 level to drop less than 2% before testing again. |
christodoulos | 7:f37620a76a1d | 555 | //Once it is less than 2%, all the flags and parameters used in calculations are reset |
christodoulos | 7:f37620a76a1d | 556 | |
christodoulos | 7:f37620a76a1d | 557 | //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 | 558 | myiAQ_Core.iAQ_Core_GetNewReading ( &myiAQ_Core_data ); //Measurement from iAQ-Core C |
christodoulos | 7:f37620a76a1d | 559 | wait(0.1); |
christodoulos | 7:f37620a76a1d | 560 | sgp30.IAQmeasure();//Measurement from SGP30 |
christodoulos | 7:f37620a76a1d | 561 | |
christodoulos | 7:f37620a76a1d | 562 | rslt = myBME680.bme680_get_sensor_data ( &data, &gas_sensor ); //Measurement for BME680 |
christodoulos | 6:f6faf142e5fc | 563 | |
christodoulos | 7:f37620a76a1d | 564 | ZMODtemp.write(ZMODTEMPaddr,wInit,2); //maybe? |
christodoulos | 7:f37620a76a1d | 565 | ZMODtemp.write(ZMODTEMPaddr,wTemp,1); |
christodoulos | 7:f37620a76a1d | 566 | ZMODtemp.read(ZMODTEMPaddr,rTemp,2); //Returns 2 bytes |
christodoulos | 7:f37620a76a1d | 567 | float temp_code=(rTemp[0]<<8)+rTemp[1]; |
christodoulos | 7:f37620a76a1d | 568 | float SiTemp=((175.72*temp_code)/65536)-46.85; |
christodoulos | 7:f37620a76a1d | 569 | 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 | 570 | rslt = myBME680.bme680_set_sensor_mode ( &gas_sensor ); //BME680 reset |
christodoulos | 7:f37620a76a1d | 571 | |
christodoulos | 7:f37620a76a1d | 572 | stream.reset(); |
christodoulos | 7:f37620a76a1d | 573 | stream.stop(); |
christodoulos | 7:f37620a76a1d | 574 | measurement_started =0; |
christodoulos | 7:f37620a76a1d | 575 | solstart=0; |
christodoulos | 7:f37620a76a1d | 576 | sum=0; |
christodoulos | 7:f37620a76a1d | 577 | var=0.0; |
christodoulos | 7:f37620a76a1d | 578 | sigma=0.0; |
christodoulos | 7:f37620a76a1d | 579 | difSum=0; |
christodoulos | 7:f37620a76a1d | 580 | sum2=0; |
christodoulos | 7:f37620a76a1d | 581 | var2=0.0; |
christodoulos | 7:f37620a76a1d | 582 | sigma2=0.0; |
christodoulos | 7:f37620a76a1d | 583 | difSum2=0; |
christodoulos | 7:f37620a76a1d | 584 | avg2=0; |
christodoulos | 7:f37620a76a1d | 585 | avg=0; |
christodoulos | 7:f37620a76a1d | 586 | flags=0; |
christodoulos | 7:f37620a76a1d | 587 | flage=0; |
christodoulos | 7:f37620a76a1d | 588 | fin=0; |
christodoulos | 7:f37620a76a1d | 589 | } |
christodoulos | 5:646a7e58989e | 590 | |
mehrnaz | 2:ef98576cd67b | 591 | } |
mehrnaz | 2:ef98576cd67b | 592 | } |