# include <LiquidCrystal_I2C.h>
# include <OneButton.h>
//Define GPIO-Ports:
int PhotoResistor = 2;
int StartLed = 27;
int ResetLed = 25;
int CalLed = 26;
int StartTasterNr = 15; // Start
int ResetTasterNr = 5; // Reset
int CalTasterNr = 4; // Callibration
// Taster initialisieren und einen Namen geben:
OneButton StartTaster(StartTasterNr, true);
OneButton ResetTaster(ResetTasterNr, true);
OneButton CalTaster(CalTasterNr, true);
// Initialisiierung des LCD des LCDs
LiquidCrystal_I2C lcd(0x27, 20, 2);
// Variablen der Zeit
float Sekunden;
String ZeitFormatiert;
String TimerTag = "Run";

String menuetag = "start";
 // reset(Neustart), calibration(Aktueller messwert->hell/dunkel), timer(laufende zeit), start(Anweisung)

void setup()
{
  Serial.begin(115200);
  pinMode(PhotoResistor, INPUT);
  startfunktion();
  pinMode(CalLed, OUTPUT);
  pinMode(ResetLed, OUTPUT);
  pinMode(StartLed, OUTPUT);
  // Aktionen dem Modus des Tasters zuordnen
  StartTaster.attachClick(TimerFunction);
  StartTaster.setPressTicks(5000);
  StartTaster.setClickTicks(500);
  StartTaster.setDebounceTicks(50);

  ResetTaster.attachClick(ResetFunction);
  ResetTaster.setPressTicks(5000);
  ResetTaster.setClickTicks(500);
  ResetTaster.setDebounceTicks(50);

  CalTaster.attachClick(Calibration);
  CalTaster.setPressTicks(1000);
  CalTaster.setClickTicks(500);
  CalTaster.setDebounceTicks(50);
}

/* LOW = 0 Licht an
  HIGH = 1 Licht aus*/

void startfunktion()
{
  if (menuetag == "start")
  TimerTag = "Run";
  {
    lcd.init();
    lcd.backlight();
    lcd.setCursor(0, 0);
    lcd.print("Calibration");
    lcd.setCursor(0, 1);
    lcd.print("Press BlueBtn");
  }
} 
void Calibration()
{
  menuetag = "calibration";
  TimerTag = "Run";
  digitalWrite(CalLed, HIGH);
  Serial.print("Wir sind in der Kallibrierung");
  Serial.println(digitalRead(PhotoResistor));
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print(":D");
  while (menuetag == "calibration")
  {
    if (digitalRead(PhotoResistor) == LOW)
    {
      lcd.setCursor(0, 1);
      lcd.print("Light on c:");
      delay(5000);
      digitalWrite(CalLed, LOW);
      menuetag = "start";
      startfunktion();
    }
    else 
    {
      lcd.setCursor(0, 1);
      lcd.print("Light off >.<");
    }
    delay(800);
  }
}

void ResetFunction()
{
  menuetag = "reset";
  TimerTag = "Run";
  Serial.print("Wir sind im Reset");
  if (menuetag == "reset")
  {
    lcd.init();
    lcd.backlight();
    lcd.setCursor(0, 0);
    lcd.print("Device Reset");
    lcd.setCursor(0, 1);
    lcd.print("(x.x)");
    delay(5000);
    menuetag = "start";
  }
}

void TimerFunction()
{
  menuetag = "timer";
  Serial.println("Bin in der TimerFunction");
  Serial.println(digitalRead(PhotoResistor));
  if (menuetag == "timer")
  {
    while (TimerTag == "Run")
    {
      digitalWrite(StartLed, HIGH);
      ResetTaster.tick();
      Serial.println("Licht an, Timer laeuft");
      lcd.init();
      lcd.backlight();
      lcd.setCursor(0, 0);
      lcd.print("Akkutest läuft");
      int Minuten = Sekunden / 60;
      int Stunden = Minuten / 60;
      lcd.setCursor(0, 1);
      lcd.print((String) Stunden + ":" + Minuten + ":" + Sekunden + " h:mm:ss");
      Sekunden += 1;
      delay(999); //0,999s wegen delays?
      if (digitalRead(PhotoResistor) == LOW)
      {
        TimerTag = "Run";
      }
      if (digitalRead(PhotoResistor) == HIGH)
      {
        TimerTag = "Stop";
      }
    }
    if (TimerTag == "Stop")
    {
      digitalWrite(StartLed, LOW);
      Serial.println("Licht aus, Timer Steht");
      lcd.init();
      lcd.backlight();
      lcd.setCursor(0, 0);
      lcd.print("Akkulaufzeit:");
      lcd.setCursor(0, 1);
      lcd.print(Sekunden);
      while (TimerTag == "Stop")
      {
        digitalWrite(ResetLed, LOW);
        digitalWrite(StartLed, LOW);
        digitalWrite(CalLed, LOW);
        delay(250);
        digitalWrite(ResetLed, HIGH);
        digitalWrite(StartLed, LOW);
        digitalWrite(CalLed, LOW);
        delay(250);
        digitalWrite(ResetLed, HIGH);
        digitalWrite(StartLed, HIGH);
        digitalWrite(CalLed, LOW);
        delay(250);
        digitalWrite(ResetLed, HIGH);
        digitalWrite(StartLed, HIGH);
        digitalWrite(CalLed, HIGH);
        delay(250);
        digitalWrite(ResetLed, LOW);
        digitalWrite(StartLed, HIGH);
        digitalWrite(CalLed, HIGH);
        delay(250);
        digitalWrite(ResetLed, LOW);
        digitalWrite(StartLed, LOW);
        digitalWrite(CalLed, HIGH);
        delay(250);
      }

    }  
  }

}

void loop()
{
  //Serial.print("Bin im LOOP");
  // Taster alle 10 Millisekunden abfragen
  StartTaster.tick();
  ResetTaster.tick();
  CalTaster.tick();
  //einKlick();
  delay(10);
}