Delay/millis = endless switch case

I am setting up a indexing rotary table using relays and an arduino uno. So the goal is to press a button to start the first switch case. all cases will turn off the motor break and start the motor until the sensor counts to two then turn the motor off, turn the break on, open a mold that has rotated to you, and then open a lead pot for "x" seconds and then close it. the issue I am running into is if there is a delay or a millis counter set up then the case will endlessly loop turning the lead pot on and off for the set delay/millis. if there is no delay in the "if" statement it runs fine but then the pot will never open as desired.

const int  sensor = 12;   
const int rotaryTable = 13;       
const int brake = 11;
const int button = 8;
const int relayOne = 7;
const int relayTwo = 6;
const int relayThree = 5;
const int relayFour = 4;
const int pourPot = 3;

int sensorCounter = 0;   
int sensorState = 0;         
int lastsensorState = 0;     

int pushCount = 0;
int pushState = 0;
int lastpushButton = 0;

unsigned long startMillis;

unsigned long currentMillis;

const unsigned long wait = 1000 ;

void setup() {
  // put your setup code here, to run once:
  pinMode(sensor, INPUT);
  pinMode(rotaryTable, OUTPUT);
  pinMode(brake, OUTPUT);
  pinMode(button, INPUT);
  pinMode(relayOne, OUTPUT);
  pinMode(relayTwo, OUTPUT);
  pinMode(relayThree, OUTPUT);
  pinMode(relayFour, OUTPUT);
  pinMode(pourPot, OUTPUT);
  Serial.begin(9600);
  startMillis = millis();
}


void loop() {
  // put your main code here, to run repeatedly:
  sensorState = digitalRead(sensor );


  // compare the sensorState to its previous state
  if (sensorState != lastsensorState) {
    // if the state has changed, increment the counter
    if (sensorState == LOW ) {
      // if the current state is HIGH then the button went from off to on:
      sensorCounter++;
      Serial.println("on");
      Serial.print("number of positions: ");
      Serial.println(sensorCounter);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }

  }
  lastsensorState = sensorState;


  pushState = digitalRead(button);

  if (pushState != lastpushButton) {
    if (pushState == HIGH ) {

      pushCount++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(pushCount);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
  }
  delay(20);
  lastpushButton = pushState;
  currentMillis = millis();

  if (pushCount > 4) pushCount = 1, sensorCounter = 0;

  switch (pushCount) {

    case 1:
      if (sensorCounter == 2) {
        digitalWrite(rotaryTable, LOW);
        digitalWrite(brake, HIGH);
        digitalWrite(relayOne, LOW);
        digitalWrite(pourPot, HIGH);
        if (currentMillis - startMillis >= wait) {
          digitalWrite(pourPot, LOW);
          startMillis = currentMillis;
        }
      } else {
        digitalWrite(brake, LOW);
        delay(100);
        digitalWrite(rotaryTable, HIGH);
        digitalWrite(relayOne, HIGH);
        digitalWrite(relayTwo, HIGH);
        digitalWrite(relayThree, HIGH);
        digitalWrite(relayFour, HIGH);
        break;
      }
    case 2:
      if (sensorCounter == 4) {
        digitalWrite(rotaryTable, LOW);
        digitalWrite(brake, HIGH);
        digitalWrite(relayTwo, LOW);
      } else {
        digitalWrite(brake, LOW);
        delay(100);
        digitalWrite(rotaryTable, HIGH);
        digitalWrite(relayOne, HIGH);
        digitalWrite(relayTwo, HIGH);
        digitalWrite(relayThree, HIGH);
        digitalWrite(relayFour, HIGH);
      }
      break;

    case 3:
      if (sensorCounter == 6) {
        digitalWrite(rotaryTable, LOW);
        digitalWrite(brake, HIGH);
        digitalWrite(relayThree, LOW);
      } else {
        digitalWrite(brake, LOW);
        delay(100);
        digitalWrite(rotaryTable, HIGH);
        digitalWrite(relayOne, HIGH);
        digitalWrite(relayTwo, HIGH);
        digitalWrite(relayThree, HIGH);
        digitalWrite(relayFour, HIGH);
        break;

      case 4:
        if (sensorCounter == 8) {
          digitalWrite(rotaryTable, LOW);
          digitalWrite(brake, HIGH);
          digitalWrite(relayFour, LOW);

        } else {
          digitalWrite(brake, LOW);
          delay(100);
          digitalWrite(rotaryTable, HIGH);
          digitalWrite(relayOne, HIGH);
          digitalWrite(relayTwo, HIGH);
          digitalWrite(relayThree, HIGH);
          digitalWrite(relayFour, HIGH);
        }

        break;
      }

  }
















}

Please show us a schematic and a good image of your wiring.

EDIT

For documentation, it is never a good idea to put multiple lines of code on one line.
It hides your intentions.

if (pushCount > 4) pushCount = 1, sensorCounter = 0;

Better:

if (pushCount > 4)
{
pushCount = 1;
sensorCounter = 0;
}

Ill work on drawing a schematic to make it easier to see.

For documentation, it is never a good idea to put multiple lines of code on one line.
It hides your intentions.

if (pushCount > 4) pushCount = 1, sensorCounter = 0;

Better:

if (pushCount > 4)
{
pushCount = 1;
sensorCounter = 0;
}

Also, be very careful of your case break; location.

    case 1:
      if (sensorCounter == 2) {
        digitalWrite(rotaryTable, LOW);
        digitalWrite(brake, HIGH);
        digitalWrite(relayOne, LOW);
        digitalWrite(pourPot, HIGH);
        if (currentMillis - startMillis >= wait) {
          digitalWrite(pourPot, LOW);
          startMillis = currentMillis;
        }
      } else {
        digitalWrite(brake, LOW);
        delay(100);
        digitalWrite(rotaryTable, HIGH);
        digitalWrite(relayOne, HIGH);
        digitalWrite(relayTwo, HIGH);
        digitalWrite(relayThree, HIGH);
        digitalWrite(relayFour, HIGH);
        break;                                         <----------<<<<<<<
      }

Green-LP:
Ill work on drawing a schematic

While you have your pens out, have a look at the diagram I gave in #11 here to show the various states and transitions in the state machine code from #9 in the same thread.

You may find such a diagram a good way to clarify what you're trying to do.

First kick at the cat:

const byte sensor      = 12;
const byte button      = 8;

const byte rotaryTable = 13;
const byte brake       = 11;
const byte relayOne    = 7;
const byte relayTwo    = 6;
const byte relayThree  = 5;
const byte relayFour   = 4;
const byte pourPot     = 3;

const byte myOutputs[] = {3, 4, 5, 6, 7, 11, 13};

byte sensorCounter = 0;
byte sensorState = 0;
byte lastsensorState = 0;

byte pushCount = 0;
byte pushState = 0;
byte lastpushButton = 0;

unsigned long startMillis;
unsigned long currentMillis;
const unsigned long wait = 1000 ;


//**************************************************************************
void setup()
{
  Serial.begin(9600);

  // put your setup code here, to run once:
  pinMode(sensor, INPUT);
  pinMode(button, INPUT);

  pinMode(rotaryTable, OUTPUT);
  pinMode(brake, OUTPUT);
  pinMode(relayOne, OUTPUT);
  pinMode(relayTwo, OUTPUT);
  pinMode(relayThree, OUTPUT);
  pinMode(relayFour, OUTPUT);
  pinMode(pourPot, OUTPUT);

  for (byte x = 0; x < sizeof(myOutputs); x++)
  {
    pinMode(myOutputs[x], OUTPUT);
  }

  startMillis = millis();

} //END of setup()


//**************************************************************************
void loop()
{
  //**********************
  sensorState = digitalRead(sensor );

  if (sensorState != lastsensorState)
  {
    lastsensorState = sensorState;

    if (sensorState == LOW )
    {
      // if the current state is HIGH then the button went from off to on:
      sensorCounter++;
      Serial.println("on");
      Serial.print("number of positions: ");
      Serial.println(sensorCounter);
    }

    else
    {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }

  } //END of if()

  //**********************
  pushState = digitalRead(button);

  if (pushState != lastpushButton)
  {
    lastpushButton = pushState;

    if (pushState == HIGH )
    {
      pushCount++;

      if (pushCount > 4)
      {
        pushCount = 1;
        sensorCounter = 0;
      }

      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(pushCount);
    }

    else
    {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }

  } //END of if()

  currentMillis = millis();

  //**********************
  switch (pushCount)
  {
    //************
    case 1:
      if (sensorCounter == 2)
      {
        digitalWrite(rotaryTable, LOW);
        digitalWrite(brake, HIGH);
        digitalWrite(relayOne, LOW);
        digitalWrite(pourPot, HIGH);

        if (currentMillis - startMillis >= wait)
        {
          digitalWrite(pourPot, LOW);
          startMillis = currentMillis;
        }
      }

      else
      {
        digitalWrite(brake, LOW);
        delay(100);
        digitalWrite(rotaryTable, HIGH);
        digitalWrite(relayOne, HIGH);
        digitalWrite(relayTwo, HIGH);
        digitalWrite(relayThree, HIGH);
        digitalWrite(relayFour, HIGH);
      }

      break;

    //************
    case 2:
      if (sensorCounter == 4)
      {
        digitalWrite(rotaryTable, LOW);
        digitalWrite(brake, HIGH);
        digitalWrite(relayTwo, LOW);
      }

      else
      {
        digitalWrite(brake, LOW);
        delay(100);
        digitalWrite(rotaryTable, HIGH);
        digitalWrite(relayOne, HIGH);
        digitalWrite(relayTwo, HIGH);
        digitalWrite(relayThree, HIGH);
        digitalWrite(relayFour, HIGH);
      }

      break;

    //************
    case 3:
      if (sensorCounter == 6)
      {
        digitalWrite(rotaryTable, LOW);
        digitalWrite(brake, HIGH);
        digitalWrite(relayThree, LOW);
      }

      else
      {
        digitalWrite(brake, LOW);
        delay(100);
        digitalWrite(rotaryTable, HIGH);
        digitalWrite(relayOne, HIGH);
        digitalWrite(relayTwo, HIGH);
        digitalWrite(relayThree, HIGH);
        digitalWrite(relayFour, HIGH);
      }

      break;

    //************
    case 4:
      if (sensorCounter == 8)
      {
        digitalWrite(rotaryTable, LOW);
        digitalWrite(brake, HIGH);
        digitalWrite(relayFour, LOW);

      }

      else
      {
        digitalWrite(brake, LOW);
        delay(100);
        digitalWrite(rotaryTable, HIGH);
        digitalWrite(relayOne, HIGH);
        digitalWrite(relayTwo, HIGH);
        digitalWrite(relayThree, HIGH);
        digitalWrite(relayFour, HIGH);
      }

      break;

  } //END of switch/case

} //END of loop()

EDIT
Made a few mistakes in previous code.
This would be closer:

also larryd your program did that same thing that mine did but never fully turned the relay on. it lit up the power light just bearly but cycled "on" and "off" for the delayed time.

Am I miss understanding how a switch case functions?
is there a better function to use other than a switch case?

I originally tied only counting the sensor to run the program and when the button was pressed it would start the motor and count. but it had the same issue im having with switch cases if i counted over 2.

if i just count to 2 it runs as intended with the delay.
idk if that helps or aids in understanding what might be happening.

 if (sensorCounter == 2) {
    
    digitalWrite(rotaryTable, LOW);
    digitalWrite(brake, HIGH);
    delay(100);
    digitalWrite(brake, LOW );
   
} else if (pushState = digitalRead(button) == HIGH) {
    digitalWrite(rotaryTable, HIGH );
    digitalWrite(brake, LOW);
    digitalWrite(relayOne, HIGH);
    digitalWrite(relayTwo, HIGH);
    digitalWrite(relayThree, HIGH);
    digitalWrite(relayFour, HIGH);

}
 if (sensorCounter == 2) sensorCounter == 0;

}

The code I offered in #5 had a mistake in it.

I corrected the mistake and re-posted the code back to #5.

I 'did not test' the code.

Your original break; for case 1: should have been out of the else() area.

switch/case 'is' a good way to select alternative pieces of code to execute.

How are you inputs wired?

also larryd your program did that same thing that mine did but never fully turned the relay on. it lit up the power light just bearly but cycled "on" and "off" for the delayed time.

Am I miss understanding how a switch case functions?
is there a better function to use other than a switch case?

I originally tied only counting the sensor to run the program and when the button was pressed it would start the motor and count. but it had the same issue im having with switch cases if i counted over 2.

if i just count to 2 it runs as intended with the delay.
idk if that helps or aids in understanding what might be happening.

if (sensorCounter == 2) sensorCounter == 0;

== is not assigning as you think.

== is not the same as =

if (sensorCounter == 2)
{
sensorCounter = 0;
}

Please add comments to these lines.

    //************
    case 1:
      if (sensorCounter == 2)
      {
        digitalWrite(rotaryTable, LOW);
        digitalWrite(brake, HIGH);
        digitalWrite(relayOne, LOW);
        digitalWrite(pourPot, HIGH);

        if (currentMillis - startMillis >= wait)
        {
          digitalWrite(pourPot, LOW);
          startMillis = currentMillis;
        }
      }

      else
      {
        digitalWrite(brake, LOW);
        delay(100);
        digitalWrite(rotaryTable, HIGH);
        digitalWrite(relayOne, HIGH);
        digitalWrite(relayTwo, HIGH);
        digitalWrite(relayThree, HIGH);
        digitalWrite(relayFour, HIGH);
      }

      break;

    //************
    case 2:
      if (sensorCounter == 4)
      {
        digitalWrite(rotaryTable, LOW);
        digitalWrite(brake, HIGH);
        digitalWrite(relayTwo, LOW);
      }

      else
      {
        digitalWrite(brake, LOW);
        delay(100);
        digitalWrite(rotaryTable, HIGH);
        digitalWrite(relayOne, HIGH);
        digitalWrite(relayTwo, HIGH);
        digitalWrite(relayThree, HIGH);
        digitalWrite(relayFour, HIGH);
      }

      break;

    //************
    case 3:
      if (sensorCounter == 6)
      {
        digitalWrite(rotaryTable, LOW);
        digitalWrite(brake, HIGH);
        digitalWrite(relayThree, LOW);
      }

      else
      {
        digitalWrite(brake, LOW);
        delay(100);
        digitalWrite(rotaryTable, HIGH);
        digitalWrite(relayOne, HIGH);
        digitalWrite(relayTwo, HIGH);
        digitalWrite(relayThree, HIGH);
        digitalWrite(relayFour, HIGH);
      }

      break;

    //************
    case 4:
      if (sensorCounter == 8)
      {
        digitalWrite(rotaryTable, LOW);
        digitalWrite(brake, HIGH);
        digitalWrite(relayFour, LOW);
      }

      else
      {
        digitalWrite(brake, LOW);
        delay(100);
        digitalWrite(rotaryTable, HIGH);
        digitalWrite(relayOne, HIGH);
        digitalWrite(relayTwo, HIGH);
        digitalWrite(relayThree, HIGH);
        digitalWrite(relayFour, HIGH);
      }

      break;

  } //END of switch/case

How are you inputs wired?

Yes thats true i was messing with the code hoping something would work and never changed it back.

my inputs are a 9v inductive sensor that runs off a 9v battery and power goes straight to pin 12.

and a foot pedal that is wired just like a button would be wired with a resistor to ground and input bouncing from the bread board to pin 8

here is the code with notes

  switch (pushCount) {
    
//button presses once and the table rotates twice. the sensor reads twice and moves case 1 into the if statement.
// it should stop after the if statement runs once and waits for the button to be pressed again. 
// this starts case 2.

    case 1:
      if (sensorCounter == 2) {
        digitalWrite(rotaryTable, LOW);
        digitalWrite(brake, HIGH);
        digitalWrite(relayOne, LOW);
        digitalWrite(pourPot, HIGH);
        if (currentMillis - startMillis >= wait) {
          digitalWrite(pourPot, LOW);
          startMillis = currentMillis;
        }
      } else {
        digitalWrite(brake, LOW);
        delay(100);
        digitalWrite(rotaryTable, HIGH);
        digitalWrite(relayOne, HIGH);
        digitalWrite(relayTwo, HIGH);
        digitalWrite(relayThree, HIGH);
        digitalWrite(relayFour, HIGH);
      }
        break;
// the button as been pressed a second time so case to runs and the table rotates twice again counting to 4.
//case 2 moves into the if statement once and then waits for the button to be pressed a third time. 

    case 2:
      if (sensorCounter == 4) {
        digitalWrite(rotaryTable, LOW);
        digitalWrite(brake, HIGH);
        digitalWrite(relayTwo, LOW);
      } else {
        digitalWrite(brake, LOW);
        delay(100);
        digitalWrite(rotaryTable, HIGH);
        digitalWrite(relayOne, HIGH);
        digitalWrite(relayTwo, HIGH);
        digitalWrite(relayThree, HIGH);
        digitalWrite(relayFour, HIGH);
      }
      break;
// the button as been pressed a third time so case to runs and the table rotates twice again counting to 6.
//case 3 moves into the if statement once and then waits for the button to be pressed a fourth time. 
    case 3:
      if (sensorCounter == 6) {
        digitalWrite(rotaryTable, LOW);
        digitalWrite(brake, HIGH);
        digitalWrite(relayThree, LOW);
      } else {
        digitalWrite(brake, LOW);
        delay(100);
        digitalWrite(rotaryTable, HIGH);
        digitalWrite(relayOne, HIGH);
        digitalWrite(relayTwo, HIGH);
        digitalWrite(relayThree, HIGH);
        digitalWrite(relayFour, HIGH);
        break;
// the button as been pressed a fourth time so case to runs and the table rotates twice again counting to 8.
//case 4 moves into the if statement once and then waits for the button to be pressed again. this time
//the counts have been reset and it starts back at case 1 to cycle through the cases again. 
      case 4:
        if (sensorCounter == 8) {
          digitalWrite(rotaryTable, LOW);
          digitalWrite(brake, HIGH);
          digitalWrite(relayFour, LOW);

        } else {
          digitalWrite(brake, LOW);
          delay(100);
          digitalWrite(rotaryTable, HIGH);
          digitalWrite(relayOne, HIGH);
          digitalWrite(relayTwo, HIGH);
          digitalWrite(relayThree, HIGH);
          digitalWrite(relayFour, HIGH);
        }

        break;
      }

  }

my inputs are a 9v inductive sensor that runs off a 9v battery and power goes straight to pin 12.

We need to see the schematic of this to ensure you are not feeding 9 volts to pin 12 :o

and a foot pedal that is wired just like a button would be wired with a resistor to ground and input bouncing from the bread board to pin 8

Draw us a schematic of this foot switch cct., schematic please.

BTW

You have jumpers installed on your relay PCBs.

These may have to be removed, but for now leave them as is.

We need to see how the relay PCBs are wired, schematic please.

//button presses once and the table rotates twice. the sensor reads twice and moves case 1 into the if statement.
// it should stop after the if statement runs once and waits for the button to be pressed again.

Are you saying:

You press the foot switch one time.
**The table then makes 2 complete (360 degree) rotations. **
i.e. The sensor produces one pulse per rotation.

digitalWrite(rotaryTable, LOW);  <------<<<< LOW does what?
digitalWrite(rotaryTable, HIGH); <------<<<< HIGH does what?

digitalWrite(brake, LOW);        <------<<<< LOW does what?
digitalWrite(brake, HIGH);       <------<<<< HIGH does what?

digitalWrite(relayOne, LOW);     <------<<<< LOW does what?
digitalWrite(relayOne, HIGH);    <------<<<< HIGH does what?

digitalWrite(relayTwo, LOW);     <------<<<< LOW does what?
digitalWrite(relayTwo, HIGH);    <------<<<< HIGH does what?

digitalWrite(relayThree, LOW);   <------<<<< LOW does what?
digitalWrite(relayThree, HIGH);  <------<<<< HIGH does what?

digitalWrite(relayFour, LOW);    <------<<<< LOW does what?
digitalWrite(relayFour, HIGH);   <------<<<< HIGH does what?

digitalWrite(pourPot, LOW);      <------<<<< LOW does what?
digitalWrite(pourPot, HIGH);     <------<<<< HIGH does what?

the indexing table has 8 positions that it is geared to stop at.

so when it the sensor counts to two its counting the gear making a full rotation which ends up being one position change on the table. once the table move two positions or 90 degrees of rotation the sensors reads 2 positions have been rotated.

the sensor only outputs 2v. its normally open so when the sensor senses something the voltage drops to less than .5v

do you have an example of a schematic? I tried using tinkercads circuits but they dont have my relays or sensors. im trying to just draw one by hand in the mean time

digitalWrite(rotaryTable, LOW); turns the motor off
digitalWrite(rotaryTable, HIGH); turns the motor on

digitalWrite(brake, LOW); turns the motor brake off
digitalWrite(brake, HIGH); turns the motor brake on

digitalWrite(relayOne, LOW); opens a pneumatic actuator
digitalWrite(relayOne, HIGH); closes a pneumatic actuator

digitalWrite(relayTwo, LOW); opens a pneumatic actuator
digitalWrite(relayTwo, HIGH); closes a pneumatic actuator

digitalWrite(relayThree, LOW); opens a pneumatic actuator
digitalWrite(relayThree, HIGH); closes a pneumatic actuator

digitalWrite(relayFour, LOW); opens a pneumatic actuator
digitalWrite(relayFour, HIGH); closes a pneumatic actuator

digitalWrite(pourPot, LOW); turns a mini actuator off
digitalWrite(pourPot, HIGH); turns a mini actuator on

at min-6:18 is the goal. i have gotten it to do everything in my program except pour the lead that needs a delay of some kind to keep it open.

Trying to understand what is going on in your project.

digitalWrite(pourPot, LOW); turns a mini actuator off <----<<< is this Closed?
digitalWrite(pourPot, HIGH); turns a mini actuator on <----<<< is this OPEN?

Still need to see some schematics . . . . . .

here is the relay schematic

Where are you getting the 5V to power the relay coils?

You 'should be using' an external 5V supply, not the Arduino 5V as you may damage on board components.

im jumping from the arduino 5v pin to the breadboard positive.

then from the breadboard positive to the relay.
ground goes from arduino ground to breadboard ground then to relay ground.

Green-LP:
im jumping from the arduino 5v pin to the breadboard positive.

then from the breadboard positive to the relay.
ground goes from arduino ground to breadboard ground then to relay ground.

How will you be powering the Arduino, from the power jack, if so, what voltage?

digitalWrite(pourPot, LOW); turns a mini actuator off <----<<< is this Closed?
digitalWrite(pourPot, HIGH); turns a mini actuator on <----<<< is this OPEN?