Dust Collection blast gate controls

Hello folks, I'm fresh new on the forum so please forgive if not properly using it. I used to code in C++ about 10 years ago, so I just realize I'm veeeeeery rusty.

I'm unsuccessfully trying to automate my dust collection blast gates integrated with dust collection system based on the following conditions:

  1. Blast gate after open(ON) and receives a close signal (OFF) should stay on for 15 seconds
  2. If the the power switched is on during the off countdown interval the signal pin (2) have to ON immediately.

I'm using milils function since delay won't meet condition #2

I'm looking for suggestions here even if have to take a total different approach.

This code is to read the state of an analog input and turns on output pin (2) to HIGH if the sensor voltage is more than 0.5V and delay for 15 seconds blast gates closing (off) when sensor senses less than 0.5V.
This controls pneumatic accutuators cilinders to open and close blast gates.

The Dust Collection engine has a contactor with delay off built-in which is set to delay 15 seconds after receiving a signal off (0V) on the contactor control pin which comes straight from the power tools with a current sensing relay which is the same one sending the 0.5V(ON) or 0V (off) signal or the (A0) on Arduino.

Arduino is only controling the blast gates. After (ON) if the voltage is lower than 0.5V, the Dust collection will run for 15 seconds and the blast gates have to stay open (on) for the same time.

Below is the code I'm using.

// Constants
const int tSawsense = A0; // pin that will sense the voltage from respective tool current sensing device
const int TableSaw = 2; // pin that signal to open (ON) or close (OFF) saw blast gate via BC-548 transistor

void setup() {
// initialize the pin 2 as an output:
pinMode(TableSaw, OUTPUT);
// initialize serial communications:
Serial.begin(9600);
}

void loop() {
// read the value of the current sensing device:
int sensorValue = analogRead(tSawsense);
int currentMillis = millis();
int delaytime = 15000;
// Convert the analog reading
float voltage = sensorValue * (5.0 / 1023.0);
// if the analog value is bigger than 0.5V, open (ON) TableSaw blast gate.
if (voltage > 0.5) {
digitalWrite(TableSaw, HIGH);
} else {
currentMillis = millis();
delaytime = 15000;
if ( (millis() > ( currentMillis + delaytime))) {
digitalWrite(TableSaw, LOW);

}
}
// print out the voltage for monitoring purposes.Will be removed after code is working as expected.
Serial.println(voltage);
}

Depending on the Arduino you're using, int may not be big enough to store millis after the system has been running for a little while. It's more usual to use unsigned long for your time variables.

Your fifteen second check is flawed. You really need to detect when the power went off, not that it is off. Look at the state change example in the IDE to see how this can be done.

Thanks wild bill for the 2 suggestion. I'm using a Arduino Mega 2560. I'm tuned the whole power off once a day but is a good optimization using unsigned long. Will do that after get this logic to work. Regarding the detection that power is off. Changed that. Do your think that will do the job? See below "in red".

// Constants
const int tSawsense = A0; // pin that will sense the voltage from respective tool current sensing device
const int TableSaw = 2; // pin that opens (ON) or closes (OFF) saw blast gate

void setup() {
// initialize the LED pin as an output:
pinMode(TableSaw, OUTPUT);
// initialize serial communications:
Serial.begin(9600);
}

void loop() {
// read the value of the current sensing device:
int sensorValue = analogRead(tSawsense);
int currentMillis = millis();
int delaytime = 15000;
// Convert the analog reading
float voltage = sensorValue * (5.0 / 1023.0);
// if the analog value is bigger than 0.5V, turn on TableSaw blast gate.
if (voltage > 0.5) {
digitalWrite(TableSaw, HIGH);
} else {
if (voltage < 0.5) {
currentMillis = millis();
delaytime = 15000;
if ( (millis() > ( currentMillis + delaytime))) {
digitalWrite(TableSaw, LOW);

}
}
// print out the voltage for monitoring purposes.Will be removed after code is working as expected.
Serial.println(voltage);
}
}

I'd suggest that you make the unsigned long change now. Otherwise things will go bad after 32 seconds of testing.

Your change won't work as desired. You still have this:

currentMillis = millis();
     delaytime = 15000;
     if ( (millis() > ( currentMillis + delaytime))) {

Since you set currentMillis to millis and microseconds later compare it to millis+15S, that if will never be true.

You still need to detect when the saw was powered down, not that it is powered down. That way you can store the time it happened once and check that for millis being 15 seconds later to drive closing the gate.

Hey there wildbill,

thanks for the guidance. I got it to work as expected. As mentioned before is being a while since my last interaction with C++ so I do believe there room for improvement and optimization but it's working. Now that is working I will remove all "Serial.println" that I put to help me out.

Below is the code:

// Constants
const int tSawsense = A0; // pin that will sense the voltage from respective tool current sensing device
const int TableSaw = 2; // pin that opens (ON) or closes (OFF) saw blast gate
unsigned long currentMillis;
unsigned long stoptime;
const int delaytime = 15000;

void setup() {
// initialize the LED pin as an output:
pinMode(TableSaw, OUTPUT);
// initialize serial communications:
Serial.begin(9600);

}

void loop() {
// read the value of the current sensing device:
int sensorValue = analogRead(tSawsense);
int StateTSaw = digitalRead(TableSaw);

// Convert the analog reading
float voltage = sensorValue * (5.0 / 1023.0);
// if the analog value is bigger than 0.5V, turn on TableSaw blast gate.
if (voltage > 0.5) {
digitalWrite(TableSaw, HIGH);
StateTSaw = digitalRead(TableSaw);
stoptime = 0;
Serial.println("Voltage is:");
Serial.println(voltage);
Serial.println("Table Saw Blast gate state is:");
Serial.println(StateTSaw);
} else{
currentMillis = millis(); // acquire current time
if ( (voltage < 0.5) & (stoptime == 0)) { //check if there was a state change
stoptime = ( currentMillis + delaytime ); // set stoptime to
Serial.println(stoptime);
Serial.println("starting the OFF countdown");
}
else {
StateTSaw = digitalRead(TableSaw);
if ( (millis() >= stoptime) & (StateTSaw == 1) ) { // create this stop multiple unnecessary writes after the the output is actually OFF
Serial.println(stoptime);
Serial.println("Closing Blastgate now");
digitalWrite(TableSaw, LOW);
StateTSaw = digitalRead(TableSaw);
Serial.println("Table Saw Blast gate state is:");
Serial.println(StateTSaw);
}
}
}
}