SGP30 (used in POCBreath_V2_smd_commercial)
Dependencies: Adafruit_SGP30_mbed mbed
flow.h@3:3d51f8870e91, 2019-07-25 (annotated)
- Committer:
- mehrnaz
- Date:
- Thu Jul 25 11:17:46 2019 +0000
- Revision:
- 3:3d51f8870e91
- Parent:
- 2:ef98576cd67b
- Child:
- 4:54dc2a95c130
final version of Handheld used for KST
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 | |
mehrnaz | 2:ef98576cd67b | 5 | AnalogIn flowIn(PA_0); |
mehrnaz | 3:3d51f8870e91 | 6 | Serial co2(PC_10,PC_11); |
mehrnaz | 2:ef98576cd67b | 7 | //Serial ttl(A1,A0); |
mehrnaz | 3:3d51f8870e91 | 8 | AnalogIn sensor1(PC_0); |
mehrnaz | 3:3d51f8870e91 | 9 | AnalogIn sensor2(PC_1); |
mehrnaz | 3:3d51f8870e91 | 10 | AnalogIn sensor3(PC_2); |
mehrnaz | 3:3d51f8870e91 | 11 | AnalogIn sensor4(PC_3); |
mehrnaz | 3:3d51f8870e91 | 12 | AnalogIn sensor5(PA_4); |
mehrnaz | 3:3d51f8870e91 | 13 | AnalogIn sensor6(PA_5); |
mehrnaz | 3:3d51f8870e91 | 14 | AnalogIn sensor7(PA_6); |
mehrnaz | 3:3d51f8870e91 | 15 | AnalogIn sensor8(PA_7); |
mehrnaz | 3:3d51f8870e91 | 16 | AnalogIn temp(PA_1); |
mehrnaz | 2:ef98576cd67b | 17 | |
mehrnaz | 3:3d51f8870e91 | 18 | ///////////////////// FLOW LOOP ///////////////////////// |
mehrnaz | 2:ef98576cd67b | 19 | float finalflow; |
mehrnaz | 2:ef98576cd67b | 20 | float flowVal1; |
mehrnaz | 2:ef98576cd67b | 21 | float flowVal2; |
mehrnaz | 2:ef98576cd67b | 22 | float P1; |
mehrnaz | 2:ef98576cd67b | 23 | int flag=0; |
mehrnaz | 2:ef98576cd67b | 24 | int o=0; |
mehrnaz | 2:ef98576cd67b | 25 | float bpArray[10]; |
mehrnaz | 2:ef98576cd67b | 26 | float fp;float sp; |
mehrnaz | 2:ef98576cd67b | 27 | float FPressure; |
mehrnaz | 2:ef98576cd67b | 28 | float flow() |
mehrnaz | 2:ef98576cd67b | 29 | { |
mehrnaz | 2:ef98576cd67b | 30 | while(1) |
mehrnaz | 2:ef98576cd67b | 31 | { |
mehrnaz | 2:ef98576cd67b | 32 | wait(0.01); |
mehrnaz | 2:ef98576cd67b | 33 | flowVal1=3.3*flowIn; //Logic level 3.3 |
mehrnaz | 2:ef98576cd67b | 34 | flowVal2 = 1.5*flowVal1; //5v |
mehrnaz | 3:3d51f8870e91 | 35 | P1 =(125*flowVal2)-62.5; //Pressure |
mehrnaz | 3:3d51f8870e91 | 36 | //making the value of pressure positive inside the SQRT function: |
mehrnaz | 2:ef98576cd67b | 37 | if(flag==0) |
mehrnaz | 2:ef98576cd67b | 38 | { |
mehrnaz | 2:ef98576cd67b | 39 | finalflow=0; |
mehrnaz | 2:ef98576cd67b | 40 | bpArray[o]=P1; |
mehrnaz | 2:ef98576cd67b | 41 | sp+=bpArray[o]; |
mehrnaz | 2:ef98576cd67b | 42 | o=o+1; |
mehrnaz | 2:ef98576cd67b | 43 | if (o=9) |
mehrnaz | 2:ef98576cd67b | 44 | { |
mehrnaz | 2:ef98576cd67b | 45 | fp=sp/10; |
mehrnaz | 2:ef98576cd67b | 46 | flag=1; |
mehrnaz | 2:ef98576cd67b | 47 | } |
mehrnaz | 2:ef98576cd67b | 48 | } |
mehrnaz | 2:ef98576cd67b | 49 | if (flag==1) |
mehrnaz | 2:ef98576cd67b | 50 | { |
mehrnaz | 2:ef98576cd67b | 51 | FPressure=P1-fp; |
mehrnaz | 2:ef98576cd67b | 52 | finalflow=(0.24*sqrt(FPressure)); //flow in litter per min |
mehrnaz | 2:ef98576cd67b | 53 | return finalflow; |
mehrnaz | 2:ef98576cd67b | 54 | } |
mehrnaz | 2:ef98576cd67b | 55 | } |
mehrnaz | 2:ef98576cd67b | 56 | } |
mehrnaz | 2:ef98576cd67b | 57 | |
mehrnaz | 3:3d51f8870e91 | 58 | ///////////////////// CO2 LOOP ///////////////////////// |
mehrnaz | 3:3d51f8870e91 | 59 | int value; |
mehrnaz | 2:ef98576cd67b | 60 | float carbon() |
mehrnaz | 2:ef98576cd67b | 61 | { |
mehrnaz | 2:ef98576cd67b | 62 | bool allow = false; |
mehrnaz | 2:ef98576cd67b | 63 | char c; |
mehrnaz | 2:ef98576cd67b | 64 | char co2_measure[5]; |
mehrnaz | 2:ef98576cd67b | 65 | int count=0; |
mehrnaz | 2:ef98576cd67b | 66 | |
mehrnaz | 2:ef98576cd67b | 67 | while(1) |
mehrnaz | 2:ef98576cd67b | 68 | { |
mehrnaz | 2:ef98576cd67b | 69 | c = co2.getc(); |
mehrnaz | 3:3d51f8870e91 | 70 | //based on the user manual PDF for the CO2 sensor, the value starts with "Z" |
mehrnaz | 3:3d51f8870e91 | 71 | //and we need to extract the right number of CO2 value |
mehrnaz | 2:ef98576cd67b | 72 | if(c=='Z') { |
mehrnaz | 2:ef98576cd67b | 73 | allow = true; |
mehrnaz | 2:ef98576cd67b | 74 | } |
mehrnaz | 2:ef98576cd67b | 75 | |
mehrnaz | 2:ef98576cd67b | 76 | if(allow) { |
mehrnaz | 2:ef98576cd67b | 77 | if(c>=48 && c<=57) { |
mehrnaz | 2:ef98576cd67b | 78 | co2_measure[count]=c; |
mehrnaz | 2:ef98576cd67b | 79 | count++; |
mehrnaz | 2:ef98576cd67b | 80 | } |
mehrnaz | 2:ef98576cd67b | 81 | |
mehrnaz | 3:3d51f8870e91 | 82 | if(count>=6) { |
mehrnaz | 2:ef98576cd67b | 83 | value = ((co2_measure[0]-'0')*100000+co2_measure[1]-'0')*10000+(co2_measure[2]-'0')*1000+(co2_measure[3]-'0')*100; |
mehrnaz | 2:ef98576cd67b | 84 | float CAR; |
mehrnaz | 2:ef98576cd67b | 85 | CAR=(float)value/10000; |
mehrnaz | 2:ef98576cd67b | 86 | count=0; |
mehrnaz | 2:ef98576cd67b | 87 | allow=false; |
mehrnaz | 2:ef98576cd67b | 88 | return CAR; |
mehrnaz | 2:ef98576cd67b | 89 | } |
mehrnaz | 2:ef98576cd67b | 90 | } |
mehrnaz | 2:ef98576cd67b | 91 | } |
mehrnaz | 2:ef98576cd67b | 92 | } |
mehrnaz | 2:ef98576cd67b | 93 | |
mehrnaz | 3:3d51f8870e91 | 94 | ///////////////////// TEMPERATURE LOOP ///////////////////////// |
mehrnaz | 3:3d51f8870e91 | 95 | |
mehrnaz | 3:3d51f8870e91 | 96 | float t2Cel; |
mehrnaz | 3:3d51f8870e91 | 97 | |
mehrnaz | 3:3d51f8870e91 | 98 | float getTemp() |
mehrnaz | 3:3d51f8870e91 | 99 | { |
mehrnaz | 3:3d51f8870e91 | 100 | while(1) { |
mehrnaz | 3:3d51f8870e91 | 101 | float B = 3478; //Define thermistor constant |
mehrnaz | 3:3d51f8870e91 | 102 | float rRef=10e3; // Define reference resistance |
mehrnaz | 3:3d51f8870e91 | 103 | float r1=10e3; // Define thermistor resistance at 25 C |
mehrnaz | 3:3d51f8870e91 | 104 | float t1=25+273; // Define thermistor initial temperature s 25C in Kelvin |
mehrnaz | 3:3d51f8870e91 | 105 | float x = temp.read(); //Measure input voltage at pin A0 in bits |
mehrnaz | 3:3d51f8870e91 | 106 | float v = 3.3*x; //Convert bits into voltage |
mehrnaz | 3:3d51f8870e91 | 107 | float r2 = (3.3*rRef/v)-rRef; //Convert voltage into thermistor resistance |
mehrnaz | 3:3d51f8870e91 | 108 | float t2 = (B*t1)/(B-t1*log(r1/r2)); //Convert thermistor resistance into temperature in Kelvin (log means natural logarithm ln) |
mehrnaz | 3:3d51f8870e91 | 109 | t2Cel = t2-273; //Convert temperature from Kelvin to Celcius |
mehrnaz | 3:3d51f8870e91 | 110 | return t2Cel; |
mehrnaz | 3:3d51f8870e91 | 111 | // printf("Temp: %f\n", t2Cel); |
mehrnaz | 3:3d51f8870e91 | 112 | } |
mehrnaz | 3:3d51f8870e91 | 113 | } |
mehrnaz | 3:3d51f8870e91 | 114 | |
mehrnaz | 3:3d51f8870e91 | 115 | ///////////////////// 8-CHANNEL SENSOR LOOP ///////////////////////// |
mehrnaz | 3:3d51f8870e91 | 116 | |
mehrnaz | 3:3d51f8870e91 | 117 | float s1() |
mehrnaz | 3:3d51f8870e91 | 118 | { |
mehrnaz | 3:3d51f8870e91 | 119 | float sen1; |
mehrnaz | 3:3d51f8870e91 | 120 | while(1){ |
mehrnaz | 3:3d51f8870e91 | 121 | sen1=sensor1*3.3; |
mehrnaz | 3:3d51f8870e91 | 122 | return sen1; |
mehrnaz | 3:3d51f8870e91 | 123 | } |
mehrnaz | 3:3d51f8870e91 | 124 | } |
mehrnaz | 3:3d51f8870e91 | 125 | float s2() |
mehrnaz | 3:3d51f8870e91 | 126 | { |
mehrnaz | 3:3d51f8870e91 | 127 | float sen2; |
mehrnaz | 3:3d51f8870e91 | 128 | while(1){ |
mehrnaz | 3:3d51f8870e91 | 129 | sen2=sensor2*3.3; |
mehrnaz | 3:3d51f8870e91 | 130 | return sen2; |
mehrnaz | 3:3d51f8870e91 | 131 | } |
mehrnaz | 3:3d51f8870e91 | 132 | } |
mehrnaz | 3:3d51f8870e91 | 133 | float s3() |
mehrnaz | 3:3d51f8870e91 | 134 | { |
mehrnaz | 3:3d51f8870e91 | 135 | float sen3; |
mehrnaz | 3:3d51f8870e91 | 136 | while(1){ |
mehrnaz | 3:3d51f8870e91 | 137 | sen3=sensor3*3.3; |
mehrnaz | 3:3d51f8870e91 | 138 | return sen3; |
mehrnaz | 3:3d51f8870e91 | 139 | } |
mehrnaz | 3:3d51f8870e91 | 140 | } |
mehrnaz | 3:3d51f8870e91 | 141 | float s4() |
mehrnaz | 3:3d51f8870e91 | 142 | { |
mehrnaz | 3:3d51f8870e91 | 143 | float sen4; |
mehrnaz | 3:3d51f8870e91 | 144 | while(1){ |
mehrnaz | 3:3d51f8870e91 | 145 | sen4=sensor4*3.3; |
mehrnaz | 3:3d51f8870e91 | 146 | return sen4; |
mehrnaz | 3:3d51f8870e91 | 147 | } |
mehrnaz | 3:3d51f8870e91 | 148 | } |
mehrnaz | 3:3d51f8870e91 | 149 | float s5() |
mehrnaz | 3:3d51f8870e91 | 150 | { |
mehrnaz | 3:3d51f8870e91 | 151 | float sen5; |
mehrnaz | 3:3d51f8870e91 | 152 | while(1){ |
mehrnaz | 3:3d51f8870e91 | 153 | sen5=sensor5*3.3; |
mehrnaz | 3:3d51f8870e91 | 154 | return sen5; |
mehrnaz | 3:3d51f8870e91 | 155 | } |
mehrnaz | 3:3d51f8870e91 | 156 | } |
mehrnaz | 3:3d51f8870e91 | 157 | float s6() |
mehrnaz | 3:3d51f8870e91 | 158 | { |
mehrnaz | 3:3d51f8870e91 | 159 | float sen6; |
mehrnaz | 3:3d51f8870e91 | 160 | while(1){ |
mehrnaz | 3:3d51f8870e91 | 161 | sen6=sensor6*3.3; |
mehrnaz | 3:3d51f8870e91 | 162 | return sen6; |
mehrnaz | 3:3d51f8870e91 | 163 | } |
mehrnaz | 3:3d51f8870e91 | 164 | } |
mehrnaz | 3:3d51f8870e91 | 165 | float s7() |
mehrnaz | 3:3d51f8870e91 | 166 | { |
mehrnaz | 3:3d51f8870e91 | 167 | float sen7; |
mehrnaz | 3:3d51f8870e91 | 168 | while(1){ |
mehrnaz | 3:3d51f8870e91 | 169 | sen7=sensor7*3.3; |
mehrnaz | 3:3d51f8870e91 | 170 | return sen7; |
mehrnaz | 3:3d51f8870e91 | 171 | } |
mehrnaz | 3:3d51f8870e91 | 172 | } |
mehrnaz | 3:3d51f8870e91 | 173 | float s8() |
mehrnaz | 3:3d51f8870e91 | 174 | { |
mehrnaz | 3:3d51f8870e91 | 175 | float sen8; |
mehrnaz | 3:3d51f8870e91 | 176 | while(1){ |
mehrnaz | 3:3d51f8870e91 | 177 | sen8=sensor8*3.3; |
mehrnaz | 3:3d51f8870e91 | 178 | return sen8; |
mehrnaz | 3:3d51f8870e91 | 179 | } |
mehrnaz | 3:3d51f8870e91 | 180 | } |