/* PH Meter! */
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
float calibration_value = 21.34;
int phval = 0; 
unsigned long int avgval; 
int buffer_arr[10],temp;

float phDesired; // PH setpoint
const float phThresh = 0.15; // Stop adjusting when within this of `pHDesired`.
const float adjustVol = 1.0; // mL
const float mlToMs = 1.0; // Calibrate to find this.
const int waitBetweenAdjustments = 180 * 100UL; // ms
const int primeTime = 1000; // ms. Time to prime pumps
const int loopDelay = 1000; // ms. Time between main loop polling

byte customChar[8] = {
	0b00000,
	0b00100,
	0b00100,
	0b11111,
	0b00100,
	0b00100,
	0b11111,
	0b00000
};

void setup() {
 Serial.begin(9600);
  lcd.init(); 
  lcd.begin(16, 2);
  lcd.createChar(0, customChar); 
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("   Welcome to      ");
  lcd.setCursor(0, 1);
  lcd.print("    PH Meter    ");
  delay(500);
  lcd.clear();
  pinMode(12, OUTPUT);  // sets the digital pin 1 as output
  pinMode(13, OUTPUT); // sets the digital pin 1 as output
     
}



  // Populate this function to calibrate dose volumes: It's easier
  // to work in volume than in time.
  float calibrate_volume(int volMl) {
      return 1.; // placeholder
  }

  void addVolume(int pump, float volMl) {
      float timeMs = mlToMs * volMl;

      digitalWrite(pump, HIGH);
      delay(timeMs);
      digitalWrite(pump, LOW);
  }

  void addPhModifier(int pump) {
      addVolume(pump, adjustVol);
  }

  // Run this function while the pumps' inputs are connected to the
  // pH up and down solutions, and output is plugged into a drain.
  void primePumps() {
      digitalWrite(12, HIGH);  // Turn on pump 1
      digitalWrite(13, HIGH);  // Turn on pump 2.
      delay(primeTime);
      digitalWrite(12, LOW);  // Turn off pump 1.
      digitalWrite(13, LOW);  // Turn off pump 2.
  }





void loop(void) {

  for(int i=0;i<10;i++) 
    { 
    buffer_arr[i]=analogRead(A0);
    delay(30);
    }
  for(int i=0;i<9;i++)
    {
      for(int j=i+1;j<10;j++)
      {
      if(buffer_arr[i]>buffer_arr[j])
          {
          temp=buffer_arr[i];
          buffer_arr[i]=buffer_arr[j];
          buffer_arr[j]=temp;
          }
      }
    }
  avgval=0;
  for(int i=2;i<8;i++)
  avgval+=buffer_arr[i];
  float volt=(float)avgval*5.0/1023/6; //taking avg of 6 values of PH 
  float ph_act = 2.8 * volt; // 0-5V => 0-14

  float phDesired=analogRead(A1)*5.0/1023*2.8;

  lcd.setCursor(0, 0);
  lcd.print("pH SP:");
  lcd.setCursor(6, 0);
  lcd.print(phDesired);  
  lcd.setCursor(11, 0);
  lcd.write((byte)8);
  lcd.setCursor(12, 0);
  lcd.print(phThresh);

  lcd.setCursor(0, 1);
  lcd.print("pH PV:");
  lcd.setCursor(6, 1);
  lcd.print(ph_act); 
  


    if (abs(ph_act - phDesired) > phThresh) {
        if  ((ph_act - phDesired) > phThresh) {
        addPhModifier(12);
        //delay(waitBetweenAdjustments);
        }
        else if ((ph_act - phDesired) < -phThresh) {
        addPhModifier(13);
        //delay(waitBetweenAdjustments);
        }
        delay(loopDelay);
        
    } 
    else {
        // do nothing
    }


  //delay(50);
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
lcd:GND
lcd:VCC
lcd:SDA
lcd:SCL
pot1:GND
pot1:SIG
pot1:VCC
led1:A
led1:C
led2:A
led2:C
r1:1
r1:2
r2:1
r2:2
pot2:GND
pot2:SIG
pot2:VCC