Switch Case Loop Problem

Hey guys,

I have a switch case that is working except I would like it to return to the first step and can't seem to figure out how that's done (except that I should never use "goto"). I've researched loops, do while, etc and can't seem to wrap my head around it.

This is what I want:

I'm asking a motor to turn on with a push button and stay on until another sensor tells it to turn off, at which point I would like to loop back to the idle state of waiting for the button to be pushed, which would start the motor, etc

Thanks for in advance for your help.

Here is what I got:

// States

// BREW MOTOR
const int waitForSignalButton = 90;
const int turnOnBrewMotor = 91;
const int waitForSignalRoller = 92;
const int turnOffBrewMotor = 93;
const int turnOffDelayBrewMotor = 94;
const int turnOnDelayBrewMotor = 96;


// PIN NUMBERS
const int buttonPin = 3;
const int brewMotorPin = 13;
const int rollerPin = 0;

void setup()
{
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(brewMotorPin, OUTPUT);
  pinMode(rollerPin, INPUT_PULLUP);
}

void loop()
{
  static int stateBrewMotor = waitForSignalButton;


  /// PUSH BUTTON LOWERS BREWMOTOR
  switch (stateBrewMotor)
  {
    case waitForSignalButton:

      if (digitalRead(buttonPin) == LOW)  {
        stateBrewMotor = turnOnBrewMotor;
      }

      break;

    case turnOnBrewMotor:
      digitalWrite(brewMotorPin, HIGH);

      stateBrewMotor = waitForSignalRoller;

      break;

    case waitForSignalRoller:

      if (digitalRead(rollerPin) == LOW)  {

        stateBrewMotor = turnOffBrewMotor;

        break;

      case turnOffBrewMotor:
        digitalWrite(brewMotorPin, LOW);

        stateBrewMotor = turnOffDelayBrewMotor;

        break;

      case turnOffDelayBrewMotor:

        stateBrewMotor = stateBrewMotor;

        break;

      }
  }

}

This is your initial state:

  static int stateBrewMotor = waitForSignalButton;

So, way down in the last case

      case turnOffDelayBrewMotor:

        stateBrewMotor = stateBrewMotor;  // <-- change this to initial state (waitForSignalButton)

        break;

Just set stateBrewMotor = waitForSignalButton.
Not sure why you set it to itself - it is unlikely it would change state at that point.

Thanks Dan95!

You know, I tried that before and it didn't work and now it did! I must have bunged something else up when doing the test. Thanks for your help!

You're welcome.
Glad I could help.