Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
5.1 kB
3
Indexable
Never
int CWUTemp = 0;
String sRead = "";
int hotwaterStartTemp;
int hotwaterStopTemp;
String next = "";
int heatingActive = 0;
int wait = 0;

unsigned long aktualnyCzas = 0;
unsigned long zapamietanyCzas = 0;
unsigned long roznicaCzasu = 0;

int resolve(String input){
  	if(input == "hotwater start") return 1;
  	if(input == "hotwater start temp") return 2;
    if(input == "hotwater stop") return 3;
  	if(input == "hotwater stop temp") return 4;
    if(input == "hotwater temp") return 5;
    if(input == "heating start") return 6;
    if(input == "heating stop") return 7;
  
  	if(input.indexOf("hotwater start temp") != -1 && input.length() > 20) return 8;
  	if(input.indexOf("hotwater stop temp") != -1 && input.length() > 20) return 9;
  }

void setup()
{
  hotwaterStartTemp = 36;
  hotwaterStopTemp = 46;
  Serial.begin(9600);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
}

void loop()
{
  zapamietanyCzas = millis();
  CWUTemp = map(((analogRead(A0) - 20) * 3.04), 0, 1023, -40, 125);
  
  if(wait == 1){
    Serial.println("Rozpoczecie oczekiwania");
    
    while(roznicaCzasu < 10000){
      aktualnyCzas = millis();
  	  roznicaCzasu = aktualnyCzas - zapamietanyCzas;
      sRead = Serial.readString();
      if(sRead.length() > 0)
      next = sRead;
    }
    
    Serial.println("Zakonczenie oczekiwania");

    wait = 0;
  }

  if(next.length() > 0){
    sRead = next;
    next = "";
  }else{
  	sRead = Serial.readString();
  }
  
  if(sRead.length() > 0 || heatingActive == 1){  
    switch(resolve(sRead)){
      case 1: {
        heatingActive = 1;
       	Serial.println("hotwater is starting");
        if(CWUTemp < hotwaterStopTemp){
          digitalWrite(2, LOW);
          digitalWrite(3, LOW);
          digitalWrite(4, HIGH);

          while(CWUTemp < hotwaterStopTemp){
            CWUTemp = map(((analogRead(A0) - 20) * 3.04), 0, 1023, -40, 125);
            
            aktualnyCzas = millis();
  			roznicaCzasu = aktualnyCzas - zapamietanyCzas;
  
  			if (roznicaCzasu >= 1000UL) {
		    	zapamietanyCzas = aktualnyCzas;
		    	Serial.println(CWUTemp);
  			}
            
            sRead = Serial.readString();
			if(sRead.length()>0){
              	next = sRead;
            	break;
            }
          }
          
          if(resolve(next) == 3 || resolve(next) == 6 || resolve(next) == 7){
            	heatingActive = 0;
            	wait = 1;
          		digitalWrite(4, LOW);
          		digitalWrite(2, HIGH);
            	Serial.println("hotwater is stopping");
    	  }
          else{
          		break;	 		 
          }
        }  
        break;
      }
      case 2: {
       	Serial.print("Starting temperature for hotwater is: ");
        Serial.println(hotwaterStartTemp);
        
        if(heatingActive == 1){
          heatingActive = 0;
          next = "hotwater start";
        }
        break;
      }
      case 3: {
       	Serial.println("hotwaterStop");
        digitalWrite(4, LOW);
        digitalWrite(2, HIGH);
        wait = 1;
        break;
      }
      case 4: {
       	Serial.print("Stoping temperature for hotwater is: ");
        Serial.println(hotwaterStopTemp);
        
        if(heatingActive == 1){
          heatingActive = 0;
          next = "hotwater start";
        }
        break;
      }
      case 5: {
       	Serial.println("current water temperature: ");
        CWUTemp = map(((analogRead(A0) - 20) * 3.04), 0, 1023, -40, 125);
        Serial.print(CWUTemp);
        
        if(heatingActive == 1){
          heatingActive = 0;
          next = "hotwater start";
        }
        break;
      }
      case 6: {
       	Serial.println("heating is starting");
        digitalWrite(4, LOW);
        digitalWrite(2, HIGH);
 	    digitalWrite(3, HIGH);
        break;
      }
      case 7: {
       	Serial.println("heating is stoping");
        digitalWrite(4, LOW);
        digitalWrite(2, LOW);
        digitalWrite(3, LOW);
        wait = 1;
        break;
      }
      case 8: { //hotwater start temp int
        int temp = sRead.substring(20).toInt();
       	Serial.println(temp);
        if(temp < hotwaterStopTemp){
        	hotwaterStartTemp = temp;
        }else{
          	Serial.println("temperatura start musi być niższa od stop");
        }
        
        if(heatingActive == 1){
          heatingActive = 0;
          next = "hotwater start";
        }
        break;
      }
      case 9: { // hotwater stop temp int
       	int temp = sRead.substring(20).toInt();
       	Serial.println(temp);
        if(temp > hotwaterStartTemp){
        	hotwaterStopTemp = temp;
        }else{
          	Serial.println("temperatura stop musi być wyższa od start");
        }
        
        if(heatingActive == 1){
          heatingActive = 0;
          next = "hotwater start";
        }
        break;
      }
  }
   
  }
  sRead = "";
}