![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
measuring the salinity
Dependencies: LinearAnalogSensors mbed
Fork of mbed_measuring_temperature by
main.cpp@11:af86c88a58ba, 2016-06-16 (annotated)
- Committer:
- ekasinambela
- Date:
- Thu Jun 16 09:22:58 2016 +0000
- Revision:
- 11:af86c88a58ba
- Parent:
- 10:3d1ffeb39123
add median by using sort function and add alarm
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ekasinambela | 0:da915495c7fc | 1 | #include "mbed.h" |
ekasinambela | 3:5baf5ad00c1c | 2 | #include <cmath> |
ekasinambela | 7:b0d12907493f | 3 | #include <stdio.h> |
ekasinambela | 7:b0d12907493f | 4 | #include <math.h> |
ekasinambela | 11:af86c88a58ba | 5 | #include <algorithm> |
ekasinambela | 2:1aab63839dba | 6 | //#include <LinearTemp.h> |
ekasinambela | 7:b0d12907493f | 7 | #include "mbed.h" |
ekasinambela | 7:b0d12907493f | 8 | #include "TextLCD.h" |
ekasinambela | 2:1aab63839dba | 9 | |
ekasinambela | 7:b0d12907493f | 10 | TextLCD lcd(p19, p20, p21, p22, p23, p24); // rs, e, d4-d7 |
ekasinambela | 0:da915495c7fc | 11 | Serial pc(USBTX, USBRX); |
ekasinambela | 0:da915495c7fc | 12 | DigitalOut myled1(LED1), myled2(LED2); |
ekasinambela | 3:5baf5ad00c1c | 13 | AnalogIn pin_1(p16), pin_2(p15), pin_3(p17) ; |
ekasinambela | 6:9de0807e2855 | 14 | AnalogOut pin_18 (p18); |
ekasinambela | 10:3d1ffeb39123 | 15 | DigitalIn switchinput(p7); |
ekasinambela | 11:af86c88a58ba | 16 | DigitalOut redLed(p5); |
ekasinambela | 11:af86c88a58ba | 17 | DigitalOut alarm (p5); |
ekasinambela | 2:1aab63839dba | 18 | //LinearTemp mysensor(p15, 0.0050354, -273.15); |
ekasinambela | 2:1aab63839dba | 19 | |
ekasinambela | 5:d2b58a5771d7 | 20 | char program_name[128] = "G3-Waterplay Project"; |
ekasinambela | 7:b0d12907493f | 21 | float roundvalue = 0; |
ekasinambela | 11:af86c88a58ba | 22 | double Temperature = 0; |
ekasinambela | 11:af86c88a58ba | 23 | float Salinity = 0; |
mariosimaremare | 1:6476db5e52e8 | 24 | |
ekasinambela | 4:49b028845be4 | 25 | void salinityMeasurement(); |
ekasinambela | 4:49b028845be4 | 26 | void temperatureMeasurement(); |
ekasinambela | 10:3d1ffeb39123 | 27 | void initialization (); |
mariosimaremare | 1:6476db5e52e8 | 28 | int main() |
mariosimaremare | 1:6476db5e52e8 | 29 | { |
ekasinambela | 11:af86c88a58ba | 30 | //int number = 0; |
ekasinambela | 11:af86c88a58ba | 31 | temperatureMeasurement(); |
ekasinambela | 11:af86c88a58ba | 32 | lcd.printf("None \n"); |
ekasinambela | 11:af86c88a58ba | 33 | |
ekasinambela | 11:af86c88a58ba | 34 | while(true){ |
ekasinambela | 11:af86c88a58ba | 35 | if (Temperature >= 32){ |
ekasinambela | 11:af86c88a58ba | 36 | pin_18 = 0; |
ekasinambela | 11:af86c88a58ba | 37 | redLed = 1; |
ekasinambela | 11:af86c88a58ba | 38 | temperatureMeasurement(); |
ekasinambela | 11:af86c88a58ba | 39 | lcd.printf("Temp too high \n"); |
ekasinambela | 11:af86c88a58ba | 40 | } |
ekasinambela | 11:af86c88a58ba | 41 | |
ekasinambela | 11:af86c88a58ba | 42 | if(Temperature >= 28 && Temperature < 32 ){ |
ekasinambela | 11:af86c88a58ba | 43 | pin_18 = 0; |
ekasinambela | 11:af86c88a58ba | 44 | myled1 = 0; |
ekasinambela | 11:af86c88a58ba | 45 | redLed = 0; |
ekasinambela | 11:af86c88a58ba | 46 | temperatureMeasurement(); |
ekasinambela | 11:af86c88a58ba | 47 | salinityMeasurement(); |
ekasinambela | 11:af86c88a58ba | 48 | } |
ekasinambela | 11:af86c88a58ba | 49 | if (Temperature < 28){ |
ekasinambela | 11:af86c88a58ba | 50 | pin_18 = 1; |
ekasinambela | 11:af86c88a58ba | 51 | myled1 = 1; |
ekasinambela | 11:af86c88a58ba | 52 | temperatureMeasurement(); |
ekasinambela | 11:af86c88a58ba | 53 | lcd.printf("None \n"); |
ekasinambela | 11:af86c88a58ba | 54 | } |
ekasinambela | 11:af86c88a58ba | 55 | } |
ekasinambela | 10:3d1ffeb39123 | 56 | |
ekasinambela | 11:af86c88a58ba | 57 | //initialization(); |
ekasinambela | 11:af86c88a58ba | 58 | //while(1){ |
ekasinambela | 11:af86c88a58ba | 59 | // wait(2); |
ekasinambela | 11:af86c88a58ba | 60 | //salinityMeasurement(); |
ekasinambela | 11:af86c88a58ba | 61 | //temperatureMeasurement(); |
ekasinambela | 11:af86c88a58ba | 62 | //} |
ekasinambela | 11:af86c88a58ba | 63 | |
ekasinambela | 11:af86c88a58ba | 64 | /* |
ekasinambela | 11:af86c88a58ba | 65 | int number = 0; |
ekasinambela | 11:af86c88a58ba | 66 | temperatureMeasurement(); |
ekasinambela | 11:af86c88a58ba | 67 | salinityMeasurement(); |
ekasinambela | 11:af86c88a58ba | 68 | for (int i = 23 ; i < 35; i++){ |
ekasinambela | 11:af86c88a58ba | 69 | while (Temperature < (double)i){ |
ekasinambela | 11:af86c88a58ba | 70 | number++; |
ekasinambela | 11:af86c88a58ba | 71 | pin_18 = 1; |
ekasinambela | 11:af86c88a58ba | 72 | myled1 = 1; |
ekasinambela | 11:af86c88a58ba | 73 | if (number %100 == 0){ |
ekasinambela | 11:af86c88a58ba | 74 | temperatureMeasurement(); |
ekasinambela | 11:af86c88a58ba | 75 | salinityMeasurement(); |
ekasinambela | 11:af86c88a58ba | 76 | } |
ekasinambela | 11:af86c88a58ba | 77 | } |
ekasinambela | 11:af86c88a58ba | 78 | pin_18 = 0; |
ekasinambela | 11:af86c88a58ba | 79 | myled1 = 0; |
ekasinambela | 11:af86c88a58ba | 80 | redLed = 0; |
ekasinambela | 11:af86c88a58ba | 81 | |
ekasinambela | 11:af86c88a58ba | 82 | alarm = 1; |
ekasinambela | 11:af86c88a58ba | 83 | wait(0.2); |
ekasinambela | 11:af86c88a58ba | 84 | alarm = 0; |
ekasinambela | 11:af86c88a58ba | 85 | wait(0.5); |
ekasinambela | 11:af86c88a58ba | 86 | alarm = 1; |
ekasinambela | 11:af86c88a58ba | 87 | wait(0.2); |
ekasinambela | 11:af86c88a58ba | 88 | alarm = 0; |
ekasinambela | 11:af86c88a58ba | 89 | |
ekasinambela | 11:af86c88a58ba | 90 | while (switchinput!=1){ |
ekasinambela | 11:af86c88a58ba | 91 | wait(1); |
ekasinambela | 11:af86c88a58ba | 92 | temperatureMeasurement(); |
ekasinambela | 11:af86c88a58ba | 93 | salinityMeasurement(); |
ekasinambela | 11:af86c88a58ba | 94 | } |
ekasinambela | 11:af86c88a58ba | 95 | |
ekasinambela | 11:af86c88a58ba | 96 | temperatureMeasurement(); |
ekasinambela | 4:49b028845be4 | 97 | salinityMeasurement(); |
ekasinambela | 0:da915495c7fc | 98 | } |
ekasinambela | 11:af86c88a58ba | 99 | */ |
ekasinambela | 0:da915495c7fc | 100 | } |
ekasinambela | 4:49b028845be4 | 101 | |
ekasinambela | 4:49b028845be4 | 102 | //salinity function |
ekasinambela | 4:49b028845be4 | 103 | void salinityMeasurement(){ |
ekasinambela | 11:af86c88a58ba | 104 | float f, vin, divider, vout; |
ekasinambela | 11:af86c88a58ba | 105 | |
ekasinambela | 11:af86c88a58ba | 106 | float salt[100]; |
ekasinambela | 11:af86c88a58ba | 107 | |
ekasinambela | 11:af86c88a58ba | 108 | for (int i = 0; i < 100; i++){ |
ekasinambela | 11:af86c88a58ba | 109 | f = pin_1.read(); |
ekasinambela | 11:af86c88a58ba | 110 | vin = f * 3.3; |
ekasinambela | 11:af86c88a58ba | 111 | divider = (float)5/ (float)3; |
ekasinambela | 11:af86c88a58ba | 112 | vout = vin * divider; |
ekasinambela | 4:49b028845be4 | 113 | |
ekasinambela | 11:af86c88a58ba | 114 | //measure the salinity |
ekasinambela | 11:af86c88a58ba | 115 | salt[i] = 16.3 * vout; |
ekasinambela | 11:af86c88a58ba | 116 | } |
ekasinambela | 11:af86c88a58ba | 117 | |
ekasinambela | 11:af86c88a58ba | 118 | sort(salt, salt + 100); |
ekasinambela | 11:af86c88a58ba | 119 | |
ekasinambela | 11:af86c88a58ba | 120 | pc.printf("vin: %f, vout: %f, salt: %f \n\r", vin, vout, salt[50]); |
ekasinambela | 11:af86c88a58ba | 121 | roundvalue = (float)floor((salt[50]*100.0) + 0.5)/100.0; |
ekasinambela | 7:b0d12907493f | 122 | lcd.printf("Sal: %g \n", roundvalue); |
ekasinambela | 11:af86c88a58ba | 123 | Salinity = roundvalue; |
ekasinambela | 4:49b028845be4 | 124 | } |
ekasinambela | 4:49b028845be4 | 125 | |
ekasinambela | 4:49b028845be4 | 126 | //temperature function |
ekasinambela | 4:49b028845be4 | 127 | void temperatureMeasurement(){ |
ekasinambela | 4:49b028845be4 | 128 | //measure the temperature |
ekasinambela | 11:af86c88a58ba | 129 | double f2, vin, divider, vout, RT, K0, K1, K2, TKelvin; |
ekasinambela | 11:af86c88a58ba | 130 | double Celcius[100]; |
ekasinambela | 4:49b028845be4 | 131 | |
ekasinambela | 11:af86c88a58ba | 132 | for (int i = 0; i < 100; i++){ |
ekasinambela | 11:af86c88a58ba | 133 | f2 = pin_2.read(); |
ekasinambela | 11:af86c88a58ba | 134 | vin = f2 * 4.85; |
ekasinambela | 11:af86c88a58ba | 135 | divider = (double)5/ (double)3; |
ekasinambela | 11:af86c88a58ba | 136 | vout = vin * divider; |
ekasinambela | 4:49b028845be4 | 137 | |
ekasinambela | 11:af86c88a58ba | 138 | RT = (vout * (double)15000) / (4.85 - vout); |
ekasinambela | 11:af86c88a58ba | 139 | K0 = 0.00102119; |
ekasinambela | 11:af86c88a58ba | 140 | K1 = 0.000222468 * (log(RT)); |
ekasinambela | 11:af86c88a58ba | 141 | K2 = double (1.33342 * pow (10.0, -7.0)) * pow ((double)log(RT), 3.0); |
ekasinambela | 11:af86c88a58ba | 142 | TKelvin = 1.0 / (K0 + K1 + K2); |
ekasinambela | 11:af86c88a58ba | 143 | Celcius[i] = (TKelvin - 273.15)+5.0; |
ekasinambela | 11:af86c88a58ba | 144 | } |
ekasinambela | 6:9de0807e2855 | 145 | //pc.printf("K0: %f, K1: %f, K2: %f \n\r", K0, K1, K2); |
ekasinambela | 6:9de0807e2855 | 146 | //pc.printf("vin: %f, vout: %f, temperature: %f \n\r", vin, vout, Celcius); |
ekasinambela | 11:af86c88a58ba | 147 | |
ekasinambela | 11:af86c88a58ba | 148 | sort(Celcius, Celcius + 100); |
ekasinambela | 11:af86c88a58ba | 149 | |
ekasinambela | 11:af86c88a58ba | 150 | pc.printf("vin: %f, vout: %f, temperature: %f \n\r",vin, vout, Celcius[50]); |
ekasinambela | 11:af86c88a58ba | 151 | roundvalue = (float)floor((Celcius[50]*100.0) + 0.5)/100.0; |
ekasinambela | 7:b0d12907493f | 152 | lcd.printf("Temp: %g \n", roundvalue); |
ekasinambela | 11:af86c88a58ba | 153 | Temperature = roundvalue; |
ekasinambela | 6:9de0807e2855 | 154 | |
ekasinambela | 11:af86c88a58ba | 155 | /* |
ekasinambela | 11:af86c88a58ba | 156 | if (Celcius[50] >= 34){ |
ekasinambela | 11:af86c88a58ba | 157 | pin_18 = 0; |
ekasinambela | 11:af86c88a58ba | 158 | redLed = 1; |
ekasinambela | 11:af86c88a58ba | 159 | lcd.printf("The temperature is too high"); |
ekasinambela | 11:af86c88a58ba | 160 | }else if(Celcius[50] >= 28 && Celcius[50] < 34 ){ |
ekasinambela | 11:af86c88a58ba | 161 | pin_18 = 0; |
ekasinambela | 11:af86c88a58ba | 162 | myled1 = 0; |
ekasinambela | 11:af86c88a58ba | 163 | redLed = 0; |
ekasinambela | 11:af86c88a58ba | 164 | }else { |
ekasinambela | 6:9de0807e2855 | 165 | pin_18 = 1; |
ekasinambela | 6:9de0807e2855 | 166 | myled1 = 1; |
ekasinambela | 6:9de0807e2855 | 167 | } |
ekasinambela | 11:af86c88a58ba | 168 | */ |
ekasinambela | 4:49b028845be4 | 169 | } |
ekasinambela | 10:3d1ffeb39123 | 170 | |
ekasinambela | 10:3d1ffeb39123 | 171 | void initialization(){ |
ekasinambela | 10:3d1ffeb39123 | 172 | int message=0; |
ekasinambela | 10:3d1ffeb39123 | 173 | while (message<8){ |
ekasinambela | 10:3d1ffeb39123 | 174 | |
ekasinambela | 11:af86c88a58ba | 175 | if (message==0){ |
ekasinambela | 11:af86c88a58ba | 176 | lcd.printf("Check the water tank \n"); |
ekasinambela | 11:af86c88a58ba | 177 | } |
ekasinambela | 11:af86c88a58ba | 178 | if (message==1){ |
ekasinambela | 11:af86c88a58ba | 179 | lcd.printf("Check the solution tank \n"); |
ekasinambela | 11:af86c88a58ba | 180 | } |
ekasinambela | 11:af86c88a58ba | 181 | if (message==2){ |
ekasinambela | 11:af86c88a58ba | 182 | lcd.printf("Check the water tube \n"); |
ekasinambela | 11:af86c88a58ba | 183 | } |
ekasinambela | 11:af86c88a58ba | 184 | if (message==3){ |
ekasinambela | 11:af86c88a58ba | 185 | lcd.printf("Check the salinity tube \n"); |
ekasinambela | 11:af86c88a58ba | 186 | } |
ekasinambela | 11:af86c88a58ba | 187 | if (message==4){ |
ekasinambela | 11:af86c88a58ba | 188 | lcd.printf("Check the valves position \n"); |
ekasinambela | 11:af86c88a58ba | 189 | } |
ekasinambela | 11:af86c88a58ba | 190 | if (message==5){ |
ekasinambela | 11:af86c88a58ba | 191 | lcd.printf("Check the syrenge \n"); |
ekasinambela | 11:af86c88a58ba | 192 | } |
ekasinambela | 11:af86c88a58ba | 193 | if (message==6){ |
ekasinambela | 11:af86c88a58ba | 194 | lcd.printf("Check the sensor position \n"); |
ekasinambela | 11:af86c88a58ba | 195 | } |
ekasinambela | 11:af86c88a58ba | 196 | if (message==7){ |
ekasinambela | 11:af86c88a58ba | 197 | lcd.printf("Check the thermostat \n"); |
ekasinambela | 11:af86c88a58ba | 198 | } |
ekasinambela | 10:3d1ffeb39123 | 199 | |
ekasinambela | 11:af86c88a58ba | 200 | if (switchinput==1){ |
ekasinambela | 11:af86c88a58ba | 201 | message++; |
ekasinambela | 11:af86c88a58ba | 202 | wait(2); |
ekasinambela | 11:af86c88a58ba | 203 | lcd.cls(); |
ekasinambela | 11:af86c88a58ba | 204 | } |
ekasinambela | 10:3d1ffeb39123 | 205 | |
ekasinambela | 10:3d1ffeb39123 | 206 | } |
ekasinambela | 10:3d1ffeb39123 | 207 | |
ekasinambela | 10:3d1ffeb39123 | 208 | lcd.printf("System will start in 2 sec \n"); |
ekasinambela | 10:3d1ffeb39123 | 209 | wait(2); |
ekasinambela | 10:3d1ffeb39123 | 210 | lcd.cls(); |
ekasinambela | 10:3d1ffeb39123 | 211 | } |
ekasinambela | 10:3d1ffeb39123 | 212 | |
ekasinambela | 10:3d1ffeb39123 | 213 | |
ekasinambela | 10:3d1ffeb39123 | 214 | |
ekasinambela | 10:3d1ffeb39123 | 215 | |
ekasinambela | 4:49b028845be4 | 216 | |
ekasinambela | 4:49b028845be4 | 217 |