measuring the salinity
Dependencies: LinearAnalogSensors mbed
Fork of mbed_measuring_temperature by
main.cpp
00001 #include "mbed.h" 00002 #include <cmath> 00003 #include <stdio.h> 00004 #include <math.h> 00005 #include <algorithm> 00006 //#include <LinearTemp.h> 00007 #include "mbed.h" 00008 #include "TextLCD.h" 00009 00010 TextLCD lcd(p19, p20, p21, p22, p23, p24); // rs, e, d4-d7 00011 Serial pc(USBTX, USBRX); 00012 DigitalOut myled1(LED1), myled2(LED2); 00013 AnalogIn pin_1(p16), pin_2(p15), pin_3(p17) ; 00014 AnalogOut pin_18 (p18); 00015 DigitalIn switchinput(p7); 00016 DigitalOut redLed(p5); 00017 DigitalOut alarm (p5); 00018 //LinearTemp mysensor(p15, 0.0050354, -273.15); 00019 00020 char program_name[128] = "G3-Waterplay Project"; 00021 float roundvalue = 0; 00022 double Temperature = 0; 00023 float Salinity = 0; 00024 00025 void salinityMeasurement(); 00026 void temperatureMeasurement(); 00027 void initialization (); 00028 int main() 00029 { 00030 //int number = 0; 00031 temperatureMeasurement(); 00032 lcd.printf("None \n"); 00033 00034 while(true){ 00035 if (Temperature >= 32){ 00036 pin_18 = 0; 00037 redLed = 1; 00038 temperatureMeasurement(); 00039 lcd.printf("Temp too high \n"); 00040 } 00041 00042 if(Temperature >= 28 && Temperature < 32 ){ 00043 pin_18 = 0; 00044 myled1 = 0; 00045 redLed = 0; 00046 temperatureMeasurement(); 00047 salinityMeasurement(); 00048 } 00049 if (Temperature < 28){ 00050 pin_18 = 1; 00051 myled1 = 1; 00052 temperatureMeasurement(); 00053 lcd.printf("None \n"); 00054 } 00055 } 00056 00057 //initialization(); 00058 //while(1){ 00059 // wait(2); 00060 //salinityMeasurement(); 00061 //temperatureMeasurement(); 00062 //} 00063 00064 /* 00065 int number = 0; 00066 temperatureMeasurement(); 00067 salinityMeasurement(); 00068 for (int i = 23 ; i < 35; i++){ 00069 while (Temperature < (double)i){ 00070 number++; 00071 pin_18 = 1; 00072 myled1 = 1; 00073 if (number %100 == 0){ 00074 temperatureMeasurement(); 00075 salinityMeasurement(); 00076 } 00077 } 00078 pin_18 = 0; 00079 myled1 = 0; 00080 redLed = 0; 00081 00082 alarm = 1; 00083 wait(0.2); 00084 alarm = 0; 00085 wait(0.5); 00086 alarm = 1; 00087 wait(0.2); 00088 alarm = 0; 00089 00090 while (switchinput!=1){ 00091 wait(1); 00092 temperatureMeasurement(); 00093 salinityMeasurement(); 00094 } 00095 00096 temperatureMeasurement(); 00097 salinityMeasurement(); 00098 } 00099 */ 00100 } 00101 00102 //salinity function 00103 void salinityMeasurement(){ 00104 float f, vin, divider, vout; 00105 00106 float salt[100]; 00107 00108 for (int i = 0; i < 100; i++){ 00109 f = pin_1.read(); 00110 vin = f * 3.3; 00111 divider = (float)5/ (float)3; 00112 vout = vin * divider; 00113 00114 //measure the salinity 00115 salt[i] = 16.3 * vout; 00116 } 00117 00118 sort(salt, salt + 100); 00119 00120 pc.printf("vin: %f, vout: %f, salt: %f \n\r", vin, vout, salt[50]); 00121 roundvalue = (float)floor((salt[50]*100.0) + 0.5)/100.0; 00122 lcd.printf("Sal: %g \n", roundvalue); 00123 Salinity = roundvalue; 00124 } 00125 00126 //temperature function 00127 void temperatureMeasurement(){ 00128 //measure the temperature 00129 double f2, vin, divider, vout, RT, K0, K1, K2, TKelvin; 00130 double Celcius[100]; 00131 00132 for (int i = 0; i < 100; i++){ 00133 f2 = pin_2.read(); 00134 vin = f2 * 4.85; 00135 divider = (double)5/ (double)3; 00136 vout = vin * divider; 00137 00138 RT = (vout * (double)15000) / (4.85 - vout); 00139 K0 = 0.00102119; 00140 K1 = 0.000222468 * (log(RT)); 00141 K2 = double (1.33342 * pow (10.0, -7.0)) * pow ((double)log(RT), 3.0); 00142 TKelvin = 1.0 / (K0 + K1 + K2); 00143 Celcius[i] = (TKelvin - 273.15)+5.0; 00144 } 00145 //pc.printf("K0: %f, K1: %f, K2: %f \n\r", K0, K1, K2); 00146 //pc.printf("vin: %f, vout: %f, temperature: %f \n\r", vin, vout, Celcius); 00147 00148 sort(Celcius, Celcius + 100); 00149 00150 pc.printf("vin: %f, vout: %f, temperature: %f \n\r",vin, vout, Celcius[50]); 00151 roundvalue = (float)floor((Celcius[50]*100.0) + 0.5)/100.0; 00152 lcd.printf("Temp: %g \n", roundvalue); 00153 Temperature = roundvalue; 00154 00155 /* 00156 if (Celcius[50] >= 34){ 00157 pin_18 = 0; 00158 redLed = 1; 00159 lcd.printf("The temperature is too high"); 00160 }else if(Celcius[50] >= 28 && Celcius[50] < 34 ){ 00161 pin_18 = 0; 00162 myled1 = 0; 00163 redLed = 0; 00164 }else { 00165 pin_18 = 1; 00166 myled1 = 1; 00167 } 00168 */ 00169 } 00170 00171 void initialization(){ 00172 int message=0; 00173 while (message<8){ 00174 00175 if (message==0){ 00176 lcd.printf("Check the water tank \n"); 00177 } 00178 if (message==1){ 00179 lcd.printf("Check the solution tank \n"); 00180 } 00181 if (message==2){ 00182 lcd.printf("Check the water tube \n"); 00183 } 00184 if (message==3){ 00185 lcd.printf("Check the salinity tube \n"); 00186 } 00187 if (message==4){ 00188 lcd.printf("Check the valves position \n"); 00189 } 00190 if (message==5){ 00191 lcd.printf("Check the syrenge \n"); 00192 } 00193 if (message==6){ 00194 lcd.printf("Check the sensor position \n"); 00195 } 00196 if (message==7){ 00197 lcd.printf("Check the thermostat \n"); 00198 } 00199 00200 if (switchinput==1){ 00201 message++; 00202 wait(2); 00203 lcd.cls(); 00204 } 00205 00206 } 00207 00208 lcd.printf("System will start in 2 sec \n"); 00209 wait(2); 00210 lcd.cls(); 00211 } 00212 00213 00214 00215 00216 00217
Generated on Wed Jul 13 2022 20:06:42 by
1.7.2
