int autoLed = 2, woodBurnerLed = 3, gasBurnerLed = 4;
int autoButton = 5, woodburnerButton = 6, gasBurnerButton = 7;
int woodBurnerRelay = 8, gasBurnerRelay = 9;
int blinker = 0;
bool owerride = false;
int manual = 0;
int connectionActivity = 0;
int timer = 0;
int turnedOn = 0;
bool heating = false;
int burnTemp = 0, pufferTemp = 0;
int ThermistorPin = 5;
int ThermistorPin2 = 3;
int Vo;
float R1 = 10000;
float logR2, R2, T, Tc, Tf;
float c1 = 1.1384e-03, c2 = 2.3245e-04, c3 = 9.489e-08;


void setup() {
  // put your setup code here, to run once:
  pinMode(autoLed, OUTPUT);
  pinMode(woodBurnerLed, OUTPUT);
  pinMode(gasBurnerLed, OUTPUT);

  pinMode(autoButton, INPUT_PULLUP);
  pinMode(woodburnerButton, INPUT_PULLUP);
  pinMode(gasBurnerButton, INPUT_PULLUP);

  pinMode(woodBurnerRelay, OUTPUT);
  digitalWrite(woodBurnerRelay, HIGH);
  delay(1);
  pinMode(gasBurnerRelay, OUTPUT);
  digitalWrite(gasBurnerRelay, HIGH);
  
  Serial.begin(9600);
}
void burnersonoff(){
    if(heating){
      if(turnedOn == 0){
        digitalWrite(woodBurnerLed, LOW);
        digitalWrite(gasBurnerLed, LOW);
        digitalWrite(woodBurnerRelay, HIGH);
        digitalWrite(gasBurnerRelay, HIGH);
      }else if(turnedOn == 1){
        digitalWrite(woodBurnerLed, HIGH);
        digitalWrite(woodBurnerRelay, LOW);
        digitalWrite(gasBurnerLed, LOW);
        digitalWrite(gasBurnerRelay, HIGH);
      }else if(turnedOn == 2){
        digitalWrite(gasBurnerLed, HIGH);
        digitalWrite(gasBurnerRelay, LOW);
        digitalWrite(woodBurnerLed, LOW);
        digitalWrite(woodBurnerRelay, HIGH);
      }
    }
}
void loop() {
  if (Serial.available()) {
    String message = Serial.readString();
    if(message[0] == 'A'){
      connectionActivity = 0;
      if(owerride == false){
        if(message[1] == '0'){
          turnedOn = 0;
        }else if(message[1] == '1'){
          turnedOn = 1;
        }else if(message[1] == '2'){
          turnedOn = 2;
        }
        if(message[2] == '0'){
          manual = 0;
        }else if (message[2] == '1'){
          manual = 1;
        }else if(message[2] == '2'){
          manual = 2;
        }
        if(message[3] == '1'){
          heating = true;
        }else heating = false;
      }else owerride = false;

      String Message = "B";
      Message += manual;
      if(heating) Message += "1";
      else Message += "0";

      String a = "";
      for(int i = 0; i < 4-String(burnTemp).length(); i++){
        a += "0";
      }
      a += String(burnTemp);
      Message += a;
      a = "";
      for(int i = 0; i < 4-String(pufferTemp).length(); i++){
        a += "0";
      }
      a += String(pufferTemp);
      Message += a;
      Serial.println(Message);

    }else{
      if(connectionActivity < 200) connectionActivity++;
    }
    burnersonoff();
  }else{
    if(connectionActivity < 200) connectionActivity++;
  }
  if(connectionActivity > 30){
      if(blinker >= 1 && blinker < 2){analogWrite(autoLed, 255);}
      if(blinker >= 1 && blinker > 2){analogWrite(autoLed, 0); blinker = 0;};
      blinker++;
      turnedOn = 0;
      burnersonoff();
  }
  for(int i = 0; i < 500; i++){
      if(connectionActivity < 30){
         if(digitalRead(autoButton) == LOW) { while(digitalRead(autoButton) == LOW);  if(manual > 0) manual = 0; else heating = !heating; turnedOn = 0; burnersonoff(); owerride = true;}
         if(digitalRead(woodburnerButton) == LOW) { while(digitalRead(woodburnerButton) == LOW); if(manual == 1 && turnedOn == 1){manual = 0; turnedOn = 0;}else { manual = 1; turnedOn = 1;} heating = true; burnersonoff(); owerride = true;}
         if(digitalRead(gasBurnerButton) == LOW) { while(digitalRead(gasBurnerButton) == LOW); if(manual == 2 && turnedOn == 2){manual = 0; turnedOn = 0;}else { manual = 2; turnedOn = 2;}heating = true; burnersonoff(); owerride = true;}
         if(manual == 0 && heating){ analogWrite(autoLed, 255); }else analogWrite(autoLed, 0);
      }
    delay(1);
  }
  
  Vo = analogRead(ThermistorPin);
  if(Vo <= 0) Vo = 0;
  R2 = R1 * (1023.0 / (float)Vo - 1.0);
  logR2 = log(R2);
  T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
  Tc = T - 273.15;
  burnTemp = int(Tc*100);

  Vo = analogRead(ThermistorPin2);
  if(Vo <= 0) Vo = 0; 
  R2 = R1 * (1023.0 / (float)Vo - 1.0);
  logR2 = log(R2);
  T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
  Tc = T - 273.15;
  pufferTemp = int(Tc*100);

}
