I have a version of this that works perfectly just using an on-off toggle switch. I'm trying to get it to work using a normally open ,momentary push button as a "Start" and a normally open, latching push button as an "Emergency Stop". I'm missing something but I don't know what. The program compiles and uploads but never starts running. Suggestions? Thank you!
//Welder Version1 Rev0
//Includes relay closure to activate welding cycle
//Includes Serial Counting and Auto Shut off when runSize is reached
//Rev1A pushbutton start and emergency stop (NOT FUNCTIONING)
const int SolRelayA = 2; // Tip Loading Plate
const int SolRelayB = 3; // Activate Loading Cylinder
const int SolRelayC = 4; // Welder Clamps
const int SolRelayD = 5; // Activate Blade Lift Cyinder
const int WeldRelayA = 6; // Activate Welder Circuit
const int switchPin = 12; //NO Momentary pushbutton to start
const int estopPin = 11; //NO Latching pushbutton Estop Button
unsigned long previousMillis;
unsigned long timeDelay = 100;
unsigned long printMillis = 0;
unsigned long currentMillis = 0;
int CycleStage = 0;
int buttonState = 0;
int estopState = 1;
int CycleCount = 0;
int runSize = 7;//number of cycles to run before stopping
int runState = 0; //has run been turned on. 0=Off, 1=O
void setup() {
Serial.begin(9600);
digitalWrite (SolRelayA, HIGH); //Set Relay Pins High
digitalWrite (SolRelayB, HIGH);
digitalWrite (SolRelayC, HIGH);
digitalWrite (SolRelayD, HIGH);
digitalWrite (WeldRelayA, HIGH);
pinMode (SolRelayA, OUTPUT); //Set Relay Pins as Outputs
pinMode (SolRelayB, OUTPUT);
pinMode (SolRelayC, OUTPUT);
pinMode (SolRelayD, OUTPUT);
pinMode (WeldRelayA, OUTPUT);
pinMode (switchPin, INPUT_PULLUP);
pinMode (estopPin, INPUT_PULLUP);
}
void loop() {//open loop
estopState = digitalRead (estopPin);
buttonState = digitalRead(switchPin);
currentMillis = millis();//update every loop
if (CycleCount < runSize) {
if ((buttonState == 0) && (estopState = 1)) {
runState == 1;
}
if (estopState = 0) {
runState == 0;
}
if (currentMillis - previousMillis > timeDelay) {
CycleStage++;
previousMillis = currentMillis;//reset timer
}
else if (buttonState == HIGH) {
previousMillis = currentMillis;
timeDelay = 0;
}
// troubleshoot();//hatch this line after testing
if (runState == 1) {
switch (CycleStage) {
case 0:
break;
case 1: //Tip Loading Plate to Vertical
digitalWrite (SolRelayA, LOW);
timeDelay = 1500;
break;
//Insert Tube into Welder
case 2://if CycleStage==1 kinda of arguement
digitalWrite (SolRelayB, LOW);
//delay (?);
timeDelay = 1000;//update timeDelay for next step
break;
case 3://Activate Welder Clamps
digitalWrite (SolRelayC, LOW);
// delay (?);
timeDelay = 200;//update timeDelay for next step
break;
case 4://Start Weld Cycle
digitalWrite (WeldRelayA, LOW);
// delay (?);
timeDelay = 300;//update timeDelay for next step
break;
case 5://Extend Insertion Cylinder
digitalWrite (SolRelayB, HIGH);
timeDelay = 500;//update timeDelay for next step
//delay(?);
break;
case 6://Tip Loading Plate to Horizontal
digitalWrite (SolRelayA, HIGH);
// delay(?);
timeDelay = 1000;//update timeDelay for next step
break;
case 7://Extend Hopper Blade
digitalWrite (SolRelayD, LOW);
//delay (?);
timeDelay = 1000;//update timeDelay for next step
break;
case 8://Stop Weld Cycle,Retract Hopper Blade
digitalWrite (SolRelayC, HIGH);
digitalWrite (SolRelayD, HIGH);
digitalWrite (WeldRelayA, HIGH);
//delay (?);
timeDelay = 1000;//update timeDelay for next step
break;
case 9:
digitalWrite (SolRelayA, HIGH);
digitalWrite (SolRelayB, HIGH);
digitalWrite (SolRelayC, HIGH);
digitalWrite (SolRelayD, HIGH);
digitalWrite (WeldRelayA, HIGH);
CycleCount = CycleCount + 1;
Serial.println(CycleCount);
timeDelay = 100;//9 is now going to loop back to 1
CycleStage = 1; //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);
digitalWrite (WeldRelayA, HIGH);
}
//back in the main loop
} //close count if
}//close main loop
//void troubleshoot() {
// if (currentMillis - printMillis > 250L) {
// Serial.print("buttonstate : ");
// Serial.print(buttonState);
// Serial.println(" 0=on 1=off");//new line
// Serial.print("CycleStage : ");
// Serial.print(CycleStage);
// Serial.print(" timer set point : ");
// Serial.println(timeDelay);//new line
// long time = currentMillis - previousMillis;
// time = timeDelay - time;
// Serial.print("CycleStage : ");
// Serial.print(" timer left: ");
// Serial.println(time);
// Serial.println(" ");
// printMillis = currentMillis;
// }
//}