#include <HX711_ADC.h>
#if defined(ESP8266)|| defined(ESP32) || defined(AVR)
#include <EEPROM.h>
#endif

const int HX711_dout_1 = 2;
const int HX711_sck_1 = 4;
//loadcell DKiri
const int HX711_dout_2 = 18;
const int HX711_sck_2 = 19;
//loadcell Bkanan
const int HX711_dout_3 = 26;
const int HX711_sck_3 = 27;
//loadcell Bkiri
const int HX711_dout_4 = 32;
const int HX711_sck_4 = 33;
//HX711 constructor:
HX711_ADC LoadCell_1(HX711_dout_1, HX711_sck_1);
HX711_ADC LoadCell_2(HX711_dout_2, HX711_sck_2);
HX711_ADC LoadCell_3(HX711_dout_3, HX711_sck_3); //HX711 1
HX711_ADC LoadCell_4(HX711_dout_4, HX711_sck_4);

float loadcell1, loadcell2, loadcell3, loadcell4;



void setup() {
  float calibrationValue = 345.48;
  Serial.begin(9600);
  LoadCell_1.begin();
  LoadCell_2.begin();
  LoadCell_3.begin();
  LoadCell_4.begin();

  LoadCell_1.start(2000, true);
  LoadCell_2.start(2000, true);
  LoadCell_3.start(2000, true);
  LoadCell_4.start(2000, true);

  //Serial.println("memulai....");
  if (LoadCell_1.getTareTimeoutFlag());
  if (LoadCell_2.getTareTimeoutFlag());
  if (LoadCell_3.getTareTimeoutFlag());
  if (LoadCell_4.getTareTimeoutFlag());
  else {
    LoadCell_1.setCalFactor(calibrationValue);
    LoadCell_2.setCalFactor(calibrationValue);
    LoadCell_3.setCalFactor(calibrationValue);
    LoadCell_4.setCalFactor(calibrationValue);

  }
}

void loop() {
  Serial.print(" | Loadcell_1 : ");
  Serial.print(LoadCell_1.getData());
  Serial.print(" | Loadcell_2 : ");
  Serial.print(LoadCell_2.getData());
  Serial.print(" | Loadcell_3 : ");
  Serial.print(LoadCell_3.getData());
  Serial.print(" | Loadcell_4 : ");
  Serial.println(LoadCell_4.getData());
  // -----------------------
  // Serial.print(" | Loadcell_1 : ");
  // Serial.print(LoadCell_1.update());
  // Serial.print(" | Loadcell_2 : ");
  // Serial.print(LoadCell_2.update());
  // Serial.print(" | Loadcell_3 : ");
  // Serial.print(LoadCell_3.update());
  // Serial.print(" | Loadcell_4 : ");
  // Serial.println(LoadCell_4.update());
  
  dataLoadcell_1();

  //int recv_lc1 = dataLoadcell_1();int recv_lc2 = dataloadcell_2();
  //int recv_lc3 = dataloadcell_3();
  //int recv_lc4 = dataLoadcell_4();


  //Serial.println(recv_lc1,recv_lc2,recv_lc3,recv_lc4);

}
void dataLoadcell_1() {
  int interval_1 = 3000;
  int i_1 = 0;
  boolean newDataReady =  LoadCell_1.update();
  long t_1 = 0;

  if (newDataReady)
  { if (millis() > t_1 + interval_1);
    { i_1 = LoadCell_1.getData();
      if (i_1 < 0)i_1 = 0;
      Serial.print("i_1 : ");
      Serial.print(i_1);
      newDataReady = 0;
      t_1 = millis();
    }
  }
  if (Serial.available() > 0)
  { char inByte = Serial.read();
    if (inByte == 't')LoadCell_1.tareNoDelay();
  }
  if (LoadCell_1.getTareStatus() == true)Serial.println("TARE SELESAI");


}
void dataLoadcell_2() {
  int interval_2 = 3000;
  int i_2 = 0;
  boolean newDataReady =  LoadCell_2.update();
  long t_2 = 0;

  if (newDataReady)
  { if (millis() > t_2 + interval_2);
    { i_2 = LoadCell_2.getData();
      if (i_2 < 0)i_2 = 0;
      Serial.println(i_2);
      newDataReady = 0;
      t_2 = millis();
    }
  }
  if (Serial.available() > 0)
  { char inByte = Serial.read();
    if (inByte == 't')LoadCell_2.tareNoDelay();
  }
  if (LoadCell_2.getTareStatus() == true)Serial.println("TARE SELESAI");

}
void dataLoadcell_3() {
  int interval_3 = 3000;
  int i_3 = 0;
  boolean newDataReady =  LoadCell_3.update();
  long t_3 = 0;

  if (newDataReady)
  { if (millis() > t_3 + interval_3);
    { i_3 = LoadCell_3.getData();
      if (i_3 < 0)i_3 = 0;
      //Serial.println(i);
      newDataReady = 0;
      t_3 = millis();
    }
  }
  if (Serial.available() > 0)
  { char inByte = Serial.read();
    if (inByte == 't')LoadCell_3.tareNoDelay();
  }
  if (LoadCell_3.getTareStatus() == true)Serial.println("TARE SELESAI");

}
void dataLoadcell_4() {
  int interval_4 = 3000;
  int i_4 = 0;
  boolean newDataReady =  LoadCell_4.update();
  long t_4 = 0;

  if (newDataReady)
  { if (millis() > t_4 + interval_4);
    { i_4 = LoadCell_4.getData();
      if (i_4 < 0)i_4 = 0;
      //Serial.println(i);
      newDataReady = 0;
      t_4 = millis();
    }
  }
  if (Serial.available() > 0)
  { char inByte = Serial.read();
    if (inByte == 't')LoadCell_4.tareNoDelay();
  }
  if (LoadCell_4.getTareStatus() == true);
  //Serial.println("TARE SELESAI");

}