#include "HX711_ADC.h"
#include <LiquidCrystal_I2C.h>
#include <Servo.h>

const int loadConstant = 420; //calibrate when build
const int subtractButton = 13;
const int addButton = 12;
const int tareButton = 11;
const int pourButton = 10;

float setWeight = 1;
int rotateTo = 180;
int rotateSetup = 0;
int oldAdd = HIGH;
int oldSubtract = HIGH;

bool confirmScreen = false;
bool pouring = false;
bool taring = false;

#define I2C_ADDR    0x27
#define LCD_COLUMNS 16
#define LCD_LINES   2

LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
HX711_ADC LoadCell(A1, A0);
Servo servo;


void setup() {
  Serial.begin(115200);
  pinMode(subtractButton, INPUT);
  pinMode(addButton, INPUT);
  pinMode(tareButton, INPUT);
  pinMode(pourButton, INPUT);
  servo.attach(9);
  servo.write(rotateSetup);
  lcd.init();
  lcd.backlight();
  LoadCell.begin(); 
  LoadCell.start(1000);
  LoadCell.setCalFactor(1);
  lcd.setCursor(0, 0);
  lcd.print(" Salt  Water:  L");
  lcd.setCursor(0, 1);
  lcd.print(" Press To Pour");
}
void askConfirmation(){
  confirmScreen = true;
  lcd.setCursor(0, 0);
  lcd.print(" Press Again To ");
  lcd.setCursor(0, 1);
  lcd.print(" Confirm:     L ");
  lcd.setCursor(10, 1);
  lcd.print(setWeight, 1);
  delay(300);
}

void pour(float set){
  tare();
  Serial.println(set);
  float load = LoadCell.getData() / loadConstant;
  while(load <= set){
    LoadCell.update(); 
    load = LoadCell.getData() / loadConstant;
    Serial.println(load);
    servo.write(rotateTo);
    pouring = true;
    if (pouring) {
      lcd.setCursor(0, 0);
      lcd.print("    Pouring:     ");
      lcd.setCursor(0, 1);
      lcd.print("  Do Not Touch   ");
    }
  }
  servo.write(rotateSetup);
  pouring = false;
}

void tare(){
  taring = true;
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("     Taring");
  lcd.setCursor(0, 1);
  lcd.print("  Do Not Touch!");
  LoadCell.start(1000);
  lcd.clear();
}

void loop() {
  LoadCell.update();
  int add = digitalRead(addButton);
  int subtract = digitalRead(subtractButton);
  if(add != oldAdd){
    if(add == HIGH && setWeight <= 7){
      setWeight += 0.5;
    }
    oldAdd = add;
  }
  if(subtract != oldSubtract){
    if(subtract == HIGH && setWeight >= 1){
      setWeight -= 0.5;
    }
    oldSubtract = subtract;
  }
  if (!confirmScreen && !pouring && !taring) {
    lcd.setCursor(0, 0);
    lcd.print("Salt  Water:");
    lcd.setCursor(0, 1);
    lcd.print(" Press To Pour");
    lcd.setCursor(12, 0);
    lcd.print(setWeight, 1);
    lcd.print("L");
  }
  if (!digitalRead(pourButton) && !taring) {
    askConfirmation();
  }
  if (confirmScreen && !pouring && !taring){
    if (!digitalRead(pourButton)) {
      pour(setWeight);
      lcd.setCursor(0, 0);
      lcd.print(" Salt  Water:  L");
      lcd.setCursor(0, 1);
      lcd.print(" Press To Pour");
      confirmScreen = false;
    }
  }
  if (!pouring && !digitalRead(tareButton)) {
    tare();
    confirmScreen = false;
    pouring = false;
    taring = false;
  }
}
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:GND.2
uno:RESET.2
uno:0
uno:1
uno:13
uno:3.3V
uno:AREF
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
uno:A6
uno:A7
uno:5V
uno:RESET
uno:GND.1
uno:VIN
uno:12.2
uno:5V.2
uno:13.2
uno:11.2
uno:RESET.3
uno:GND.3
cell1:VCC
cell1:DT
cell1:SCK
cell1:GND
lcd1:GND
lcd1:VCC
lcd1:SDA
lcd1:SCL
servo:GND
servo:V+
servo:PWM
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
btn2:1.l
btn2:2.l
btn2:1.r
btn2:2.r
btn3:1.l
btn3:2.l
btn3:1.r
btn3:2.r
btn4:1.l
btn4:2.l
btn4:1.r
btn4:2.r
r1:1
r1:2
r2:1
r2:2
r3:1
r3:2
r4:1
r4:2