What I am trying to do,
When ACPI is switched to LOW I would like the program to delay 20 secs and then drive AC output to LOW.
What is happening,
When I switch ACPI to LOW the first time after reset, the system delays 20 secs and then sends the correct output to AC pin so everything looks as if it is working. When I switch it off AC output shuts off. But if I wait more than 20 secs and turn the switch back on AC output pin immediately goes LOW (no delay). If I turn the switch off and then back on (sooner than 20 secs) the system delays and seems to work as it should. It seems that the millis counter is still running even when ACPI is not switched on. I think it might have to do with my if statements but I cant seem to pinpoint it.
I have tried working through all the tutorials I could find online and adding/modifying to fit my code with no luck. Ive attached my entire code but the only part that is not working is the millis delay section.
Any help is greatly appreciated.
Thanks
Scott
// Initialize Inputs:
const int DPI = 22;
const int PLI = 23;
const int JLI = 24;
const int CBI = 25;
const int BI = 26;
const int DCI = 27;
const int ACPI = 28;
const int TI = 29;
const int E1 = 39;
const int E2 = 41;
const int E3 = 43;
const int E4 = 45;
const int E5 = 30;
const int E6 = 32;
const int E7 = 34;
// Intialize Outputs:
const int T = 31;
const int DC = 33;
const int AC = 35;
const int DP = 36;
const int PL = 38;
const int JL = 40;
const int CB = 42;
const int B = 44;
const int VFD = 37;
// Variables that will change:
int DPIState = 0;
int PLIState = 0;
int JLIState = 0;
int CBIState = 0;
int BIState = 0;
int DCIState = 0;
int ACPIState = 0;
int TIState = 0;
int ACState = LOW;
unsigned long previousMillis = 0;
unsigned long interval = 20000;
void setup(){
//Drive all relays to High to begin
digitalWrite(T, HIGH);
digitalWrite(DC, HIGH);
digitalWrite(AC, HIGH);
digitalWrite(DP, HIGH);
digitalWrite(PL, HIGH);
digitalWrite(JL, HIGH);
digitalWrite(CB, HIGH);
digitalWrite(B, HIGH);
digitalWrite(VFD, HIGH);
digitalWrite(E1, HIGH);
digitalWrite(E2, HIGH);
digitalWrite(E3, HIGH);
digitalWrite(E4, HIGH);
digitalWrite(E5, HIGH);
digitalWrite(E6, HIGH);
digitalWrite(E7, HIGH);
// Pin Outputs to Ouputs
pinMode(T, OUTPUT);
pinMode(DC, OUTPUT);
pinMode(AC, OUTPUT);
pinMode(DP, OUTPUT);
pinMode(PL, OUTPUT);
pinMode(JL, OUTPUT);
pinMode(CB, OUTPUT);
pinMode(B, OUTPUT);
pinMode(VFD, OUTPUT);
pinMode(E1, OUTPUT);
pinMode(E2, OUTPUT);
pinMode(E3, OUTPUT);
pinMode(E4, OUTPUT);
pinMode(E5, OUTPUT);
pinMode(E6, OUTPUT);
pinMode(E7, OUTPUT);
// Pin Inputs to Inputs:
pinMode(DPI, INPUT_PULLUP);
pinMode(PLI, INPUT_PULLUP);
pinMode(JLI, INPUT_PULLUP);
pinMode(CBI, INPUT_PULLUP);
pinMode(BI, INPUT_PULLUP);
pinMode(DCI, INPUT_PULLUP);
pinMode(ACPI, INPUT_PULLUP);
pinMode(TI, INPUT_PULLUP);
}
void loop()
{
// read if 440V transformer should be turned on
TIState = digitalRead(TI);
// check if the pushbutton is pressed.
// if it is, the buttonState is LOW:
if (TIState == LOW){
//Turn on transformer
digitalWrite(T, LOW);
}
else {
digitalWrite(T, HIGH);
}
// AIR COMPRESSOR
// read compressor command switch
ACState = digitalRead(ACPI);
if (ACState == LOW){
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
digitalWrite(AC, LOW);
}
else {
digitalWrite(AC, HIGH);
}
}
// read the state of the Drill Press Switch value:
// Drill Press does not require dust collection
DPIState = digitalRead(DPI);
// check if the pushbutton is pressed.
// if it is, the buttonState is LOW:
if (DPIState == LOW) {
// turn DP relay on and Turn on Inverter
digitalWrite(DP, LOW);
digitalWrite(VFD, LOW);
}
else {
digitalWrite(DP, HIGH);
digitalWrite(VFD, HIGH);
}
// read the state of the Planer Switch value:
//planer requires dust collector
PLIState = digitalRead(PLI);
// check if the pushbutton is pressed.
// if it is, the buttonState is LOW:
if (PLIState == LOW) {
// turn Planer relay on and Turn on Inverter
digitalWrite(PL, LOW);
digitalWrite(VFD, LOW);
digitalWrite(DC, LOW);
}
else {
digitalWrite(PL, HIGH);
digitalWrite(VFD, HIGH);
digitalWrite(DC, HIGH);
}
// read the state of the Jointer Switch value:
//Jointer requires dust collector
JLIState = digitalRead(JLI);
// check if the pushbutton is pressed.
// if it is, the buttonState is LOW:
if (JLIState == LOW) {
// turn Planer relay on and Turn on Inverter
digitalWrite(JL, LOW);
digitalWrite(VFD, LOW);
digitalWrite(DC, LOW);
}
else {
digitalWrite(JL, HIGH);
digitalWrite(VFD, HIGH);
digitalWrite(DC, HIGH);
}
// read the state of the Crescent Bandsaw Switch value:
//Bandsaw requires dust collector
CBIState = digitalRead(CBI);
// check if the pushbutton is pressed.
// if it is, the buttonState is LOW:
if (CBIState == LOW) {
// turn Planer relay on and Turn on Inverter
digitalWrite(CB, LOW);
digitalWrite(VFD, LOW);
digitalWrite(DC, LOW);
}
else {
digitalWrite(CB, HIGH);
digitalWrite(VFD, HIGH);
digitalWrite(DC, HIGH);
}
// read the state of the Oliver Bandsaw Switch value:
//Bandsaw requires dust collector
BIState = digitalRead(BI);
// check if the pushbutton is pressed.
// if it is, the buttonState is LOW:
if (BIState == LOW) {
// turn Planer relay on and Turn on Inverter
digitalWrite(B, LOW);
digitalWrite(VFD, LOW);
digitalWrite(DC, LOW);
}
else {
digitalWrite(B, HIGH);
digitalWrite(VFD, HIGH);
digitalWrite(DC, HIGH);
}
}