Switch case "stepping through" when i think it shouldnt

So the sketch is to run a home brew kit to make beer, using a "full volume mash" method, once all the mash is complete controlled by a BSC 460, I want a "mashComplete" signal from the BSC460 so open an auto valve to run the mash liquor in to a boil kettle at a controlled rate over a 1 hour period then stop all functions.

Ive attached the Sketch I have so far there is changes to make but I want to understand why its stepping through the switch cases.

Im watching this run via the serial port so I can see it step through straight away.

Any help would be much appreciated

Pesh

sketch_FullVolume_Mash.ino (20.4 KB)

which case is it stepping thru unexpectedly?

looks like it conditionally steps from case 0 to 1, and from case 2, meaning it dwells in cases 1 & 2, but advances to case 2 immediately from case 1.

also, since there not case 3, 4, ..., what do you expect to happen when section is incremented in case 2.

i've never seen a switch statement without a default case; not sure what happens

  if    (MashComplete == HIGH);

If MashComplete is HIGH, do nothing. Otherwise, do nothing. Seems pointless.

     {section = section +1;}

{What's} {with} {the} {useless} {braces} {?}

Your case 0 statement does not have an unconditional break statement. It MUST.

You should use autoformat and then you can see the issues in your code.

This is really messed up:

      if (MashComplete == HIGH)
      {
        Serial.println("HIGH");

        if (MashComplete == LOW)
        {
          Serial.println("LOW");
        }

        if    (MashComplete == HIGH);
        {
          section = section + 1;
        }
        break;
      }

First of all, you will not hit the break unless MashComplete == HIGH otherwise it will fall thru to the next case.

Secondly, if MashComplete == HIGH why are you checking for MashComplete == LOW within that code block? You are also checking again for MashComplete == HIGH but you have a ; after the conditional so it is useless anyway.

The checking for the input to be low was just to see something over the serial port, it has to be high or low so ill get a result either way.

Thanks for the help

Pesh

Pesho77:
it has to be high or low so ill get a result either way.

I doubt it: it only tests for low when it already knows it's not....

It should be set low until the BSC460 sends a signal to set I high, the mash takes 1 hour to complete so it shouldn't do any thing till then, thats what im panning any way, so the serial port should show it as low till then.

I appreciate the code may be wrong which is why I asked for help.

Pesh

Here is my attempt to optimize and clean up the switch. You may find it helpful:

  switch (section) 
  {
    case 0: //////////// wait for mash to complete
      MashComplete = digitalRead(Pin); // Reading status of Arduino digital Pin
      if (MashComplete == HIGH)
      {
        Serial.println("HIGH");
        section = section + 1;
      }
      else
      {
        Serial.println("LOW");
      }
      break;

    case 1: // Start sparge
      openMLTFillValve();    //Open MLT fill valve
      //      delay(5000);
      section = section + 1;
      break;

    case 2: // set sparge rate
      Serial.println(spargeRate);
      Serial.println();
      Serial.println(MLTFillFlowRate);
      Serial.println();
      Serial.println(BKFillFlowRate);
      if (MLTFillFlowRate >= spargeRate  ) 
      {
        MLTFillValveOpenControl() ;
      }
      else
      {
        MLTFillValveCloseControl();
      }
      //  SpargeInterval = 120000;   // wait 2 mins
      //   SpargePreviousMillis = 0;
      if (SpargeCurrentMillis - SpargePreviousMillis > SpargeInterval) 
      {
        //openBKFillValve();
      } // Fill BK
      if (BKFillFlowRate >= spargeRate   ) 
      {
        BKFillValveOpenControl() ;
      }
      else 
      {
        BKFillValveCloseControl();
      }


      //      delay(5000);

      // Section added to time sparge for 1 hour

      S--;
      delay(1000);
      if (S < 0)
      {
        M--;
        S = 59;
      }
      if (M < 0)
      {
        H--;
        M = 59;
      }
      if (H < 0) 
      {
        H = 23;
        M = 59;
        S = 59;
      } 
      if (M <= 9)
      {
        Serial.print("0" );
      }
      Serial.print(M);
      Serial.print("  " );
      Serial.print ("\t");

      if (S <= 9)
      {
        Serial.print("0" );
      }
      Serial.print(S);
      Serial.print("  " );
      Serial.print("\t");

      if (H <= 9)
      {
        Serial.println ();
      }

      if (M == 0 && S == 0)
      {
        Serial.print ("stop");
        section = section + 1;
      }
    }
    break;
  }

It should be set low until the BSC460 sends a signal to set I high, the mash takes 1 hour to complete so it shouldn't do any thing till then, thats what im panning any way, so the serial port should show it as low till then.

Suppose I gave you a glass of liquid, and said "Taste this. If it's beer, go through door #1. If it's grape juice, go through door #2.". You taste it, and say "It taste's like weasel (DELETED). I say "Well, that's Budweiser, so go through door #1".

Would it make ANY sense for anyone in the room beyond door #1 to ask if you glass contains apple juice?

THAT is what your code is asking.