#define tryb_CWU 3
#define tryb_CO 4
#define zawor_3D 5


const float BETA = 3950; // should match the Beta Coefficient of the thermistor
bool heating = false;

int span, index, value, lastvalue;
String command;
float end_temp = 46.00f;
float start_temp = 42.00f;
bool sleep = false;
int sleep_time = 5, counter = 0;


void setup() {
  pinMode(zawor_3D, OUTPUT);
  pinMode(tryb_CO, OUTPUT);
  pinMode(tryb_CWU, OUTPUT);
  Serial.begin(9600);
  Serial.println("Haier 8kW");
}

void loop() {


  int analogValue = analogRead(A0);
  float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
  delay(1000);
  span = 0;
  index = 0;
  value = 0;
  lastvalue = 0;


  command = Serial.readStringUntil('\n');

  if ( 1 == sscanf ( &command[index], "%d%n", &value, &span)) {
      index += span;
      lastvalue = value;
  }
  while ( 1 == sscanf ( &command[index], "%*[^0-9]%d%n", &value, &span)) {
      index += span;
      lastvalue = value;
  }

  if(lastvalue != 0){
    for(int i=index-1; i>=0; i--)
    {
    if(command[i] == ' '){
      command.remove(i);
      break;
    }
    }
  }


  if(celsius >= end_temp && heating == true && sleep == false){
    digitalWrite(zawor_3D, LOW);
    sleep = true;
    heating = false; //koniec grzania
  }
  else if(celsius <= start_temp || heating == true){
    digitalWrite(tryb_CWU, HIGH);
    digitalWrite(zawor_3D, HIGH);
    digitalWrite(tryb_CO, LOW);
    heating = true; //start grzania
  }
  else if(heating == false && sleep == false){
    digitalWrite(tryb_CO, HIGH);
    digitalWrite(tryb_CWU, LOW);
    digitalWrite(zawor_3D, LOW);
  }

  if(command == "hotwater start" || command == "heating stop"){
    if(celsius < end_temp && heating == false){
      Serial.println("CWU start, CO stop");
      heating = true;
    }
    else{
      Serial.println("CWU działa lub temperatura jest za wysoka!");
    }
  }

  if(command == "hotwater start temp" && lastvalue == 0){
    Serial.print(start_temp);
    Serial.println(" C°");
  }

  if(command == "hotwater start temp" && lastvalue != 0){
    if(float(lastvalue) < end_temp){
      start_temp = float(lastvalue);
      Serial.println("Zmieniono temperature startu CWU");
    }
    else
    {
      Serial.println("Temperatura startu CWU musi być mniejsza niż temp. zatrzymania CWU ");
    }
  }

  if(command == "hotwater stop" || command == "heating start"){
    if(heating == true && celsius > start_temp && sleep == false){
      Serial.println("CWU stop, CO start");
      digitalWrite(zawor_3D, LOW);
      heating = false; //koniec grzania
      sleep = true; // czekam
    }
    else{
      Serial.println("Temperatura wody jest za niska!");
    }
  }
  if(command == "hotwater stop temp" && lastvalue == 0){
    Serial.print(end_temp);
    Serial.println(" C°");
  }
  if(command == "hotwater stop temp" && lastvalue != 0){
    if(float(lastvalue) > start_temp){
      end_temp = float(lastvalue);
      Serial.println("Zmieniono temperature startu CWU");
    }
    else
    {
      Serial.println("Temperatura zatrzymania CWU musi być wyższa niż temp. startu CWU");
    }
  }
  if(command == "hotwater temp"){
    Serial.print(celsius);
    Serial.println(" C°");
  }



  if(sleep == true){
    counter += 1;
    if(counter >= sleep_time){
      sleep = false;
      counter = 0;
      digitalWrite(tryb_CWU, LOW);
      digitalWrite(tryb_CO, HIGH);
    }
  }


}
Zawór 3D
CWU
CO