ok lets think about why the code doesn't work.
if ((buttonState == LOW) && (currentMillis - previousMillis > timeDelay)) {//open if
//if true then change CycleStage and reset timer
CycleStage++;//++means add one same as CycleStage=CycleStage+1
previousMillis = currentMillis;//reset timer
// prevmillis=currentmillis really means reset timer
} //close if
//else keeps the next if linked to the first if
else if (buttonState == HIGH) {//open else if
//what to do if some one lets go off button
previousMillis = currentMillis;//reset timer
timeDelay = 0; //reset to default for step one which is no timer
CycleStage = 7;//im guessing go to last step to stop everything
}//close else if
// back in the main loop
notice the lines above. Everything is wrapped in a if or else if
both are looking at buttonState. The first part says if button ==LOW do this when timer says its done
if that fails check to see if the buttonState is HIGH. If it is then do this
(really should change buttonState to SwitchState as most people think of button as a pushbutton and a switch as maintained)
ok so does anything make the steps run when the button is high.....nope
notice we are returning to main loop
//back in the main loop
switch (CycleStage) {//look up switch its just a fancy if statement
//if CycleStage==? kinda of arguement its a better way of writing it
case 0://if CycleStage==0 kinda of arguement
digitalWrite (SolRelayA, LOW);
timeDelay = 6000;//now you need to understand that i only wrote one timer
//the timer is being used 7 times so after it done it job i have
//to tell it what the next job is
//first job was timedelay==0 as i didnt want it to delay
//next step i want it to wait for 6 seconds
//its not perfect timing but this timing doesnt need it
break;//this allows the processor to get out of the switch after
//doing what ever is in the case that way it doesnt have to look at
//the rest of the code in the cases
ok next section. before this section we are in the main loop.
ok now we get to the switch statement.
do we ask if we should run the switch?. nope we are in main loop its going to hit the switch and as the cyclestage is a number between 0 and 7 its going to run the switch.
Well that not whats required so we need to block the switch from running when ever its read by the main loop.
so what should we use as a block. Maybe we should block unless the button is low. But if we do that then the cylinder relay will stay on. So you have to decided how to deal with this
posted code removes the step 7 block and blocks the switch from main code based on if the button is low. Also added extra lines to shut down the cyclinders if the button is high
//never use pin 0 or 1 unless you have no other option
//your usb is kinda using the pins
const int SolRelayA = 2; // changed this to diffrent pin
const int SolRelayB = 3; // Extend Loading Cylinder
const int SolRelayC = 4; // Start Welding Cycle
const int SolRelayD = 5; // Extend Blade Lift Cyinder
const int switchPin = 12;
unsigned long previousMillis;
unsigned long timeDelay = 0;
int CycleStage = 0;
int buttonState = 0;
void setup() {
Serial.begin(9600);//this allows printing to the plc to see
//whats really happening in the code
//also the reason not to use pin0 and pin1
//i would put pinmode first but compiler seems happy to do it this way
digitalWrite (SolRelayA, HIGH); //Set Relay Pins High
digitalWrite (SolRelayB, HIGH);
digitalWrite (SolRelayC, HIGH);
digitalWrite (SolRelayD, HIGH);
pinMode (SolRelayA, OUTPUT); //Set Relay Pins as Outputs
pinMode (SolRelayB, OUTPUT);
pinMode (SolRelayC, OUTPUT);
pinMode (SolRelayD, OUTPUT);
pinMode (switchPin, INPUT_PULLUP);
}
void loop() {//open loop
unsigned long currentMillis = millis();//update every loop
// what i think you are trying to program
//when i press the button run 7 steps in sequence. add timing
//between the steps, first step is instant
//next line says have you pressed the button and is the timer done
//first press has no delay. time delay was set to 0 on start up
if ((buttonState == LOW) && (currentMillis - previousMillis > timeDelay)) {//open if
//if true then change CycleStage and reset timer
CycleStage++;//++means add one same as CycleStage=CycleStage+1
previousMillis = currentMillis;//reset timer
// prevmillis=currentmillis really means reset timer
} //close if
//else keeps the next if linked to the first if
else if (buttonState == HIGH) {//open else if
//what to do if some one lets go off button
previousMillis = currentMillis;//reset timer
timeDelay = 0; //reset to default for step one which is no timer
}//close else if
// back in the main loop
if (buttonState == LOW) { //block switch below from main loop
switch (CycleStage) {
case 0:
digitalWrite (SolRelayA, LOW);
timeDelay = 6000;
break;
case 1://if CycleStage==1 kinda of arguement
digitalWrite (SolRelayB, LOW);
//delay (5000);
timeDelay = 5000;//update timeDelay for next step
break;
case 2:
digitalWrite (SolRelayC, LOW);
// delay (500);
timeDelay = 500;//update timeDelay for next step
break;
case 3:
digitalWrite (SolRelayB, HIGH);
timeDelay = 500;//update timeDelay for next step
//delay(500);
break;
case 4:
digitalWrite (SolRelayA, HIGH);
// delay(4000);
timeDelay = 4000;//update timeDelay for next step
break;
case 5:
digitalWrite (SolRelayC, HIGH);
digitalWrite (SolRelayD, LOW);
//delay (1000);
timeDelay = 1000;//update timeDelay for next step
break;
case 6:
digitalWrite (SolRelayD, HIGH);
timeDelay = 6000;//update timeDelay for next step
break;
case 7:
digitalWrite (SolRelayA, HIGH);
digitalWrite (SolRelayB, HIGH);
digitalWrite (SolRelayC, HIGH);
digitalWrite (SolRelayD, HIGH);
timeDelay = 6000;//7 is now going to loop back to 0
CycleStage = 0; //go back to step one
break;
}//close switch
//next line is the close to if (buttonState == LOW)
} else { //button is not low so must be high
digitalWrite (SolRelayA, HIGH);
digitalWrite (SolRelayB, HIGH);
digitalWrite (SolRelayC, HIGH);
digitalWrite (SolRelayD, HIGH);
}
//back in the main loop
}//close main loop