measuring the salinity

Dependencies:   LinearAnalogSensors mbed

Fork of mbed_measuring_temperature by Mario Simaremare

Committer:
ekasinambela
Date:
Tue Jun 14 13:42:14 2016 +0000
Revision:
10:3d1ffeb39123
Parent:
7:b0d12907493f
Child:
11:af86c88a58ba
add the initial procedure before the system start

Who changed what in which revision?

UserRevisionLine numberNew 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 2:1aab63839dba 5 //#include <LinearTemp.h>
ekasinambela 7:b0d12907493f 6 #include "mbed.h"
ekasinambela 7:b0d12907493f 7 #include "TextLCD.h"
ekasinambela 2:1aab63839dba 8
ekasinambela 7:b0d12907493f 9 TextLCD lcd(p19, p20, p21, p22, p23, p24); // rs, e, d4-d7
ekasinambela 0:da915495c7fc 10 Serial pc(USBTX, USBRX);
ekasinambela 0:da915495c7fc 11 DigitalOut myled1(LED1), myled2(LED2);
ekasinambela 3:5baf5ad00c1c 12 AnalogIn pin_1(p16), pin_2(p15), pin_3(p17) ;
ekasinambela 6:9de0807e2855 13 AnalogOut pin_18 (p18);
ekasinambela 10:3d1ffeb39123 14 DigitalIn switchinput(p7);
ekasinambela 2:1aab63839dba 15 //LinearTemp mysensor(p15, 0.0050354, -273.15);
ekasinambela 2:1aab63839dba 16
ekasinambela 5:d2b58a5771d7 17 char program_name[128] = "G3-Waterplay Project";
ekasinambela 7:b0d12907493f 18 float roundvalue = 0;
mariosimaremare 1:6476db5e52e8 19
ekasinambela 4:49b028845be4 20 void salinityMeasurement();
ekasinambela 4:49b028845be4 21 void temperatureMeasurement();
ekasinambela 10:3d1ffeb39123 22 void initialization ();
mariosimaremare 1:6476db5e52e8 23 int main()
mariosimaremare 1:6476db5e52e8 24 {
ekasinambela 10:3d1ffeb39123 25
ekasinambela 10:3d1ffeb39123 26 initialization();
ekasinambela 10:3d1ffeb39123 27
ekasinambela 2:1aab63839dba 28 while(1){
ekasinambela 6:9de0807e2855 29 wait(2);
ekasinambela 4:49b028845be4 30 salinityMeasurement();
ekasinambela 4:49b028845be4 31 temperatureMeasurement();
ekasinambela 0:da915495c7fc 32 }
ekasinambela 0:da915495c7fc 33 }
ekasinambela 4:49b028845be4 34
ekasinambela 4:49b028845be4 35 //salinity function
ekasinambela 4:49b028845be4 36 void salinityMeasurement(){
ekasinambela 4:49b028845be4 37 float f = pin_1.read();
ekasinambela 4:49b028845be4 38
ekasinambela 4:49b028845be4 39 float vin = f * 3.3;
ekasinambela 4:49b028845be4 40 float divider = (float)5/ (float)3;
ekasinambela 4:49b028845be4 41 float vout = vin * divider;
ekasinambela 4:49b028845be4 42
ekasinambela 4:49b028845be4 43 //measure the salinity
ekasinambela 6:9de0807e2855 44 float salt = 16.3 * vout;
ekasinambela 6:9de0807e2855 45 pc.printf("vin: %f, vout: %f, salt: %f \n\r", vin, vout, salt);
ekasinambela 7:b0d12907493f 46 roundvalue = (float)floor((salt*100.0) + 0.5)/100.0;
ekasinambela 7:b0d12907493f 47 lcd.printf("Sal: %g \n", roundvalue);
ekasinambela 4:49b028845be4 48 }
ekasinambela 4:49b028845be4 49
ekasinambela 4:49b028845be4 50 //temperature function
ekasinambela 4:49b028845be4 51 void temperatureMeasurement(){
ekasinambela 4:49b028845be4 52 //measure the temperature
ekasinambela 4:49b028845be4 53 double f2 = pin_2.read();
ekasinambela 4:49b028845be4 54 double vin = f2 * 4.85;
ekasinambela 4:49b028845be4 55 double divider = (double)5/ (double)3;
ekasinambela 4:49b028845be4 56 double vout = vin * divider;
ekasinambela 4:49b028845be4 57
ekasinambela 4:49b028845be4 58 double RT = (vout * (double)15000) / (4.85 - vout);
ekasinambela 4:49b028845be4 59 double K0 = 0.00102119;
ekasinambela 4:49b028845be4 60 double K1 = 0.000222468 * (log(RT));
ekasinambela 4:49b028845be4 61 double K2 = double (1.33342 * pow (10.0, -7.0)) * pow ((double)log(RT), 3.0);
ekasinambela 4:49b028845be4 62 double TKelvin = 1.0 / (K0 + K1 + K2);
ekasinambela 4:49b028845be4 63 double Celcius = (TKelvin - 273.15)+5.0;
ekasinambela 4:49b028845be4 64
ekasinambela 6:9de0807e2855 65 //pc.printf("K0: %f, K1: %f, K2: %f \n\r", K0, K1, K2);
ekasinambela 6:9de0807e2855 66 //pc.printf("vin: %f, vout: %f, temperature: %f \n\r", vin, vout, Celcius);
ekasinambela 6:9de0807e2855 67 pc.printf("vin: %f, vout: %f, temperature: %f \n\r",vin, vout, Celcius);
ekasinambela 7:b0d12907493f 68 roundvalue = (float)floor((Celcius*100.0) + 0.5)/100.0;
ekasinambela 7:b0d12907493f 69 lcd.printf("Temp: %g \n", roundvalue);
ekasinambela 6:9de0807e2855 70
ekasinambela 7:b0d12907493f 71 if (Celcius < 30){
ekasinambela 6:9de0807e2855 72 pin_18 = 1;
ekasinambela 6:9de0807e2855 73 myled1 = 1;
ekasinambela 6:9de0807e2855 74 }else{
ekasinambela 6:9de0807e2855 75 pin_18 = 0;
ekasinambela 6:9de0807e2855 76 myled1 = 0;
ekasinambela 6:9de0807e2855 77 }
ekasinambela 4:49b028845be4 78 }
ekasinambela 10:3d1ffeb39123 79
ekasinambela 10:3d1ffeb39123 80 void initialization(){
ekasinambela 10:3d1ffeb39123 81 int message=0;
ekasinambela 10:3d1ffeb39123 82 while (message<8){
ekasinambela 10:3d1ffeb39123 83
ekasinambela 10:3d1ffeb39123 84
ekasinambela 10:3d1ffeb39123 85
ekasinambela 10:3d1ffeb39123 86 if (message==0){
ekasinambela 10:3d1ffeb39123 87 lcd.printf("Check the water tank \n");
ekasinambela 10:3d1ffeb39123 88 }
ekasinambela 10:3d1ffeb39123 89
ekasinambela 10:3d1ffeb39123 90 if (message==1){
ekasinambela 10:3d1ffeb39123 91 lcd.printf("Check the solution tank \n");
ekasinambela 10:3d1ffeb39123 92 }
ekasinambela 10:3d1ffeb39123 93 if (message==2){
ekasinambela 10:3d1ffeb39123 94 lcd.printf("Check the water tube \n");
ekasinambela 10:3d1ffeb39123 95 }
ekasinambela 10:3d1ffeb39123 96 if (message==3){
ekasinambela 10:3d1ffeb39123 97 lcd.printf("Check the salinity tube \n");
ekasinambela 10:3d1ffeb39123 98 }
ekasinambela 10:3d1ffeb39123 99 if (message==4){
ekasinambela 10:3d1ffeb39123 100 lcd.printf("Check the valves position \n");
ekasinambela 10:3d1ffeb39123 101 }
ekasinambela 10:3d1ffeb39123 102 if (message==5){
ekasinambela 10:3d1ffeb39123 103 lcd.printf("Check the syrenge \n");
ekasinambela 10:3d1ffeb39123 104 }
ekasinambela 10:3d1ffeb39123 105 if (message==6){
ekasinambela 10:3d1ffeb39123 106 lcd.printf("Check the sensor position \n");
ekasinambela 10:3d1ffeb39123 107 }
ekasinambela 10:3d1ffeb39123 108 if (message==7){
ekasinambela 10:3d1ffeb39123 109 lcd.printf("Check the thermostat \n");
ekasinambela 10:3d1ffeb39123 110 }
ekasinambela 10:3d1ffeb39123 111
ekasinambela 10:3d1ffeb39123 112 if (switchinput==1){
ekasinambela 10:3d1ffeb39123 113 message++;
ekasinambela 10:3d1ffeb39123 114 wait(2);
ekasinambela 10:3d1ffeb39123 115 lcd.cls();
ekasinambela 10:3d1ffeb39123 116 }
ekasinambela 10:3d1ffeb39123 117
ekasinambela 10:3d1ffeb39123 118 }
ekasinambela 10:3d1ffeb39123 119
ekasinambela 10:3d1ffeb39123 120 lcd.printf("System will start in 2 sec \n");
ekasinambela 10:3d1ffeb39123 121 wait(2);
ekasinambela 10:3d1ffeb39123 122 lcd.cls();
ekasinambela 10:3d1ffeb39123 123 }
ekasinambela 10:3d1ffeb39123 124
ekasinambela 10:3d1ffeb39123 125
ekasinambela 10:3d1ffeb39123 126
ekasinambela 10:3d1ffeb39123 127
ekasinambela 4:49b028845be4 128
ekasinambela 4:49b028845be4 129