Automated dust extraction system, well hopefully

Hi a bit about me, I am a carpenter I can make pretty much anything and if I seen it done once I can usually replicate.
also good at negative earth wiring in cars, but when I comes to reading and learning I'm useless lol.
I got my mega last week to see if I could learn.
So I'm looking to automate my dust collection system like the guy from YouTube channel I like to make stuff. iltms_automated_dust_collection/DustCollectionAutomation_v2.ino at master · iliketomakestuff/iltms_automated_dust_collection · GitHub
I intend to use car door lock actuators to open/close the blast gates and a solid state relay to turn on the extractor. for this I have x16 relay to be able to reverse the polarity control 8 blast gates. I found a code to control current sensing (ACS712) on one tool I will need to add some of code below to open the gates in the correct sequence and then to work with 5 separate tools.
So that leads me on to where I need the help, seems quite trivial I need to add a delay to allow the gates and extractor to run on for 10 seconds when the toggle state changes. Delay doesn't work, it seams to turn off anywhere between 5 and 10 seconds. I also have another toggle and requires a different sequence, when I add this to the sketch the switches seam to have there own mind.
thanks in advance
Curt

const int gate1=22; //open-outlet/ toggle switch
const int gateA=23; //close-outlet/ toggle switch
const int gate2=24; //open-drillpress/current sensor1
const int gateB=25; //close-drillpress/current sensor1
const int gate3=26; //open-if gate a or b open
const int gateC=27; //close-if gate a or b open
const int gate4=28; //open-if gate e,f,g or h open
const int gateD=29; //close-if gate e,f, or h open
const int gate5=30; //open-tablesaw/current sensor2
const int gateE=31; //close-tablesaw/current sensor2
const int gate6=32; //open-mitresaw/current sensor3
const int gateF=33; //close-mitresaw/current sensor3
const int gate7=34; //open-mft/current sensor4
const int gateG=35; //close-mft/current sensor4
const int gate8=36; //open-boom/toggle switch/current sensor5
const int gateH=37; //close-boom/toggle switch/current sensor5
const int SSR=39;   //power to extractor
const int RF1= 41;  //outlet toggle switch
const int RF2= 42;  //boom toggle switch
int RF1Read = 0;
int RF2Read = 0;
unsigned long interval = 10000;
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;


void setup() 
  {
  Serial.begin(9600);   
  pinMode(SSR,  OUTPUT); 
  pinMode(gateA,OUTPUT); 
  pinMode(gateC,OUTPUT);  
  pinMode(gateD,OUTPUT); 
  pinMode(gateH,OUTPUT);
  pinMode(gate1,OUTPUT); 
  pinMode(gate3,OUTPUT);  
  pinMode(gate4,OUTPUT); 
  pinMode(gate8,OUTPUT);
  digitalWrite(gate1, HIGH);
  digitalWrite(gate2, HIGH);
  digitalWrite(gate3, HIGH);
  digitalWrite(gate4, HIGH);
  digitalWrite(gate5, HIGH);
  digitalWrite(gate6, HIGH);
  digitalWrite(gate7, HIGH);
  digitalWrite(gate8, HIGH);
  digitalWrite(gateA, HIGH);
  digitalWrite(gateB, HIGH);
  digitalWrite(gateC, HIGH);
  digitalWrite(gateD, HIGH);
  digitalWrite(gateE, HIGH);
  digitalWrite(gateF, HIGH);
  digitalWrite(gateG, HIGH); 
  digitalWrite(gateH, HIGH); 
  digitalWrite(SSR,   HIGH);
  pinMode(RF1, INPUT_PULLUP); 
  pinMode(RF2, INPUT_PULLUP); 
  }
  
  
void loop() 
  {
unsigned long  currentMillis1 = millis();
   
  RF1Read = digitalRead(RF1);
  RF2Read = digitalRead(RF2);
  
  Serial.print("outlet");
  Serial.print(RF1Read);
  Serial.print(" ");
  Serial.print("boom");
  Serial.println(RF2Read);
  delay(250);

  
if (RF1Read==LOW)//0
  {
  digitalWrite(gate1, LOW);//ON
  digitalWrite(gate3, LOW);
  digitalWrite(SSR,   LOW);
  // I need to delay the above for 10sec after the state changes
  }
  

if (RF1Read == HIGH) //1
  {
  digitalWrite(gate1, HIGH);//off
  digitalWrite(gate3, HIGH);
  digitalWrite(SSR,   HIGH);
  delay(1000);
  digitalWrite(gateA, LOW);//0n
  digitalWrite(gateC, LOW);
  delay(1000);
  digitalWrite(gateA, HIGH);//off
  digitalWrite(gateC, HIGH);
while (digitalRead(RF1) ==HIGH); // do nothing until state changes
  }


  }

Wiring diagram + links to components?

Why can't the blast gates be just tied to the power switches of the different tools and skip the processor altogether?

-jim lee

JCA34F:
Wiring diagram + links to components?

mega 2560 r3

x16 relay board

remote relay as a toggle switch

actuators

Solid state relay

i cant up load a scan of my hand drawn wiring diagram :confused:

jimLee:
Why can't the blast gates be just tied to the power switches of the different tools and skip the processor altogether?

-jim lee

The blast gates will be all over the workshop, 2 gates will be remotely activated and I will be having 2 branches of pipe (poss 3 in the future). If you don't close 1 branch you get a suction drop so I will need 2/3 gates in different places to open/close at the same time whilst turning the extractor on/off.
Should mention I have attempted to use millis for the delay but to no avail as i said it can take awhile for things to sink in :confused:

I think you're going to have to get it working with millis. To that end, look at the state change example in the IDE. It'll be handy because then you can note the time when a tool is turned off, not when it IS off. That's the time you'll need to know that tells you when it's time to turn the extractor off.

Also, you'll want to avoid delay because it would be hard to deal with a situation where you turn one tool off and within the ten second window, start another.

wildbill:
I think you're going to have to get it working with millis. To that end, look at the state change example in the IDE. It'll be handy because then you can note the time when a tool is turned off, not when it IS off. That's the time you'll need to know that tells you when it's time to turn the extractor off.

Also, you'll want to avoid delay because it would be hard to deal with a situation where you turn one tool off and within the ten second window, start another.

I have got it working with millis I found an example that was able to understand. Thanks
The trouble is now that I'm attempting to add another switch to control another a different sequence shown below,
When there is the 2 if statements, the RF switches doesn't control them separately its hard to explain they interfere with each other and wont turn off.

const int gate1=22; //open-outlet/ toggle switch
const int gateA=23; //close-outlet/ toggle switch
const int gate2=24; //open-drillpress/current sensor1
const int gateB=25; //close-drillpress/current sensor1
const int gate3=26; //open-if gate a or b open
const int gateC=27; //close-if gate a or b open
const int gate4=28; //open-if gate e,f,g or h open
const int gateD=29; //close-if gate e,f, or h open
const int gate5=30; //open-tablesaw/current sensor2
const int gateE=31; //close-tablesaw/current sensor2
const int gate6=32; //open-mitresaw/current sensor3
const int gateF=33; //close-mitresaw/current sensor3
const int gate7=34; //open-mft/current sensor4
const int gateG=35; //close-mft/current sensor4
const int gate8=36; //open-boom/toggle switch/current sensor5
const int gateH=37; //close-boom/toggle switch/current sensor5
const int SSR=39;   //power to extractor
const int RF1= 41;  //outlet toggle switch
const int RF2= 42;  //boom toggle switch
int RF1Read = 0;
int RF2Read = 0;
const long interval = 10000;
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;


void setup() 
  {
  Serial.begin(9600);   
  pinMode(SSR,  OUTPUT); 
  pinMode(gateA,OUTPUT); 
  pinMode(gateC,OUTPUT);  
  pinMode(gateD,OUTPUT); 
  pinMode(gateH,OUTPUT);
  pinMode(gate1,OUTPUT); 
  pinMode(gate3,OUTPUT);  
  pinMode(gate4,OUTPUT); 
  pinMode(gate8,OUTPUT);
  digitalWrite(gate1, HIGH);
  digitalWrite(gate2, HIGH);
  digitalWrite(gate3, HIGH);
  digitalWrite(gate4, HIGH);
  digitalWrite(gate5, HIGH);
  digitalWrite(gate6, HIGH);
  digitalWrite(gate7, HIGH);
  digitalWrite(gate8, HIGH);
  digitalWrite(gateA, HIGH);
  digitalWrite(gateB, HIGH);
  digitalWrite(gateC, HIGH);
  digitalWrite(gateD, HIGH);
  digitalWrite(gateE, HIGH);
  digitalWrite(gateF, HIGH);
  digitalWrite(gateG, HIGH); 
  digitalWrite(gateH, HIGH); 
  digitalWrite(SSR,   HIGH);
  pinMode(RF1, INPUT_PULLUP); 
  pinMode(RF2, INPUT_PULLUP); 
  }
  
  
void loop() 
  {
unsigned long  currentMillis = millis();
   
  RF1Read = digitalRead(RF1);
  RF2Read = digitalRead(RF2);
  
  Serial.print("outlet");
  Serial.print(RF1Read);
  Serial.print(" ");
  Serial.print("boom");
  Serial.println(RF2Read);
  

  
if (RF1Read==LOW)//0
  {
  digitalWrite(gate1, LOW);//ON
  digitalWrite(gate3, LOW);
  digitalWrite(SSR,   LOW);
 
  previousMillis=millis();
  
  }
  else
  {
  currentMillis=millis();
  
  if (currentMillis - previousMillis >= interval)
  {
  digitalWrite(gate1, HIGH);//off
  digitalWrite(gate3, HIGH);
  digitalWrite(SSR,   HIGH);
  delay(1000);
  digitalWrite(gateA, LOW);//0n
  digitalWrite(gateC, LOW);
  delay(1000);
  digitalWrite(gateA, HIGH);//off
  digitalWrite(gateC, HIGH);
  
while (digitalRead(RF1) ==HIGH); // do nothing until state changes
  }
  } 


//----------------------------------------------------------------------

  
   if (RF2Read==LOW)
  {
  digitalWrite(gate4,  LOW);//ON
  digitalWrite(gate8,  LOW);
  digitalWrite(SSR,    LOW);
 
previousMillis=millis();
  
  }
  else
  {
  currentMillis=millis();
  
  if (currentMillis - previousMillis >= interval)
  {  
  digitalWrite(gate4, HIGH);//off
  digitalWrite(gate8, HIGH);
  digitalWrite(SSR,   HIGH);
  delay(1000);
  digitalWrite(gateD, LOW);//on
  digitalWrite(gateH, LOW);
  delay(1000);
  digitalWrite(gateD, HIGH);
  digitalWrite(gateH, HIGH);//off
while (digitalRead(RF2 )==HIGH); // do nothing until state changes

  }
   
  }    
  }

I expect the two lines like this are most of your problem:

while (digitalRead(RF2 )==HIGH); // do nothing until state changes

Once execution goes there, RF1 is not read anymore.

In addition though, both switches are touching/working with the previousMillis variable. They need one each.

I put ( digitalRead (RF1 || RF2) == HIGH); and that didn’t change it. With millis statement there would that do away with the need for the While statement? Away from pc so can’t test yet.

That should be:

(digitalRead (RF1) == HIGH) || (digitalRead (RF2) == HIGH)

Although I think as you add tools to the mix, you may need to rethink that method.

Also, will you ever be running multiple tools at the same time?

(digitalRead (RF1) == HIGH) || (digitalRead (RF2) == HIGH);

This comes up with expected primary-expression before ‘||’ token

I used while statement because it was the only thing that found that could stop the if statement from constantly repeating. I’m starting to realize that its going to be an issue.

It will be one tool at a time so only need one sequence at a time.

Is there any thing you can suggest for me to read up on?

this is the link to the code for the current sensor that I was thinking I could integrate in to the code.

State machines would be a useful thing to read up on. Find a tutorial with lots of examples - they're simple things, but surprisingly difficult to comprehend when you first encounter them.

Edit:
To get started, think about the three states the extractor can be in: off, on and waiting for off.

The code to achieve that takes a switch statement and is a simple state machine.

Thanks I will have a read up.