POC Breath using SMD commercial sensors
Dependencies: iAQ_Core Adafruit_SGP30_mbed mbed BME680
flow.h@10:aad246b57873, 2020-06-14 (annotated)
- Committer:
- christodoulos
- Date:
- Sun Jun 14 15:47:13 2020 +0000
- Revision:
- 10:aad246b57873
- Parent:
- 7:f37620a76a1d
CO2 back to 9 samples
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mehrnaz | 2:ef98576cd67b | 1 | #include "mbed.h" |
mehrnaz | 2:ef98576cd67b | 2 | |
mehrnaz | 3:3d51f8870e91 | 3 | // First the inputs and pins are defined |
mehrnaz | 3:3d51f8870e91 | 4 | |
christodoulos | 5:646a7e58989e | 5 | AnalogIn flowIn(PA_1);//PA0 on schematic but PA1 on PCB |
mehrnaz | 3:3d51f8870e91 | 6 | Serial co2(PC_10,PC_11); |
mehrnaz | 3:3d51f8870e91 | 7 | AnalogIn sensor1(PC_0); |
mehrnaz | 3:3d51f8870e91 | 8 | AnalogIn sensor2(PC_1); |
mehrnaz | 3:3d51f8870e91 | 9 | AnalogIn sensor3(PC_2); |
mehrnaz | 3:3d51f8870e91 | 10 | AnalogIn sensor4(PC_3); |
christodoulos | 5:646a7e58989e | 11 | AnalogIn sensor5(PA_4); //Also 2612 |
christodoulos | 5:646a7e58989e | 12 | AnalogIn sensor6(PA_5); //Also 2610 |
christodoulos | 5:646a7e58989e | 13 | AnalogIn sensor7(PA_6); //Also 2620 |
christodoulos | 5:646a7e58989e | 14 | AnalogIn sensor8(PA_7); //Also 2602 |
christodoulos | 5:646a7e58989e | 15 | AnalogIn temp(PA_0);//PA1 on schematic but PA0 on PCB |
mehrnaz | 2:ef98576cd67b | 16 | |
mehrnaz | 3:3d51f8870e91 | 17 | ///////////////////// FLOW LOOP ///////////////////////// |
mehrnaz | 2:ef98576cd67b | 18 | float finalflow; |
mehrnaz | 2:ef98576cd67b | 19 | float flowVal1; |
mehrnaz | 2:ef98576cd67b | 20 | float flowVal2; |
mehrnaz | 2:ef98576cd67b | 21 | float P1; |
mehrnaz | 2:ef98576cd67b | 22 | int flag=0; |
mehrnaz | 2:ef98576cd67b | 23 | int o=0; |
mehrnaz | 2:ef98576cd67b | 24 | float bpArray[10]; |
christodoulos | 5:646a7e58989e | 25 | float fp; |
christodoulos | 5:646a7e58989e | 26 | float sp; |
mehrnaz | 2:ef98576cd67b | 27 | float FPressure; |
christodoulos | 5:646a7e58989e | 28 | |
mehrnaz | 2:ef98576cd67b | 29 | float flow() |
mehrnaz | 2:ef98576cd67b | 30 | { |
mehrnaz | 2:ef98576cd67b | 31 | while(1) |
mehrnaz | 2:ef98576cd67b | 32 | { |
mehrnaz | 2:ef98576cd67b | 33 | wait(0.01); |
mehrnaz | 2:ef98576cd67b | 34 | flowVal1=3.3*flowIn; //Logic level 3.3 |
mehrnaz | 2:ef98576cd67b | 35 | flowVal2 = 1.5*flowVal1; //5v |
mehrnaz | 3:3d51f8870e91 | 36 | P1 =(125*flowVal2)-62.5; //Pressure |
mehrnaz | 3:3d51f8870e91 | 37 | //making the value of pressure positive inside the SQRT function: |
mehrnaz | 2:ef98576cd67b | 38 | if(flag==0) |
mehrnaz | 2:ef98576cd67b | 39 | { |
mehrnaz | 2:ef98576cd67b | 40 | finalflow=0; |
mehrnaz | 2:ef98576cd67b | 41 | bpArray[o]=P1; |
mehrnaz | 2:ef98576cd67b | 42 | sp+=bpArray[o]; |
mehrnaz | 2:ef98576cd67b | 43 | o=o+1; |
christodoulos | 5:646a7e58989e | 44 | if (o==9) |
mehrnaz | 2:ef98576cd67b | 45 | { |
mehrnaz | 2:ef98576cd67b | 46 | fp=sp/10; |
mehrnaz | 2:ef98576cd67b | 47 | flag=1; |
mehrnaz | 2:ef98576cd67b | 48 | } |
mehrnaz | 2:ef98576cd67b | 49 | } |
mehrnaz | 2:ef98576cd67b | 50 | if (flag==1) |
mehrnaz | 2:ef98576cd67b | 51 | { |
christodoulos | 5:646a7e58989e | 52 | FPressure=P1-fp; |
christodoulos | 7:f37620a76a1d | 53 | finalflow=(0.0649*sqrt(FPressure))-0.3256; |
christodoulos | 7:f37620a76a1d | 54 | if(isnan(finalflow)==true ||finalflow<0){ |
christodoulos | 5:646a7e58989e | 55 | return 0; |
christodoulos | 5:646a7e58989e | 56 | } |
christodoulos | 5:646a7e58989e | 57 | else{ |
christodoulos | 5:646a7e58989e | 58 | return finalflow; |
christodoulos | 5:646a7e58989e | 59 | } |
mehrnaz | 2:ef98576cd67b | 60 | } |
mehrnaz | 2:ef98576cd67b | 61 | } |
mehrnaz | 2:ef98576cd67b | 62 | } |
mehrnaz | 2:ef98576cd67b | 63 | |
mehrnaz | 3:3d51f8870e91 | 64 | ///////////////////// CO2 LOOP ///////////////////////// |
mehrnaz | 3:3d51f8870e91 | 65 | int value; |
mehrnaz | 2:ef98576cd67b | 66 | float carbon() |
mehrnaz | 2:ef98576cd67b | 67 | { |
mehrnaz | 2:ef98576cd67b | 68 | bool allow = false; |
mehrnaz | 2:ef98576cd67b | 69 | char c; |
mehrnaz | 2:ef98576cd67b | 70 | char co2_measure[5]; |
mehrnaz | 2:ef98576cd67b | 71 | int count=0; |
mehrnaz | 2:ef98576cd67b | 72 | |
mehrnaz | 2:ef98576cd67b | 73 | while(1) |
mehrnaz | 2:ef98576cd67b | 74 | { |
mehrnaz | 2:ef98576cd67b | 75 | c = co2.getc(); |
mehrnaz | 3:3d51f8870e91 | 76 | //based on the user manual PDF for the CO2 sensor, the value starts with "Z" |
mehrnaz | 3:3d51f8870e91 | 77 | //and we need to extract the right number of CO2 value |
mehrnaz | 2:ef98576cd67b | 78 | if(c=='Z') { |
mehrnaz | 2:ef98576cd67b | 79 | allow = true; |
mehrnaz | 2:ef98576cd67b | 80 | } |
mehrnaz | 2:ef98576cd67b | 81 | |
mehrnaz | 2:ef98576cd67b | 82 | if(allow) { |
mehrnaz | 2:ef98576cd67b | 83 | if(c>=48 && c<=57) { |
mehrnaz | 2:ef98576cd67b | 84 | co2_measure[count]=c; |
mehrnaz | 2:ef98576cd67b | 85 | count++; |
mehrnaz | 2:ef98576cd67b | 86 | } |
mehrnaz | 2:ef98576cd67b | 87 | |
mehrnaz | 3:3d51f8870e91 | 88 | if(count>=6) { |
mehrnaz | 2:ef98576cd67b | 89 | value = ((co2_measure[0]-'0')*100000+co2_measure[1]-'0')*10000+(co2_measure[2]-'0')*1000+(co2_measure[3]-'0')*100; |
mehrnaz | 2:ef98576cd67b | 90 | float CAR; |
mehrnaz | 2:ef98576cd67b | 91 | CAR=(float)value/10000; |
mehrnaz | 2:ef98576cd67b | 92 | count=0; |
mehrnaz | 2:ef98576cd67b | 93 | allow=false; |
mehrnaz | 2:ef98576cd67b | 94 | return CAR; |
mehrnaz | 2:ef98576cd67b | 95 | } |
mehrnaz | 2:ef98576cd67b | 96 | } |
mehrnaz | 2:ef98576cd67b | 97 | } |
mehrnaz | 2:ef98576cd67b | 98 | } |
mehrnaz | 2:ef98576cd67b | 99 | |
mehrnaz | 3:3d51f8870e91 | 100 | ///////////////////// TEMPERATURE LOOP ///////////////////////// |
mehrnaz | 3:3d51f8870e91 | 101 | |
mehrnaz | 3:3d51f8870e91 | 102 | float t2Cel; |
mehrnaz | 3:3d51f8870e91 | 103 | |
mehrnaz | 3:3d51f8870e91 | 104 | float getTemp() |
mehrnaz | 3:3d51f8870e91 | 105 | { |
mehrnaz | 3:3d51f8870e91 | 106 | while(1) { |
christodoulos | 5:646a7e58989e | 107 | float B = 3380.00; //Define thermistor constant |
christodoulos | 5:646a7e58989e | 108 | float rRef=10000.00; // Define reference resistance |
christodoulos | 5:646a7e58989e | 109 | float r1=10000.00; // Define thermistor resistance at 25 C |
christodoulos | 5:646a7e58989e | 110 | float t1=25.00+273.00; // Define thermistor initial temperature s 25C in Kelvin |
mehrnaz | 3:3d51f8870e91 | 111 | float x = temp.read(); //Measure input voltage at pin A0 in bits |
mehrnaz | 3:3d51f8870e91 | 112 | float v = 3.3*x; //Convert bits into voltage |
christodoulos | 5:646a7e58989e | 113 | float r2 = (5*rRef/v)-rRef; //Convert voltage into thermistor resistance |
mehrnaz | 3:3d51f8870e91 | 114 | float t2 = (B*t1)/(B-t1*log(r1/r2)); //Convert thermistor resistance into temperature in Kelvin (log means natural logarithm ln) |
christodoulos | 5:646a7e58989e | 115 | t2Cel = t2-273.00; //Convert temperature from Kelvin to Celcius |
mehrnaz | 3:3d51f8870e91 | 116 | return t2Cel; |
mehrnaz | 3:3d51f8870e91 | 117 | // printf("Temp: %f\n", t2Cel); |
mehrnaz | 3:3d51f8870e91 | 118 | } |
mehrnaz | 3:3d51f8870e91 | 119 | } |
mehrnaz | 3:3d51f8870e91 | 120 | |
mehrnaz | 3:3d51f8870e91 | 121 | ///////////////////// 8-CHANNEL SENSOR LOOP ///////////////////////// |
mehrnaz | 3:3d51f8870e91 | 122 | |
christodoulos | 6:f6faf142e5fc | 123 | int c2612S0() |
christodoulos | 5:646a7e58989e | 124 | { |
christodoulos | 5:646a7e58989e | 125 | float sen5; |
christodoulos | 6:f6faf142e5fc | 126 | float v5; |
christodoulos | 6:f6faf142e5fc | 127 | float res5; |
christodoulos | 6:f6faf142e5fc | 128 | float r5; |
christodoulos | 6:f6faf142e5fc | 129 | |
christodoulos | 5:646a7e58989e | 130 | while(1){ |
christodoulos | 6:f6faf142e5fc | 131 | v5=3.3*sensor5; |
christodoulos | 6:f6faf142e5fc | 132 | res5=5/v5; |
christodoulos | 6:f6faf142e5fc | 133 | r5=res5-1; |
christodoulos | 6:f6faf142e5fc | 134 | sen5=1000*r5; |
christodoulos | 6:f6faf142e5fc | 135 | return (int)sen5; |
christodoulos | 6:f6faf142e5fc | 136 | } |
christodoulos | 6:f6faf142e5fc | 137 | } |
christodoulos | 6:f6faf142e5fc | 138 | |
christodoulos | 6:f6faf142e5fc | 139 | int c2612S1() |
christodoulos | 6:f6faf142e5fc | 140 | { |
christodoulos | 6:f6faf142e5fc | 141 | float sen5; |
christodoulos | 6:f6faf142e5fc | 142 | float v5; |
christodoulos | 6:f6faf142e5fc | 143 | float res5; |
christodoulos | 6:f6faf142e5fc | 144 | float r5; |
christodoulos | 6:f6faf142e5fc | 145 | |
christodoulos | 6:f6faf142e5fc | 146 | while(1){ |
christodoulos | 6:f6faf142e5fc | 147 | v5=(3.3*sensor5); |
christodoulos | 6:f6faf142e5fc | 148 | res5=5/v5; |
christodoulos | 6:f6faf142e5fc | 149 | r5=res5-1; |
christodoulos | 6:f6faf142e5fc | 150 | sen5=1000*r5; |
christodoulos | 5:646a7e58989e | 151 | return (int)sen5; |
christodoulos | 5:646a7e58989e | 152 | } |
christodoulos | 5:646a7e58989e | 153 | } |
christodoulos | 5:646a7e58989e | 154 | |
christodoulos | 6:f6faf142e5fc | 155 | int c2610S0() |
christodoulos | 5:646a7e58989e | 156 | { |
christodoulos | 5:646a7e58989e | 157 | float sen6; |
christodoulos | 6:f6faf142e5fc | 158 | float v6; |
christodoulos | 6:f6faf142e5fc | 159 | float res6; |
christodoulos | 6:f6faf142e5fc | 160 | float r6; |
christodoulos | 6:f6faf142e5fc | 161 | |
christodoulos | 5:646a7e58989e | 162 | while(1){ |
christodoulos | 6:f6faf142e5fc | 163 | v6=3.3*sensor6; |
christodoulos | 6:f6faf142e5fc | 164 | res6=5/v6; |
christodoulos | 6:f6faf142e5fc | 165 | r6=res6-1; |
christodoulos | 6:f6faf142e5fc | 166 | sen6=1000*r6; |
christodoulos | 6:f6faf142e5fc | 167 | return (int)sen6; |
christodoulos | 6:f6faf142e5fc | 168 | } |
christodoulos | 6:f6faf142e5fc | 169 | } |
christodoulos | 6:f6faf142e5fc | 170 | |
christodoulos | 6:f6faf142e5fc | 171 | int c2610S1() |
christodoulos | 6:f6faf142e5fc | 172 | { |
christodoulos | 6:f6faf142e5fc | 173 | float sen6; |
christodoulos | 6:f6faf142e5fc | 174 | float v6; |
christodoulos | 6:f6faf142e5fc | 175 | float res6; |
christodoulos | 6:f6faf142e5fc | 176 | float r6; |
christodoulos | 6:f6faf142e5fc | 177 | |
christodoulos | 6:f6faf142e5fc | 178 | while(1){ |
christodoulos | 6:f6faf142e5fc | 179 | v6=(3.3*sensor6); |
christodoulos | 6:f6faf142e5fc | 180 | res6=5/v6; |
christodoulos | 6:f6faf142e5fc | 181 | r6=res6-1; |
christodoulos | 6:f6faf142e5fc | 182 | sen6=1000*r6; |
christodoulos | 5:646a7e58989e | 183 | return (int)sen6; |
christodoulos | 5:646a7e58989e | 184 | } |
christodoulos | 5:646a7e58989e | 185 | } |
christodoulos | 5:646a7e58989e | 186 | |
christodoulos | 6:f6faf142e5fc | 187 | int c2620S0() |
mehrnaz | 3:3d51f8870e91 | 188 | { |
christodoulos | 5:646a7e58989e | 189 | float sen7; |
christodoulos | 6:f6faf142e5fc | 190 | float v7; |
christodoulos | 6:f6faf142e5fc | 191 | float res7; |
christodoulos | 6:f6faf142e5fc | 192 | float r7; |
christodoulos | 6:f6faf142e5fc | 193 | |
christodoulos | 5:646a7e58989e | 194 | while(1){ |
christodoulos | 6:f6faf142e5fc | 195 | v7=3.3*sensor7; |
christodoulos | 6:f6faf142e5fc | 196 | res7=5/v7; |
christodoulos | 6:f6faf142e5fc | 197 | r7=res7-1; |
christodoulos | 6:f6faf142e5fc | 198 | sen7=1000*r7; |
christodoulos | 5:646a7e58989e | 199 | return (int)sen7; |
christodoulos | 5:646a7e58989e | 200 | } |
christodoulos | 5:646a7e58989e | 201 | } |
christodoulos | 5:646a7e58989e | 202 | |
christodoulos | 6:f6faf142e5fc | 203 | int c2620S1() |
christodoulos | 6:f6faf142e5fc | 204 | { |
christodoulos | 6:f6faf142e5fc | 205 | float sen7; |
christodoulos | 6:f6faf142e5fc | 206 | float v7; |
christodoulos | 6:f6faf142e5fc | 207 | float res7; |
christodoulos | 6:f6faf142e5fc | 208 | float r7; |
christodoulos | 6:f6faf142e5fc | 209 | |
christodoulos | 6:f6faf142e5fc | 210 | while(1){ |
christodoulos | 6:f6faf142e5fc | 211 | v7=(3.3*sensor7); |
christodoulos | 6:f6faf142e5fc | 212 | res7=5/v7; |
christodoulos | 6:f6faf142e5fc | 213 | r7=res7-1; |
christodoulos | 6:f6faf142e5fc | 214 | sen7=1000*r7; |
christodoulos | 6:f6faf142e5fc | 215 | return (int)sen7; |
christodoulos | 6:f6faf142e5fc | 216 | } |
christodoulos | 6:f6faf142e5fc | 217 | } |
christodoulos | 6:f6faf142e5fc | 218 | |
christodoulos | 6:f6faf142e5fc | 219 | int c2602S0() |
christodoulos | 5:646a7e58989e | 220 | { |
christodoulos | 5:646a7e58989e | 221 | float sen8; |
christodoulos | 6:f6faf142e5fc | 222 | float v8; |
christodoulos | 6:f6faf142e5fc | 223 | float res8; |
christodoulos | 6:f6faf142e5fc | 224 | float r8; |
christodoulos | 6:f6faf142e5fc | 225 | |
mehrnaz | 3:3d51f8870e91 | 226 | while(1){ |
christodoulos | 6:f6faf142e5fc | 227 | v8=3.3*sensor8; |
christodoulos | 6:f6faf142e5fc | 228 | res8=5/v8; |
christodoulos | 6:f6faf142e5fc | 229 | r8=res8-1; |
christodoulos | 6:f6faf142e5fc | 230 | sen8=1000*r8; |
christodoulos | 6:f6faf142e5fc | 231 | return (int)sen8; |
christodoulos | 6:f6faf142e5fc | 232 | } |
christodoulos | 6:f6faf142e5fc | 233 | } |
christodoulos | 6:f6faf142e5fc | 234 | |
christodoulos | 6:f6faf142e5fc | 235 | int c2602S1() |
christodoulos | 6:f6faf142e5fc | 236 | { |
christodoulos | 6:f6faf142e5fc | 237 | float sen8; |
christodoulos | 6:f6faf142e5fc | 238 | float v8; |
christodoulos | 6:f6faf142e5fc | 239 | float res8; |
christodoulos | 6:f6faf142e5fc | 240 | float r8; |
christodoulos | 6:f6faf142e5fc | 241 | |
christodoulos | 6:f6faf142e5fc | 242 | while(1){ |
christodoulos | 6:f6faf142e5fc | 243 | v8=(3.3*sensor8); |
christodoulos | 6:f6faf142e5fc | 244 | res8=5/v8; |
christodoulos | 6:f6faf142e5fc | 245 | r8=res8-1; |
christodoulos | 6:f6faf142e5fc | 246 | sen8=1000*r8; |
christodoulos | 5:646a7e58989e | 247 | return (int)sen8; |
christodoulos | 5:646a7e58989e | 248 | } |
christodoulos | 5:646a7e58989e | 249 | } |
christodoulos | 5:646a7e58989e | 250 | |
christodoulos | 5:646a7e58989e | 251 | int s1() |
christodoulos | 5:646a7e58989e | 252 | { |
christodoulos | 5:646a7e58989e | 253 | int sen1; |
christodoulos | 5:646a7e58989e | 254 | while(1){ |
christodoulos | 5:646a7e58989e | 255 | sen1=((5E6)/(sensor1*3.3))-(5E6); |
mehrnaz | 3:3d51f8870e91 | 256 | return sen1; |
mehrnaz | 3:3d51f8870e91 | 257 | } |
mehrnaz | 3:3d51f8870e91 | 258 | } |
christodoulos | 5:646a7e58989e | 259 | int s2() |
mehrnaz | 3:3d51f8870e91 | 260 | { |
christodoulos | 5:646a7e58989e | 261 | int sen2; |
mehrnaz | 3:3d51f8870e91 | 262 | while(1){ |
christodoulos | 5:646a7e58989e | 263 | sen2=((5E6)/(sensor2*3.3))-(5E6); |
mehrnaz | 3:3d51f8870e91 | 264 | return sen2; |
mehrnaz | 3:3d51f8870e91 | 265 | } |
mehrnaz | 3:3d51f8870e91 | 266 | } |
christodoulos | 5:646a7e58989e | 267 | int s3() |
mehrnaz | 3:3d51f8870e91 | 268 | { |
christodoulos | 5:646a7e58989e | 269 | int sen3; |
mehrnaz | 3:3d51f8870e91 | 270 | while(1){ |
christodoulos | 5:646a7e58989e | 271 | sen3=((5E6)/(sensor3*3.3))-(5E6); |
mehrnaz | 3:3d51f8870e91 | 272 | return sen3; |
mehrnaz | 3:3d51f8870e91 | 273 | } |
mehrnaz | 3:3d51f8870e91 | 274 | } |
christodoulos | 5:646a7e58989e | 275 | int s4() |
mehrnaz | 3:3d51f8870e91 | 276 | { |
christodoulos | 5:646a7e58989e | 277 | int sen4; |
mehrnaz | 3:3d51f8870e91 | 278 | while(1){ |
christodoulos | 5:646a7e58989e | 279 | sen4=((5E6)/(sensor4*3.3))-(5E6); |
mehrnaz | 3:3d51f8870e91 | 280 | return sen4; |
mehrnaz | 3:3d51f8870e91 | 281 | } |
mehrnaz | 3:3d51f8870e91 | 282 | } |
christodoulos | 5:646a7e58989e | 283 | int s5() |
mehrnaz | 3:3d51f8870e91 | 284 | { |
christodoulos | 5:646a7e58989e | 285 | int sen5; |
mehrnaz | 3:3d51f8870e91 | 286 | while(1){ |
christodoulos | 5:646a7e58989e | 287 | sen5=((5E6)/(sensor5*3.3))-(5E6); |
mehrnaz | 3:3d51f8870e91 | 288 | return sen5; |
mehrnaz | 3:3d51f8870e91 | 289 | } |
mehrnaz | 3:3d51f8870e91 | 290 | } |
christodoulos | 5:646a7e58989e | 291 | int s6() |
mehrnaz | 3:3d51f8870e91 | 292 | { |
christodoulos | 5:646a7e58989e | 293 | int sen6; |
mehrnaz | 3:3d51f8870e91 | 294 | while(1){ |
christodoulos | 5:646a7e58989e | 295 | sen6=((5E6)/(sensor6*3.3))-(5E6); |
mehrnaz | 3:3d51f8870e91 | 296 | return sen6; |
mehrnaz | 3:3d51f8870e91 | 297 | } |
mehrnaz | 3:3d51f8870e91 | 298 | } |
christodoulos | 5:646a7e58989e | 299 | int s7() |
mehrnaz | 3:3d51f8870e91 | 300 | { |
christodoulos | 5:646a7e58989e | 301 | int sen7; |
mehrnaz | 3:3d51f8870e91 | 302 | while(1){ |
christodoulos | 5:646a7e58989e | 303 | sen7=((5E6)/(sensor7*3.3))-(5E6); |
mehrnaz | 3:3d51f8870e91 | 304 | return sen7; |
mehrnaz | 3:3d51f8870e91 | 305 | } |
mehrnaz | 3:3d51f8870e91 | 306 | } |
christodoulos | 5:646a7e58989e | 307 | int s8() |
mehrnaz | 3:3d51f8870e91 | 308 | { |
christodoulos | 5:646a7e58989e | 309 | int sen8; |
mehrnaz | 3:3d51f8870e91 | 310 | while(1){ |
christodoulos | 5:646a7e58989e | 311 | sen8=((5E6)/(sensor8*3.3))-(5E6); |
mehrnaz | 3:3d51f8870e91 | 312 | return sen8; |
mehrnaz | 3:3d51f8870e91 | 313 | } |
mehrnaz | 3:3d51f8870e91 | 314 | } |