#include <uTimerLib.h>
#include <LiquidCrystal.h>
#include <MillisTimer.h>
#include <TimeLib.h>

//int P15 = 15;
 
MillisTimer T_DoseTime;
MillisTimer T_PumpDelay;
MillisTimer T_Pump;

typedef struct {
  int ZH; // hour
  int ZM; // Minute
  int32_t ZLH; // lenght in milli sec
  int32_t ZD; // dose time in sec's
} RunData;
RunData RD[6];


unsigned long LastTim = 0;
unsigned long HoldTim = 0;
unsigned long AuxTimer = 0;
boolean ok;
boolean bp;
int TPumpDelay = 1000;



LiquidCrystal lcd(13, 12, 14, 27, 26, 25);

void setup() {

  Serial.begin(115200);
  while (!(Serial)) {
    Serial.println("waiting");
  }
  setTime(8, 0, 0, 23, 1, 2022);
  T_Pump.setInterval(10);
  T_DoseTime.setInterval(10);
  T_PumpDelay.setInterval(10);
  T_Pump.setRepeats(1);
  T_DoseTime.setRepeats(1);
  T_PumpDelay.setRepeats(1);
  T_DoseTime.reset();
  T_PumpDelay.reset();
  T_Pump.reset();
  T_Pump.stop();
  T_DoseTime.stop();
  T_PumpDelay.stop();


  pinMode(15, INPUT);
  pinMode(33, OUTPUT);
  pinMode(18, OUTPUT);
  pinMode(19, OUTPUT);

  lcd.begin(20, 4);
  //// you can now interact with the LCD, e.g.:
  //  lcd.print("Hello World!");

  RD[0] = {8, 0, 6000, 4000};
  RD[1] = {8, 0, 28800000, 720000};
  RD[2] = {8, 0, 28800000, 720000};
  RD[3] = {8, 0, 28800000, 720000};
  RD[4] = {8, 0, 28800000, 720000};
  RD[5] = {8, 0, 28800000, 720000};
  RD[6] = {8, 0, 28800000, 720000};



  // timer1.expiredHandler(myTimerFunction);

}


void loop() {
  // put your main code here, to run repeatedly:
  bp = digitalRead(15);

  if (bp == true) {
    T_Pump.setInterval(RD[0].ZLH);
    T_DoseTime.setInterval(RD[0].ZD);
    T_PumpDelay.setInterval(TPumpDelay);
    T_DoseTime.start();
    T_Pump.start();
    T_PumpDelay.start();
    digitalWrite(33, HIGH);
  }
  else {
    digitalWrite(33, LOW);
  }

  if (( millis() ) < 2000 ) {

    T_Pump.stop();
  }

  pumpcontrol();

  /* Pump control  ==========================================
    if (T_DoseTime.isRunning())
    {digitalWrite(18, HIGH);}
    else
    {digitalWrite(18, LOW);}

    if (T_Pump.isRunning() && !T_PumpDelay.isRunning())
    {digitalWrite(19, HIGH);}
    else
    {digitalWrite(19, LOW);}
  */ //Pump control  ==========================================

  if (( millis() - AuxTimer ) > 2000 ) {

    Serial.print("LastTim");
    Serial.println( millis());
    AuxTimer = millis();
  }
  if (( millis() - LastTim ) > 200 ) {
    lcd.clear();
    //  lcd.home();


    T_DoseTime.run();
    T_PumpDelay.run();
    T_Pump.run();

    lcd.setCursor(0, 0);
    lcd.print("Time = ");
    lcd.print(millis());

    if (T_DoseTime.isRunning()) {
      lcd.setCursor(0, 1);
      lcd.print("Dose Time = ");
      lcd.print(T_DoseTime.getRemainingTime());
    }



    if (T_PumpDelay.isRunning()) {
      lcd.setCursor(0, 2);
      lcd.print("T_PumpDela = ");
      lcd.print(T_PumpDelay.getRemainingTime());
    }
    LastTim = millis();
  }

}

void pumpcontrol() {
  // Pump control  ==========================================
  if (T_DoseTime.isRunning())
  {
    digitalWrite(18, HIGH);
  }
  else
  {
    digitalWrite(18, LOW);
  }

  if (T_Pump.isRunning() && !T_PumpDelay.isRunning())
  {
    digitalWrite(19, HIGH);
  }
  else
  {
    digitalWrite(19, LOW);
  }

  if (T_Pump.isRunning()) {
    lcd.setCursor(0, 3);
    lcd.print("T_Pump = ");
    lcd.print(T_Pump.getRemainingTime());
  }
  // Pump control  ==========================================
}