Using If statement in BLYNK_READ

I have the following code:

BLYNK_READ(V10) { if (premash == true){ Blynk.virtualWrite(V10, "preM");} if (mashrun == true){ Blynk.virtualWrite(V10, "MASH");} if (mashend == true){ Blynk.virtualWrite(V10, "preB");} if (boilrun == true){ Blynk.virtualWrite(V10, "BOIL");} if (chillrun == true){ Blynk.virtualWrite(V10, "CHILL");} else{ Blynk.virtualWrite(V10, "N/A");} }

However my V10 is always displaying Chill, but I can see it flip through every condition and then it rests on chill until called again, so it seems like the “if” is not working and every condition is right which isnt the case.

Why is this if statement not working properly based on the global variables?

@philmurp maybe because chillrun is true so you always see latest value? So it is always overrides all others?

you probably meant:

if(...) {

else if(...) {

else if(...) {

else {
}

You need an else before every if (except the first one).

As you can see, nested if statements like this can be error-prone. You might also want to consider using a switch statement instead.

1 Like

Yeah I would prefer the switch case for sure, but I am not sure how I would figure out that since i have more than one variable to switch.

I could convert all blynk inputs into states but that would require an if statement and then that kind of defeats the purpose.

Do you have any thoughts on how a switch case can work with multiple variables?

A switch statement can only work with one variable, it’s a simple as that :wink: So you have to either change your code to use a single variable, or you’re stuck with nested if statements.

The crux of the question is this: Are the states mutually exclusive? If they are then you can (and should) change your code to model them with a single variable.

IF the states are not exclusive (i.e. you can be in multiple states at once), AND you only want to display one at a time on V10, then you need to write some rules to say which state has priority - which is what you’re doing here.

I want them to be mutually exclusive, but my concern is I have many different combinations of buttons to be pressed.

ie. Start Button, Pause Button, Pump Button : So
state 1 = Start on, pause off, pump off
state 2 = Start off, pause on, pump off
state 3 = start off, pause off, pump on
State 4= start on , pause on, pump off
etc…

I am trying to think how I would create this sort of “state” variable which can then be the main variable in a switch/case

What are your thoughts on nested Switch/Cases?

Don’t do that :smiley:

It’ll get messy really fast. I think you need to “define” all your states so you can assign them a sequential order of numbers. That would be my idea. I think your idea of the state should work fine.

You can always split things up in several functions, that makes things much clearer too. E.g. have a function for every action, like start(int state); pause(int state) etc. and combine those in several states. E.g. 101, 111 etc. and base your case on that.

Thats what I was attempting to do way back when I started but I am not sure how to define those states based on Blynk button inputs. Let me read up on it and see if I can figure it out again.

For example, how do I say State = 1 when start button is high and pause is low and pump is low without using an if statement?

In our project we have 28 different ‘states’ and we use a switch statement to work out what to do for each of the 28 states.

That would be so much better for me, how did you set those states based on Blynk button inputs?

We used a 28 item Menu widget.

Sometimes it helps to draw a State Transition Diagram. Your outputs are states (nodes in the digram) and your inputs are transitions (edges in the diagram). So for example if you’re ‘pumping’ and the pump is switched to off, you need to transition to the ‘not-pumping’ state.

Once you’ve got a clear diagram of all your possible states, it’s much easier to write your code as a bunch of state transitions.

1 Like

Interesting, so If I manually pressed menu option 1, then my code could take it from there and as conditions are met it would switch the state further down the menu automatically.

ie. I press menu option 1 to start things off. Then once a condition happens my code would say write to the menu widget saying index value is #2 now and so on?

And so your menu basically replaced the buttons functionality?

The Menu widget was introduced to make variable setting easier.

We still use buttons for some things.

Our sketch doesn’t update the Menu it is simply the Menu updating the variables in the sketch. I guess there is no reason why you couldn’t write to the Menu from the sketch.

case 5 from our Menu looks like this:

case 5: {    // ON between Low Temp C and High Temp C
  Conditions = 5;
  ConditionParameter = minimumtempC;
  ConditionParameter1 = maximumtempC;
  oneccparameter = false;
  Blynk.virtualWrite(V36, ConditionParameter);
  Blynk.virtualWrite(V37, ConditionParameter1);     
  break;
}

So I can use this menu option to replace the buttons and it seems to work, but once I get to my automated code I dont know how to make that into states. My states rely on temperature and time, so if a temperature is hit for a long enough time period then a state is triggered. I dont know how I can convert something that tracks two variables into a state without if/else statement

or nested swithc/cases

I have been messing around with this for a and I am at a question I just dont understand why something is happening. So when I hit any button besides the start button (associated with val_start, V4), it works as expected. However when I hit the Start Button, I have printed values and I am getting that it thinks switchState is = 9 when is should be =1 as I havent hooked up any heaters etc. Any idea on how this is happening? Full code below, focus on about 3/4 of the way down that around the Auto Brewing code segment, I put if statement precedding it, and then used that to do a switch case in the Auto Brewing button code. However it keeps showing me as the stateSwitch value is 9 instead of 1 like it should be and I have no idea why it likes that 9 number. Thanks!!

////Sensor Initialization
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
// Data wire (containing the Temp Sensors) is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 3
// Setup a oneWire instance to communicate with any OneWire device
OneWire oneWire(ONE_WIRE_BUS);
// Pass the oneWire reference to Dallas Temperature. 
DallasTemperature sensor(&oneWire);

//////////////////////BLYNK CODE
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
SimpleTimer timer;
const char* ssid = "Hefty 2G";
const char* password = "J4yPhil8";
char auth[] = "6e413df8de0.....";
WidgetTerminal terminal(V0);
WidgetTerminal terminalHops(V14);

////////Setting up variables to be declared by Blynk app
int MASHTEMP; // Desired Mash Temp(deg F)
int MASHTIME; //Desired Mash Time(milli sec.)
int BOILTIME; //Desired Boil Time(milli sec.)
int CHILLTIME; //Desired time to pump all wort through the wort chiller (milli sec.)

//HOP ADDITIONS, HOPTIME and HOPINFO must be the same array length
int hopCounter; //Number of hop adds that I input, based upon my terminal input
int HOPTIME[20]; // Hop times
String HOPINFO[20];  // HOPINFO;

////////////Assign counters for independent and pause control buttons
int noButtonCounter;
int pacounter;
int pucounter;
int h1counter;
int h2counter;


////////////////////////Assign variables
float tanktemp; //Declaring temperature variables
unsigned long pausestart; //Time when pause button was pushed
unsigned long elapsedpausetimem; //Show how long during mash paused
unsigned long elapsedpausetimeb; //Show how long during boil paused
unsigned long elapsedpausetimec; //Show how long during chill paused
unsigned long totalpausem;
unsigned long totalpauseb;
unsigned long totalpausec;
unsigned long MashStartTime; //used for MASH TIME
unsigned long ElapsedMashTime = (millis()/60000) - MashStartTime - totalpausem;
unsigned long BoilStartTime; //used for BOIL TIME
unsigned long ElapsedBoilTime = (millis()/60000) - BoilStartTime - totalpauseb;
unsigned long ChillStartTime; //used for BOIL TIME
unsigned long ElapsedChillTime = (millis()/60000) - ChillStartTime - totalpausec;
unsigned long startingtime; //overall starting time


//Setup all states
int switchState;
boolean autostart = true;
boolean premash;
boolean mashrun;
boolean mashreheat;
boolean mashend;
boolean boilrun;
boolean reboil;
boolean boilend;
boolean chillrun;
boolean rechill;
boolean chillend;

//Pin Inputs/Outputs to Arduino:
const int PUMP = D4;
const int HEATER1 = D5;
const int HEATER2 = D6;

//Status variables for buttons
int val_start; 
int val_pause;
int val_pump;
int val_heater1;
int val_heater2;


void brewingstuff(){
  
//No button pressed nothing is on (Also works so that when Auto button is on and then turned off, everything shuts down
if (val_start == LOW && val_pause == LOW && val_pump == LOW && val_heater1 == LOW && val_heater2 == LOW){
      switch (noButtonCounter){
      case 0:
         analogWrite(HEATER1, 0);
         analogWrite(HEATER2, 0);
         analogWrite(PUMP, 0);
         terminal.println("No Button Pressed, to pause must press pause button!");
         terminal.flush();
         noButtonCounter = 1;
         pausestart = (millis()/60000);
      break;
      
      case 1:
      if (mashrun = true){
       elapsedpausetimem = ((millis()/60000) - pausestart); 
      }
      else if (boilrun = true){
       elapsedpausetimeb = ((millis()/60000) - pausestart); 
      }
      else if (chillrun = true){
       elapsedpausetimec = ((millis()/60000) - pausestart); 
      }
      unsigned long elapsedpausetime = ((millis()/60000) - pausestart);
      terminal.print("No button pressed for:  ");
      terminal.println(elapsedpausetime);
      terminal.flush();
      break;} 
  }

if ((val_start == HIGH || val_pause == HIGH || val_pump == HIGH || val_heater1 == HIGH || val_heater2 == HIGH) && noButtonCounter == 1){ //If anything is pressed right after no button was pressed, reset no button counter
        noButtonCounter = 0; 
        if (mashrun = true){
          totalpausem = totalpausem + elapsedpausetimem; 
        }
       else if (boilrun = true){
          reboil = true;
          totalpauseb = totalpauseb + elapsedpausetimeb; 
       }
       else if (chillrun = true){
          rechill = true;
          totalpausec = totalpausec + elapsedpausetimec; 
       }  
  }
  
//pause stops everything  
if (val_pause == HIGH){
       switch (pacounter){
       case 0:
        Blynk.virtualWrite(V4,LOW);
        Blynk.virtualWrite(V6,LOW);
        Blynk.virtualWrite(V7,LOW);
        Blynk.virtualWrite(V8,LOW);
        pausestart=(millis()/60000);
        pacounter=1;
        analogWrite(HEATER1, 0);
        analogWrite(HEATER2, 0);
        analogWrite(PUMP, 0);
        terminal.println("PAUSE WORKING");
      break;
      case 1:
      if (mashrun = true){
       elapsedpausetimem = ((millis()/60000) - pausestart); 
      }
      else if (boilrun = true){
       elapsedpausetimeb = ((millis()/60000) - pausestart); 
      }
      else if (chillrun = true){
       elapsedpausetimec = ((millis()/60000) - pausestart); 
      }
      unsigned long elapsedpausetime = ((millis()/60000) - pausestart);
      terminal.print("Manual Pause for: ");
      terminal.println(elapsedpausetime);
      terminal.flush();
      break;} 
  }
///Pause button on and then turned off it resets counter
if (pacounter == 1 && val_pause == LOW){
       pacounter = 0;
       if (mashrun = true){
          totalpausem = totalpausem + elapsedpausetimem; 
        }
       else if (boilrun = true){
          reboil = true;
          totalpauseb = totalpauseb + elapsedpausetimeb; 
       }
       else if (chillrun = true){
          rechill = true;
          totalpausec = totalpausec + elapsedpausetimec; 
       }
          
    }


//Button pressed on pump starts
if (val_start == LOW && val_pause == LOW && val_pump == HIGH){
      unsigned long btstart;
      switch (pucounter){
        case 0:
        //if (pucounter=0)
        terminal.println("Pump starting, need to open ball valve!!");
        terminal.flush();
        tone(5, 500, 10000);
        analogWrite(PUMP, 255);
        btstart=(millis()/60000);
        pucounter=1;
        break;

        case 1:
        unsigned long elapsedbuttontime = ((millis()/60000) - btstart);
        terminal.print("Manual Pump On for: ");
        terminal.println(elapsedbuttontime);
        terminal.flush(); 
        break;
      }  

  }
///Pump button on and then turned off it stops pump
if (pucounter == 1 && val_pump == LOW){
        pucounter = 0;
        analogWrite(PUMP, 0);
  }

//Heater 1 button pressed on heater starts
if (val_start == LOW && val_pause == LOW && val_heater1 == HIGH){
      unsigned long btstart1;
      switch (h1counter) {
      case 0: 
        btstart1=(millis()/60000);
        tone(5, 500, 10000);
        terminal.println("Heat 1 starting, need cover w water!!");
        terminal.flush();
        analogWrite(HEATER1, 255);
        h1counter=1;
        break;
     case 1: unsigned long elapsedbuttontime1 = ((millis()/60000) - btstart1);
      terminal.print("Manual Heater 1 On for: ");
      terminal.println(elapsedbuttontime1);
      terminal.flush();
      break; } 
  } 
///When Heater1 button on and then turned off it stops one heater
if (h1counter == 1 && val_heater1 == LOW){
        h1counter = 0;
        analogWrite(HEATER1, 0);
  }

  
if (val_start == LOW && val_pause == LOW && val_heater2 == HIGH){
      unsigned long btstart2;
      switch (h2counter){
      case 0:
        btstart2=(millis()/60000);
        tone(5, 500, 10000);
        terminal.println("Heat 2 starting, need cover w water!!");
        terminal.flush();
        analogWrite(HEATER2, 255);
        h2counter=1;
        break;
      case 1:
      unsigned long elapsedbuttontime2 = ((millis()/60000) - btstart2);
      terminal.print("Manual Heater 2 On for: ");
      terminal.println(elapsedbuttontime2);
      terminal.flush();
      break;} 
  } 
///When Heater2 button on and then turned off it stops two heater
if (h2counter == 1 && val_heater2 == LOW){
        h2counter = 0;
        analogWrite(HEATER2, 0);
  }
  
if (tanktemp < MASHTEMP && autostart == true) {
  switchState=1;
}
if (tanktemp >= MASHTEMP - 12.0 && tanktemp < MASHTEMP - 2.0 && premash == true) { 
  switchState=2;
}

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

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

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

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

if (tanktemp >= 212 && mashend == true) { 
  switchState=7;
}

if (reboil == true){ 
  switchState=8;
}

if (boilrun == true) {
  switchState=9;
}

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

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

if (rechill == true){ 
  switchState=12;
}

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


//When Auto Brewing Start button pressed 
if (val_start == HIGH && val_pause == LOW && val_pump == LOW && val_heater1 == LOW && val_heater2 == LOW){
terminal.println("Auto Start ON");
terminal.println(switchState);
}

}


/////////BLYNK CODE: DISPLAYS AND INPUTS
///////BLYNK DASHBOARD DISPLAYS
//Send tanktemp continuously
BLYNK_READ (V9)
{
  Blynk.virtualWrite(V9,tanktemp);
}
//Send overall elapsed time of entire brewday continuously
BLYNK_READ (V12)
{
  Blynk.virtualWrite(V12,(millis()/60000) - startingtime);
}

//Send Elapsed Time to BLYNK Dashboard Display
BLYNK_READ(V11)
 {
  if (mashrun = true)
    Blynk.virtualWrite(V11, ElapsedMashTime);
  if (boilrun = true)
    Blynk.virtualWrite(V11, ElapsedBoilTime);
  if (chillrun = true)
    Blynk.virtualWrite(V11, ElapsedChillTime);
  } 

//Display Phase which corresponds with the elapsed time above
BLYNK_READ(V10)
 {
  if (premash = true)
    Blynk.virtualWrite(V10, "preM");
  if (mashrun = true)
    Blynk.virtualWrite(V10, "MASH");
  if (mashend = true)
    Blynk.virtualWrite(V10, "preB");  
  if (boilrun = true)
    Blynk.virtualWrite(V10, "BOIL");
  if (chillrun = true)
    Blynk.virtualWrite(V10, "CHILL");
  } 

//////////////////////USER INPUTS: Mash Time, Mash Temp, Boil Time, Chill Time, Hop Times, Hop Info
BLYNK_WRITE(V1)
{
  MASHTEMP= param.asInt();
  terminal.println(MASHTEMP);
}

BLYNK_WRITE(V2)
{
  MASHTIME= param.asInt();
  terminal.println(MASHTEMP);
}

BLYNK_WRITE(V3)
{
  BOILTIME= param.asInt();
  terminal.println(MASHTEMP);
}
  
BLYNK_WRITE(V13)
{
  CHILLTIME= param.asInt();
  terminal.println(CHILLTIME);
  terminal.flush();
}

BLYNK_WRITE(V14)  // Terminal for HOPTIME and HOPINFO input
{
  String fromTerminal = param.asStr();
  
    
  if (fromTerminal.endsWith("?")) {
   terminalHops.println("Comma delimited, and first letter followed by '!' resets previous inputs: HOPTIMES:start w/ 'T'//HOPINFO: starts w/ 'I'");
   terminalHops.flush(); 
      }
  
  if (fromTerminal.startsWith("T") ) { //Hoptimes input
      int lastIndex;
      
      if (fromTerminal.endsWith("!")){ //In case I need to reset everything and re input hop additions
       hopCounter = 0;
       lastIndex = 0;
      }
      
      fromTerminal.remove(0,1);
      for (int i = 0; i < fromTerminal.length(); i++) {
        if (fromTerminal.substring(i, i+1) == ",") {
          HOPTIME[hopCounter] = (fromTerminal.substring(lastIndex, i)).toInt();
          lastIndex = i + 1;
          hopCounter++;
        }
        if (i == fromTerminal.length() - 1) {  // If we're at the end of the string (no more commas to stop us)
          HOPTIME[hopCounter] = (fromTerminal.substring(lastIndex, i)).toInt();
        }
      }   
      terminalHops.print(HOPTIME[0,hopCounter]);
      terminalHops.println(" from ArrayHOPTIME");
      terminalHops.flush();
      }

 
  
  if (fromTerminal.startsWith("I")){ //Hopinfo input
      int counter;
      int lastIndex;
      
      if (fromTerminal.endsWith("!")){ //In case I need to reset everything and re input hop additions
       counter = 0;
       lastIndex = 0;
      }
      
      fromTerminal.remove(0,1);
      for (int i = 0; i < fromTerminal.length(); i++) {
        if (fromTerminal.substring(i, i+1) == ",") {
          HOPINFO[counter] = fromTerminal.substring(lastIndex, i);
          lastIndex = i + 1;
          counter++;
        }
        if (i == fromTerminal.length() - 1) {  // If we're at the end of the string (no more commas to stop us)
          HOPINFO[counter] = fromTerminal.substring(lastIndex, i);
        }
      }   
      terminalHops.print(HOPINFO[0,counter]);
      terminalHops.println(" from CharArrayHOPINFO");
      terminalHops.flush();        
      }
 } 

////Manual Controls
BLYNK_WRITE(V4) //Start Button
{
  val_start= param.asInt();
}

BLYNK_WRITE(V5) //Pause Button
{
  val_pause= param.asInt();
}

BLYNK_WRITE(V6) //Pump Button
{
  val_pump= param.asInt();
}

BLYNK_WRITE(V7) //Heater 1 Button
{
  val_heater1= param.asInt();
}

BLYNK_WRITE(V8) //Heater 2 Button
{
  val_heater2= param.asInt();
}







void setup() 
{
  Serial.begin(115200);
  Blynk.begin(auth,ssid,password);
  timer.setInterval(1000, brewingstuff); //Check my brewing code every 1s
  sensor.begin();
  sensor.setResolution(9); //Resolution of 9 means 0.5degC increments.
  pinMode(PUMP, OUTPUT); //initialize pin locations as output or input
  pinMode(HEATER1, OUTPUT);
  pinMode(HEATER2, OUTPUT);
  terminal.println("Setup done, Phil's beer is on the way, Ready?");
  terminal.flush();
}

void loop() 
{
  Blynk.run();
  timer.run();
  sensor.requestTemperatures(); //Get TEMP reading continuously
  tanktemp = sensor.getTempFByIndex(0);
}

Is the IF statement at row 293?

Which part of the code is telling you it is in stateSwitch 9 as I can’t see anywhere in the code that ever sets boilrun to be true? This indicates if it is actually 9 it is being set in a piece of code you haven’t provided.

Yes it appears that the code is saying that this if statement is true

if (boilrun == true) {
switchState=9;
}

I provided all the code I have, I did add a terminal.println(switchState) and that displayed 9, so then I went into the switch case and for case 9 put another terminal print and it output that print out. So it is setting switchState to 9 and then from there is seems to be working properly to execute the switch case.

I have no idea how it is getting there because nowhere in the code does boirun = true to that point. Hence my confusion

EDIT: Found it, i had an if statment = true instead of ==true so it was setting it true; I think that explains a lot of my weird behavior because I had that mistake in several places so things were being set to true

1 Like

So you’re building a brewtroller.

Is there ever a case where you would be performing these steps at the same time? If not, you want ONE Int to control the state. i.e. Brewstate = 1 is chill, brewstate = 2 is mash etc. Trying to keep multiple state variables synced for a single process is a risky move because you can go into a unknown state if multiple variables are true. With one control Int you are always sure what mode the process is in.

Yupp thats what I am making. So I have the code in a place that I think will work, I have tested it with relays etc, now I need to actually hook it up to my brew kettle. As shown below, I used a “if/else” statement for variable stateSwitch so like you said the auto process can only be in state 1-14 and not more than that. Based on the stateSwitch variable, then I have the switch/case to run the actual actions done in each state.

I could probably have combined the two (if statement and switch/case) but separating the conditions with the actions makes is easier to identify problems in my opinion.

Also there are some states that are not directly tied to a time or temperature, and that accounts for if I hit pause or there is some interruption and the temperature falls then it is is in a state of “remash, re chill, re boil” etc

Also Also I have SwitchCase set back to 0 everytime it enters a case so that way the code runs through the actions of the state once so I can set timers etc to run.

Any thoughts on the efficiency and robustness of this code for the purposes outlined?

if (tanktemp < MASHTEMP && autostart == true && switchState ==0) {
  switchState=1;
}

else if (tanktemp >= MASHTEMP - 12.0 && tanktemp < MASHTEMP - 2.0 && premash == true && 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 >= 212 && 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: 
      Blynk.notify("Auto Starting");
      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;


//reboil: turn heaters back on after a pause during boilrun 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: 
    switchState = 0;
    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");
    Blynk.notify("Mash temp hit, mash begin");
    terminal.flush();
break;

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

case 5: //Reheat Mash
  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;  

//boilrun
case 7:
    switchState = 0;
    boilrun = true;
    mashend= false;
    BoilStartTime = (millis()/60000);
    tone(BUZZER,500,2000);
    terminal.println("Boiling start, heaters on, pump off, look for hop adds");
    terminal.flush();
break;  

//reboil: turn heaters back on after a pause during boilrun stage
case 8:
    switchState = 0;
    reboil = false;
    digitalWrite(HEATER1, HIGH);
    digitalWrite(HEATER2, HIGH);
    terminal.println("Back from pause, boil started again");
    terminal.flush(); 
break;

//Hop Additions Code
case 9:
  for (int h=0 ; h=hopCounter; h++) {
  int HopAddtime = HOPTIME[h];
       if (ElapsedBoilTime >= (HopAddtime - 1.0) && ElapsedBoilTime <= (HopAddtime + 1.0)) { //BE SURE THAT THE +/- of the elapsedboil time doesnt result in overlay of two hop additions or else it could be confusing
        tone(BUZZER,500,2000);
        terminal.print("Add: ");
        terminal.println(HOPINFO[h]);
        terminal.flush();
        Blynk.notify(HOPINFO[h]);
        tone(BUZZER,500,2000);
       }
      }
break;
     
//boilend = heat off , pump off
case 10:
    switchState = 0;
    boilend = true;
    boilrun = false;
    digitalWrite(HEATER1, LOW);
    digitalWrite(HEATER2, LOW);
    digitalWrite(PUMP, LOW);
    terminal.println("Boil Complete");
    Blynk.notify("CONNECT ALL CHILL TUBING, MAKE SURE FERMENTER IS READY:Boil Complete");
    terminal.flush();
break;

//chill run: Turns pump on once temp is low enough that going through plate chiller will work: Maybe bump up this temperature
case 11:
    switchState = 0;
    chillrun = true;
    boilend = false;
    ChillStartTime = (millis()/60000);
    terminal.println("Pump starting, need to open ball valve!!");
    terminal.flush();
    tone(BUZZER,500,5000);
    digitalWrite(PUMP, HIGH);    
break;

//rechill: turn pump back on after a pause during chillrun stage
case 12:
    switchState = 0;
    rechill = false;
    digitalWrite(PUMP, HIGH);
    terminal.println("Back from pause, chill pumping started again");
    terminal.flush(); 
break;

//chillend == turn pump off once chill time is over THIS NEEDS TO BE TEST AND FIGURED OUT.
case 13:
    switchState = 0;
    chillend = true;
    chillrun = false;
    tone(BUZZER,500,5000);
    digitalWrite(PUMP, LOW); 
    terminal.println("Auto Brewing Complete");
    Blynk.notify("Auto Brewing Complete");  
    terminal.flush();  
break;
  }
 }
}