Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
7.5 kB
0
Indexable
Never
#include <LiquidCrystal.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define set A0   //set pin on A0
#define inc A1  //increment pin on A1
#define dec A2  //decrement pin on A2
//********Serial_Data**************
float data1 = 0;
float data2 = 0;
float data3 = 0;

//######################SETTINGS_FOR_EC_MEASUREMENT##############################

#define ECPump 10 
#define ONE_WIRE_BUS 6                    // Data wire For Temp Probe is plugged into pin 2 on the Arduino 
int R1=450;                                 //resistance provided for the votage 
int Ra=25;                                  //Resistance of powering Pins
int ECPin= A3;
int ECGround=A4;
int ECPower =A5;

float TemperatureCoef = 0.019;             //this changes depending on what chemical we are measuring
float K=3.3;                               //value from the calibration test
OneWire oneWire(ONE_WIRE_BUS);             // Setup a oneWire instance to communicate with any OneWire devices
const int TempProbePossitive =9;           //Temp Probe power connected to pin 9
const int TempProbeNegative=8;             //Temp Probe Negative connected to pin 8
DallasTemperature sensors(&oneWire);       // Pass our oneWire reference to Dallas Temperature.
float Temperature=0;
float EC=0;
float EC25 =0;
float finalEC=0;
float raw= 0;
float Vin= 5;
float Vdrop= 0;
float Rc= 0;
float buffer=0;
int ecMaxStatus=0; //flag to check if the Ec has reached its maximum value

//#################################LCD_SETTINGS###############################################


const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; // pin number on arduino
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

//void screen1(int ec,int ph,int temp);
//#################################PH_SETTINGS################################################
#define PHPump 7
float ph=0;
#define SensorPin A6            //pH meter Analog output to Arduino Analog Input 0
#define Offset -9.45            //deviation compensate
unsigned long int avgValue;     //Store the average value of the sensor feedback
//################################CHECK_TIMES################################################
long int EC_CheckTime=10000; // 30 seconds
long int PH_CheckTime=12000;
unsigned long int previousMillis = 0;
//################SET_EC&PH####################################################
float setEC=2.7;
float setPH=1;


void setup(){
  //***************LCD_SETUP****************
  lcd.begin(16,2);
  lcd.print("T  F  C");
  //****************EC&TEMP_SETUP*******************
  pinMode(TempProbeNegative , OUTPUT );             //seting ground pin as output for tmp probe
  digitalWrite(TempProbeNegative , LOW );           //Seting it to ground so it can sink current
  pinMode(TempProbePossitive , OUTPUT );            //ditto but for positive
  digitalWrite(TempProbePossitive , HIGH );  
  pinMode(ECPin,INPUT);
  pinMode(ECGround,OUTPUT);                         //setting pin for sinking current
  digitalWrite(ECGround,LOW);                      //We can leave the ground connected permanantly
  pinMode(ECPump,OUTPUT);
  digitalWrite(ECPump,LOW);
  pinMode(PHPump,OUTPUT);
  digitalWrite(PHPump,LOW);                                      // gives sensor time to settle
  
  sensors.begin();
  delay(100);
  R1=(R1+Ra);         
  //************************************************
  Serial.begin(9600);

}

void loop(){
  GetEC();
  GetPH();
  Data(Temperature,finalEC,ph);
  
  screen1(finalEC,ph,Temperature);
  delay(5000);
  
  
  unsigned long currentMillis=millis();
  if(currentMillis-previousMillis >= EC_CheckTime && Temperature>0)
  {
    previousMillis=currentMillis;
    if(finalEC<setEC)
    {
      increaseEC();
    }
    if(ph>setPH)
    {
      decreasePH();
    }
  }
  if(Temperature<=0)
  {
  lcd.clear();
  lcd.setCursor(1,0);
  lcd.print("Error in Temp!");
  lcd.setCursor(1,1);
  lcd.print("Waiting....");
  delay(200);
  }
  /*if(currentMillis-previousMillis >= PH_CheckTime)
  {
    previousMillis=currentMillis;
    if(ph>setPH)
    {
      decreasePH();
      delay(5000);
    }
  }
  screen1(finalEC,ph,Temperature);*/
  
 


}

//************************************************LCD FUNCTIONS****************************************************************************************************
void screen1(float ec,float ph,float temp){
  lcd.clear();
  lcd.setCursor(1,0);
  lcd.print("EC");
  lcd.setCursor(5, 0 );
  lcd.print("Ph");
  lcd.setCursor(9, 0 );
  lcd.print("Temp");

  lcd.setCursor(1,1);
  lcd.print(ec,1);
  lcd.setCursor(5,1);
  lcd.print(ph,0);
  lcd.setCursor(9,1);
  lcd.print(temp,1);
  delay(200);
  
}

//*************************************************************************SERIAL_DATA_TRANSFER*******************************************************************************************
void Data( float a, float b, float c)    
{
  data1 = a;
  data2 = b;
  data3 = c;
  
  Serial.print("t ");
  Serial.println(data1);
  
  Serial.print("e ");
  Serial.println(data2);
  
  Serial.print("p ");
  Serial.println(data3);
  
}
//***************************************************************ELECTRONIC_CONDUCTIVITY********************************************************************************
void increaseEC(void)
{
  lcd.clear();
  lcd.setCursor(4,0);
  lcd.print("EC Low!");
  lcd.setCursor(1,1);
  lcd.print("Increasing EC..");
  delay(200);
  digitalWrite(ECPump,HIGH);
  delay(5000);
  digitalWrite(ECPump,LOW);
  }
//****************************************************************
void GetEC(){
 
 
//*********Reading Temperature Of Solution *******************//
sensors.requestTemperatures();// Send the command to get temperatures
Temperature=sensors.getTempCByIndex(0); //Stores Value in Variable
 
//************Estimates Resistance of Liquid ****************//
digitalWrite(ECPower,HIGH);
raw= analogRead(ECPin);
raw= analogRead(ECPin);// This is not a mistake, First reading will be low beause if charged a capacitor
digitalWrite(ECPower,LOW);
delay(10);
 
//***************** Converts to EC **************************//
Vdrop= (Vin*raw)/1024.0;
Rc=(Vdrop*R1)/(Vin-Vdrop);
Rc=Rc-Ra; //acounting for Digital Pin Resitance
EC = 1000/(Rc*K);
 
 
//*************Compensating For Temperaure********************//
EC25  =  EC/ (1+ TemperatureCoef*(Temperature-25.0));
finalEC=EC25/10;
finalEC=finalEC*(-1);
 ;}
 //************************************************PH_FUNCTIONS********************************************************************************************

 void GetPH(){
  int buf[10];                //buffer for read analog
  for(int i=0;i<10;i++)       //Get 10 sample value from the sensor for smooth the value
  {
    buf[i]=analogRead(SensorPin);
    delay(10);
  }
  for(int i=0;i<9;i++)        //sort the analog from small to large
  {
    for(int j=i+1;j<10;j++)
    {
      if(buf[i]>buf[j])
      {
        int temp=buf[i];
        buf[i]=buf[j];
        buf[j]=temp;
      }
    }
  }
  avgValue=0;
  for(int i=2;i<8;i++)                      //take the average value of 6 center sample
    avgValue+=buf[i];
  float phValue=(float)avgValue*5/1024/6; //convert the analog into millivolt
  ph=8.10*phValue+Offset;                      //convert the millivolt into pH value

   
  delay(800);
}