Although I am a fan of state machines using switch/case the requirement here is easily met using very simple logic.
const byte welderPin = 13;
const byte gasPin = 12;
const byte buttonPin = A3;
byte currentButtonState = HIGH;
byte previousButtonState = HIGH;
unsigned long currentTime;
unsigned long postFlowStartTime;
unsigned long postFlowPeriod = 5000;
boolean postFlowInProgress = false;
void setup()
{
Serial.begin(115200);
digitalWrite(welderPin, HIGH);
digitalWrite(gasPin, HIGH);
pinMode(gasPin, OUTPUT);
pinMode(welderPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
digitalWrite(welderPin, HIGH);
digitalWrite(gasPin, HIGH);
}
void loop()
{
currentTime = millis();
previousButtonState = currentButtonState;
currentButtonState = digitalRead(buttonPin);
if (currentButtonState != previousButtonState)
{
if (currentButtonState == LOW) //button became pressed
{
digitalWrite(welderPin, LOW); //turn on the welder
digitalWrite(gasPin, LOW); //turn on the gas
postFlowInProgress = false; //allow for welder restart during post flow period
}
else //button became released
{
digitalWrite(welderPin, HIGH); //turn off the welder
postFlowStartTime = currentTime;
postFlowInProgress = true;
}
}
if (postFlowInProgress)
{
if (currentTime - postFlowStartTime >= postFlowPeriod)
{
digitalWrite(gasPin, HIGH); //turn off the gas
postFlowInProgress = false;
}
}
}