measuring the salinity

Dependencies:   LinearAnalogSensors mbed

Fork of mbed_measuring_temperature by Mario Simaremare

main.cpp

Committer:
ekasinambela
Date:
2016-06-14
Revision:
10:3d1ffeb39123
Parent:
7:b0d12907493f
Child:
11:af86c88a58ba

File content as of revision 10:3d1ffeb39123:

#include "mbed.h"
#include <cmath>
#include <stdio.h>   
#include <math.h>
//#include <LinearTemp.h>
#include "mbed.h"
#include "TextLCD.h"

TextLCD lcd(p19, p20, p21, p22, p23, p24); // rs, e, d4-d7
Serial pc(USBTX, USBRX);
DigitalOut myled1(LED1), myled2(LED2);
AnalogIn pin_1(p16), pin_2(p15), pin_3(p17) ;
AnalogOut pin_18 (p18);
DigitalIn switchinput(p7);
//LinearTemp mysensor(p15, 0.0050354, -273.15); 
 
char program_name[128] = "G3-Waterplay Project";
float roundvalue = 0;

void salinityMeasurement();
void temperatureMeasurement();
void initialization ();
int main()
{
     
     initialization();    
     
     while(1){
        wait(2); 
        salinityMeasurement();
        temperatureMeasurement();
    }
}
    
//salinity function
void salinityMeasurement(){
    float f = pin_1.read();

    float vin = f * 3.3;
    float divider = (float)5/ (float)3;
    float vout = vin * divider;
    
    //measure the salinity
    float salt = 16.3 * vout; 
    pc.printf("vin: %f, vout: %f, salt: %f \n\r", vin, vout, salt);
    roundvalue = (float)floor((salt*100.0) + 0.5)/100.0;
    lcd.printf("Sal:  %g \n", roundvalue);
}

//temperature function
void temperatureMeasurement(){
    //measure the temperature
    double f2 = pin_2.read();
    double vin = f2 * 4.85;
    double divider = (double)5/ (double)3;
    double vout = vin * divider;
    
    double RT = (vout * (double)15000) / (4.85 - vout);
    double K0 = 0.00102119;
    double K1 = 0.000222468 * (log(RT));
    double K2 = double (1.33342 * pow (10.0, -7.0)) * pow ((double)log(RT), 3.0);
    double TKelvin = 1.0 / (K0 + K1 + K2);
    double Celcius = (TKelvin - 273.15)+5.0;
    
    //pc.printf("K0: %f, K1: %f, K2: %f \n\r", K0, K1, K2); 
    //pc.printf("vin: %f, vout: %f, temperature: %f \n\r", vin, vout, Celcius);
    pc.printf("vin: %f, vout: %f, temperature: %f \n\r",vin, vout, Celcius);
    roundvalue = (float)floor((Celcius*100.0) + 0.5)/100.0;
    lcd.printf("Temp: %g \n", roundvalue);
    
    if (Celcius < 30){
        pin_18 = 1;
        myled1 = 1;
    }else{
        pin_18 = 0;  
        myled1 = 0;      
    }
}

void initialization(){
int message=0;
    while (message<8){
    
    
    
    if (message==0){
        lcd.printf("Check the water tank  \n");
    }
        
    if (message==1){
        lcd.printf("Check the solution tank  \n");
    }
    if (message==2){
        lcd.printf("Check the water tube  \n");
    }
    if (message==3){
        lcd.printf("Check the salinity tube  \n");
    }
    if (message==4){
        lcd.printf("Check the valves position  \n");
    }
    if (message==5){
        lcd.printf("Check the syrenge  \n");
    }
    if (message==6){
        lcd.printf("Check the sensor position  \n");
    }
    if (message==7){
        lcd.printf("Check the thermostat  \n");
    }
    
    if (switchinput==1){
        message++;
        wait(2);
        lcd.cls();
    }
    
    }
    
    lcd.printf("System will start in 2 sec \n");
    wait(2);
    lcd.cls();
}