My BLYNK Timing is not working

Hello there, I have a project I have been working on and I finally have the hardware setup. However, I am having problems with tracking timing. I get up to case 3 fine, and then when I am supposed to wait in that state until ElapsedMashTime >= MASHTIME it just clicks right through that state into the next one. It seems like the way I am timing is not working. Any ideas?

//Auto Brewing State decision upfront
if (tanktemp < MASHTEMP && autostart == true && switchState ==0) {
  switchState=1;
}

else if (tanktemp >= MASHTEMP - 8.0 && tanktemp < MASHTEMP - 2.0 && premash == true && mashPID == false && switchState ==0) { 
  switchState=2;
}

else if (tanktemp >= MASHTEMP - 2.0 && tanktemp <= MASHTEMP + 2.0 && premash == true && switchState ==0) { 
  switchState=3;
}

else if (tanktemp <= MASHTEMP - 8.0 && mashrun == true && mashreheat == false && switchState ==0){ 
  switchState=4;
}

else if (tanktemp >= MASHTEMP && mashreheat == true && switchState ==0){
  switchState=5;
}

else if (ElapsedMashTime >= MASHTIME && mashrun == true && switchState ==0) { 
  switchState=6;
}

else if (tanktemp >= 200 && mashend == true && switchState ==0) { 
  switchState=7;
}

else if (reboil == true && switchState ==0){ 
  switchState=8;
}

else if (boilrun == true && switchState ==0) {
  switchState=9;
}

else if (ElapsedBoilTime >= BOILTIME && boilrun == true && switchState ==0) { 
  switchState=10;
}

else if (tanktemp <= 190 && boilend == true && switchState ==0) {
  switchState=11;
}

else if (rechill == true && switchState ==0){ 
  switchState=12;
}

else if (ElapsedChillTime >= CHILLTIME && chillrun == true && switchState ==0) { 
  switchState=13;
}

else if (reheatpremash == true && switchState ==0) { 
  switchState=14;
}


//When Auto Brewing Start button pressed 
if (val_start == HIGH && val_pause == LOW && val_pump == LOW && val_heater1 == LOW && val_heater2 == LOW){
ElapsedPreMashTime = (millis()/60000) - startingtime;
ElapsedMashTime = (millis()/60000) - MashStartTime - totalpausem;
ElapsedBoilTime = (millis()/60000) - BoilStartTime - totalpauseb;
ElapsedChillTime = (millis()/60000) - ChillStartTime - totalpausec;


switch (switchState){
//premash = Heater 1 & 2 on, pump off
case 1: 
      switchState = 0;
      startingtime = (millis()/60000);
      terminal.println(startingtime);
      premash=true;
      autostart = false;
      tone(BUZZER,500,5000);
      terminal.println("Heat starting, need to cover with water!!");
      terminal.flush();
      digitalWrite(HEATER1, HIGH);
      digitalWrite(HEATER2, HIGH);
      terminal.println("Heaters On");
      terminal.flush();    
break;


//reheatpremash: turn heaters back on after a pause during premash stage
case 14:
    switchState = 0;
    reheatpremash = false;
    digitalWrite(HEATER1, HIGH);
    digitalWrite(HEATER2, HIGH);
    terminal.println("Back from pause, premash heaters back on");
    terminal.flush(); 
break;


//close to mash temp = only one heater stay on (MY VERSION OF A PID)
case 2: 
    Blynk.notify("Close to MashTemp, ready to insert grain");
    switchState = 0;
    mashPID = true;
    digitalWrite(HEATER2, HIGH);
    tone(BUZZER,500,2000);
    terminal.println("Temp close to Mash, one heater on");
    terminal.flush();
break;

//mashrun = heaters off, pump on
case 3:
    switchState = 0;
    mashrun = true;
    premash=false;
    MashStartTime = (millis()/60000);   
    digitalWrite(HEATER1, LOW);
    digitalWrite(HEATER2, LOW);
    digitalWrite(PUMP, HIGH);
    tone(BUZZER,500,2000);
    terminal.println("Mash temp hit, mash begin");
    terminal.flush();
break;

//Mash Reheat Adds function so that if paused or a lot of heat loss the heater keeps us at mash temp
case 4:
  switchState = 0;
  mashreheat = true;
  digitalWrite(HEATER1, HIGH);
  digitalWrite(HEATER2, HIGH);
  terminal.println("Heat loss, heaters back on");
  terminal.flush();
break;

//Mash Reheat Complete
case 5: 
  switchState = 0;
  mashreheat = false;
  digitalWrite(HEATER1, LOW);
  digitalWrite(HEATER2, LOW);
  terminal.println("Mash back up to temp, heaters back off");
  terminal.flush();
break;

//mashend = heaters both on, pump off
case 6:
    switchState = 0;
    mashend = true;
    mashrun = false;
    digitalWrite(HEATER1, HIGH);
    digitalWrite(HEATER2, HIGH);
    digitalWrite(PUMP, LOW);
    tone(BUZZER,500,2000);
    terminal.println("Mash Complete, heat on, pump off, pre boil stage");
    Blynk.notify("REMOVE GRAIN BAG: Mash Complete, heat on, pump off, pre boil stage");
    terminal.flush(); 
break;
1 Like

I think it was an issue of bringing up the calculation higher in the code. However if you have other ideas let e know. Thanks.

my IF statements all look like this:

((tanktemp <= MASHTEMP - 8.0) && (mashrun == true) && (mashreheat == false) && (switchState ==0))

not this:

(tanktemp <= MASHTEMP - 8.0 && mashrun == true && mashreheat == false && switchState ==0)

dunno if that helps you though!

1 Like

t’s probably not helping, but it’s sure making your code more readable :slight_smile: Which in itself can help very well in debugging.