I am using a digital sensor to control a valve, and an interlock bypass loop. I want to turn on the valve based on a signal from the input, and open/close an interlock bypass based on the same input. I need to open the valve during the process, and leave it open for some interval after the process finishes. I am using millis for timing, since I need to deal with the possibility of the process coming back on during the delay interval.
I am having problems due to a lack of experience with programming. I have had some C in school, but it's been a while. I've been looking at a lot of example sketches, and trying to adapt my code, but I think my logic may be flawed in one way or another.
I'm using led's to represent the valve and interlock circuit, but I've wired in the relay board I'll be using, and it works fine as well.
My sketch works as follows:
- idle condition starts and runs fine.
- when the signal (I'm using a tact switch) is activated one time, it works fine... it turns on the purge, turns off the bypass and waits for the interval to complete before going back to idle.
- if the switch is released, and pressed again during the delay interval, I only get the remainder of the delay interval.
Here is the code.
Any help would be much appreciated.
Thanks,
Steve
P.S.
This is my first post to this forum. I have really tried to do all my homework on this, but the solution has eluded me thus far.
/*
N2 Purge Control
To control an exhaust line purge for dirty vacuum chamber process. We want to turn on purge when vacuum chamber is in use, and
leave it on for some appropriate time (using 10 seconds for testing the sketch). We also have to control an interlock bypass.
When the purge is active, we want to interlock the process based on a flow switch on the purge, but need to be able to bypass
the interlock when we purposefully turn off the purge. Processes are typically shorter than the overpurge time, and start/stop
automatically so we need to remain aware of process starts/stops during purge.
Broken down into idle() and processRunning() functions. Trying to set it up as a state machine; as in
Blink Without Delay sketch.
*/
const int gasBoxSignal = 5; // take a signal from Gas Box pneumatic reed valve or something.
const int purgePin = 9; // used to turn on the purge.
const int bypassPin = 8; // used to control the GB interlock bypass.
int signalState = HIGH; // keep track of the state of the signal pin.
int bypass_delay = 1000; // delay turning off interlock bypass to avoid GB alarms.
unsigned long interval = 10000; // continue to purge "interval" milliseconds after GB signal shuts off.
unsigned long previousMillis = 0;
unsigned long currentMillis;
void setup()
{
pinMode(gasBoxSignal, INPUT_PULLUP); // sets the input signal pin to normally high, closing the switch will sink it.
pinMode(purgePin, OUTPUT); // output signal pin to turn on/off the purge.
pinMode(bypassPin, OUTPUT); // output signal pin to open/close the bypass.
Serial.begin (115200); // initialize serial comms at 115200 baud for debuging.
idle(); // initialize to idle state.
}
void loop()
{
currentMillis = millis();
Serial.print (F("currentMillis is: ")); Serial.println(currentMillis);
Serial.print (F("previousMillis is: ")); Serial.println(previousMillis);
if ((signalState == HIGH) && (currentMillis - previousMillis >= interval)) {
idle();
currentMillis = millis();
previousMillis = currentMillis;
}
else if (signalState == LOW) {
processRunning();
currentMillis = millis();
previousMillis = currentMillis;
}
}
void idle()
{
while (signalState == HIGH) { // no process running.
digitalWrite(bypassPin, HIGH); // bypass GB interlock.
delay(bypass_delay); // delay 1 second.
digitalWrite(purgePin, LOW); // turn off the purge.
signalState = (digitalRead(gasBoxSignal));
Serial.print(F("Process Idle: time = ")); Serial.println(millis());
Serial.print (F("The State of the gas box signal is ")); Serial.println(signalState);
}
Serial.println (F("Exiting idle"));
}
void processRunning()
{
while (signalState == LOW) { // process is running.
digitalWrite(purgePin, HIGH); // turn on the purge.
delay(bypass_delay); // wait 1 sec. before taking GB out of bypass.
digitalWrite(bypassPin, LOW); // Take GB out of bypass.
signalState = (digitalRead(gasBoxSignal));
Serial.print(F("Process Running: time = ")); Serial.println(millis());
Serial.print (F("The State of the gas box signal is ")); Serial.println(signalState);
}
Serial.println (F("Exiting running"));
}