Your application has two state machines. Start by trying to identify what they are (give them simple names).
Your application essentially has three inputs that drive the two state machines. Try to identify what they are.
Your application has two outputs which you have identified...
int valveOut = 3; //output pin 3
int welderOut = 4; //output pin 4
There are two strategies for writing to outputs: on-transition and in-state. I believe the second will simplify your code a bit. A reasonable structure for the second is...
- Collect inputs into local variables
- Process transitions
- Update outputs
Update outputs might look something like this...
if ( WelderState == WelderOn )
{
digitalWrite(welderOut, LOW); // turn on welder
}
else
{
digitalWrite(welderOut, HIGH); // turn off welder
}
if ( GasState == GasOn )
{
digitalWrite(valveOut, LOW); // turn on valve for gas flow
}
else
{
digitalWrite(valveOut, HIGH); // turn off valve for gas flow
}
I have no doubt that code looks overly verbose. But, if all goes well, you will be able to use your eyeballs and a little bit of brain to determine if the code will or will not work. And, you will be able to easily extend the code. For example, let's say you decide to add an LED to indicate the gas is on. The change is trivial...
if ( GasState == GasOn )
{
digitalWrite(valveOut, LOW); // turn on valve for gas flow
digitalWrite(valveLED, HIGH); // let the human know the gas is supposed to be flowing
}
else
{
digitalWrite(valveOut, HIGH); // turn off valve for gas flow
digitalWrite(valveLED, LOW); // indicate the gas is stopped
}