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;
}
}
}

