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