#include "HX711_ADC.h"
#include <LiquidCrystal_I2C.h>
HX711_ADC LoadCell(6, 7);
LiquidCrystal_I2C lcd(0x27, 16, 2);
int buzzer = 11;
int button1 = 2;
int button2 = 3;
int button3 = 4;
int button4 = 5;
const int led = 12;
int taree = 8;
int num = 1;
int buttonState = 0;
int buttonState1 = 0;
int buttonState2 = 0;
float alarm1 = 11;
float alarm2 = 0;
float z1 = 28.3495;
void setup()

{
  pinMode(led, OUTPUT);
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  pinMode(button3, INPUT_PULLUP);
  pinMode(button4, INPUT_PULLUP);
  pinMode(buzzer, OUTPUT);
  pinMode(taree, INPUT_PULLUP);
  LoadCell.begin();
  LoadCell.start(1000);
  LoadCell.setCalFactor(100);

  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
}



void loop() {

  static unsigned long currentTime = millis();
  buttonState = digitalRead(button2);
  buttonState1 = digitalRead(button1);
  buttonState2 = digitalRead(button4);
  Serial.print(buttonState);
  Serial.print("     ");
  Serial.print(buttonState2);
  Serial.print("     ");
  Serial.print(buttonState1);
  Serial.print("     ");
  Serial.print(currentTime);
  Serial.print("     ");
  Serial.print(millis());
  Serial.print("     ");
  Serial.println(num);
  if (buttonState == 0)  // if button pressed,
    currentTime = millis();
  if (buttonState1 == 0)
    currentTime = millis();


  LoadCell.update();             // retrieves data from the load cell
  float i = LoadCell.getData();  // get output value
  float z = i / 28.3495;
  switch (num) {
    case 1:
      {
        if (digitalRead(button3) == LOW)
          alarm1 = 0;
        alarm2 = 0;

        lcd.setCursor(0, 0);  // set cursor to secon row
        lcd.print(i, 2);      // print out the retrieved value to the second row
        lcd.print("g");

        lcd.setCursor(0, 1);
        lcd.print(z, 2);
        lcd.print("oz");

        if (millis() - currentTime >= 3000)  // else
        {
          num = 1;
        } else
          num = num;
        break;
      }
    case 2:
      {
        if (digitalRead(button1) == LOW) {
          alarm1 = alarm1 + 1;
          delay(75);
        } else if (digitalRead(button2) == LOW) {
          alarm1 = alarm1 - 1;
          delay(75);
        }
        if (digitalRead(button3) == LOW)
          alarm1 = 0;

        lcd.setCursor(0, 0);   // set cursor to secon row
        lcd.print(alarm1, 2);  // print out the retrieved value to the second row
        lcd.print("g ");

        alarm2 = alarm1 / z1;
        lcd.setCursor(0, 1);
        lcd.print(alarm2, 2);
        lcd.print("oz ");

        if (millis() - currentTime >= 3000) {
          num = 1;
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("    Ayarlaniyor    ");
          delay(500);
          lcd.clear();
        } else
          num = num;
        break;
      }
    case 3:
      {
        if (digitalRead(button1) == LOW) {
          alarm2 = alarm2 + 1;
          delay(75);
        } else if (digitalRead(button2) == LOW) {
          alarm2 = alarm2 - 1;
          delay(75);
        }


        lcd.setCursor(0, 0);   // set cursor to secon row
        lcd.print(alarm1, 2);  // print out the retrieved value to the second row
        lcd.print("g");
        alarm1 = alarm2 * z1;
        lcd.setCursor(0, 1);
        lcd.print(alarm2, 2);
        lcd.print("oz");

        if (digitalRead(button3) == LOW)
          alarm2 = 0;

        if (millis() - currentTime >= 3000)  // else
        {
          num = 1;
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("    Scale    ");
          delay(500);
          lcd.clear();
        } else
          num = num;
        break;
      }
  }


  if (i < 2500) {
    i = 0;
    lcd.setCursor(0, 0);  // set cursor to secon row
    delay(100);
    lcd.clear();

  } else {
    lcd.setCursor(0, 0);
    lcd.print("TANKTA CAMUR VAR");
    lcd.setCursor(0, 1);
    lcd.print("TEMIZLIK YAPIN!");
    digitalWrite(led, HIGH);
    delay(300);
    digitalWrite(led, LOW);
    delay(100);
  }
  if (digitalRead(taree) == LOW) {
    lcd.clear();
    lcd.setCursor(0, 1);  // set cursor to secon row
    lcd.print("Dara Aliniyor... ");
    LoadCell.start(1000);
    lcd.setCursor(0, 0);
    lcd.print("                ");
    lcd.clear();
  }
  if (digitalRead(button4) == LOW) {
    {
      if (num > 2) num = 1;
      else num++;
      currentTime = millis();
      delay(300);
      lcd.clear();
      delay(300);
    }
    if (num == 3) {
      lcd.setCursor(0, 0);
      lcd.print("    Ayarla Oz    ");
      delay(500);
      lcd.clear();
    }

    else if (num == 2) {
      lcd.setCursor(0, 0);
      lcd.print("   Ayarla Gram    ");
      delay(500);
      lcd.clear();
    } else if (num == 1) {
      lcd.setCursor(0, 0);
      lcd.print("    Ayar    ");
      delay(500);
      lcd.clear();
    }
  }


  if (i == alarm1 && alarm1 > 0) {

    noTone(buzzer);
    delay(150);
    tone(buzzer, 1000);
    delay(500);
    noTone(buzzer);
    delay(150);
  }

  if (i > alarm1 && alarm1 > 0) {
 
   noTone(buzzer);
    delay(100);
    tone(buzzer, 2000);
    delay(100);
    tone(buzzer, 1000);
    delay(100);
    tone(buzzer, 2000);
    delay(100);
    tone(buzzer, 1000);
    delay(100);
    tone(buzzer, 2000);
    delay(100);
    tone(buzzer, 1000);
    delay(100);
    noTone(buzzer);
    delay(50);
  } else
    noTone(buzzer);
}