Manual switch for automated dust collector

/*
   This code is for the project at
   http://www.iliketomakestuff.com/how-to-automate-a-dust-collection-system-arduino
   All of the components are list on the url above.

  This script was created by Bob Clagett for I Like To Make Stuff
  For more projects, check out iliketomakestuff.com and modified by Terry Neal

  Includes Modified version of "Measuring AC Current Using ACS712"
  http://henrysbench.capnfatz.com/henrys-bench/arduino-current-measurements/acs712-arduino-ac-current-tutorial/

  Parts of this sketch were taken from the keypad and servo sample sketches that comes with the keypad and servo libraries.

  Uses https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library
*/
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

// called this way, it uses the default address 0x40
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
// you can also call it with a different address you want
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);

// Depending on your servo make, the pulse width min and max may vary, you
// want these to be as small/large as possible without hitting the hard stop
// for max range. You'll have to tweak them as necessary to match the servos you
// have!
//servos used DS3218mg
// our servo # counter
uint8_t servoCount = 4;
uint8_t servonum = 0;

const int OPEN_ALL = 100;
const int CLOSE_ALL = 99;

boolean buttonTriggered = 0;
boolean powerDetected = 0;
boolean collectorIsOn = 0;
int DC_spindown = 3000;
//allow vacuum system to run after tool is turned off
const int NUMBER_OF_TOOLS = 4;
const int NUMBER_OF_GATES = 4;

String tools[NUMBER_OF_TOOLS] = {"Spindle sander", "Belt sander", "Miter saw", "Router"};
int voltSensor[NUMBER_OF_TOOLS] = {A0, A1, A2, A3};
long int voltBaseline[NUMBER_OF_TOOLS] = {0, 0, 0, 0};


//Set the throw of each gate separately, if needed
int gateMinMax[NUMBER_OF_GATES][4
                               ] = {

  /*open,close*/

  {220, 135}, //Spindle sander
  {305, 140}, //Belt sander
  {265, 120}, //Miter saw
  {260, 135}, //Router


};

//keep track of gates to be toggled ON/OFF for each tool
int gates[NUMBER_OF_TOOLS][NUMBER_OF_GATES] = {

  {1, 0, 0, 0},
  {0, 1, 0, 0},
  {0, 0, 1, 0},
  {0, 0, 0, 1},
};

const int dustCollectionRelayPin = 11;
const int manualSwitchPin = 12; //for button activated gate, currently NOT implemented

int mVperAmp = 185; // use 100 for 20A Module and 66 for 30A Module
double ampThreshold = .20;

double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;

//button debouncing
int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup() {
  Serial.begin(9600);
  pinMode(dustCollectionRelayPin, OUTPUT);
  pwm.begin();
  pwm.setPWMFreq(60);  // Default is 1000mS

  //record baseline sensor settings
  //currently unused, but could be used for voltage comparison if need be.
  delay(1000);
  for (int i = 0; i < NUMBER_OF_TOOLS; i++) {
    pinMode(voltSensor[i], INPUT);
    voltBaseline[i] = analogRead(voltSensor[i]);
  }

}

void loop() {
  // use later for button debouncing
  reading = digitalRead(manualSwitchPin);

  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH) {
      state = LOW;
      buttonTriggered = false;
    } else {
      state = HIGH;
      buttonTriggered = true;
      time = millis();
    }
  }
  previous = reading;
  Serial.println("----------");
  //loop through tools and check
  int activeTool = 50;// a number that will never happen
  for (int i = 0; i < NUMBER_OF_TOOLS; i++) {
    if ( checkForAmperageChange(i)) {
      activeTool = i;
      exit;
    }
    if ( i != 0) {
      if (checkForAmperageChange(0)) {
        activeTool = 0;
        exit;
      }
    }
  }
  if (activeTool != 50) {
    // use activeTool for gate processing
    if (collectorIsOn == false) {
      //manage all gate positions
      for (int s = 0; s < NUMBER_OF_GATES; s++) {
        int pos = gates[activeTool][s];
        if (pos == 1) {
          openGate(s);
        } else {
          closeGate(s);
        }
      }
      delay(3000);
      //delay added to allow gates to open before vacuum system comes on
      turnOnDustCollection();
    }
  } else {
    if (collectorIsOn == true) {
      delay(DC_spindown);
      turnOffDustCollection();
    }
  }
}
boolean checkForAmperageChange(int which) {
  Voltage = getVPP(voltSensor[which]);
  VRMS = (Voltage / 2.0) * 0.707;
  AmpsRMS = (VRMS * 1000) / mVperAmp;
  Serial.print(tools[which] + ": ");
  Serial.print(AmpsRMS);
  Serial.println(" Amps RMS");
  if (AmpsRMS > ampThreshold) {
    return true;
  } else {
    return false;
  }
}
void turnOnDustCollection() {
  Serial.println("turnOnDustCollection");
  digitalWrite(dustCollectionRelayPin, 1);
  collectorIsOn = true;
}
void turnOffDustCollection() {
  Serial.println("turnOffDustCollection");
  digitalWrite(dustCollectionRelayPin, 0);
  collectorIsOn = false;
}

float getVPP(int sensor)
{
  float result;

  int readValue;             //value read from the sensor
  int maxValue = 0;          // store max value here
  int minValue = 1024;          // store min value here

  uint32_t start_time = millis();
  while ((millis() - start_time) < 500) //sample for 1 Sec
  {
    readValue = analogRead(sensor);
    // see if you have a new maxValue
    if (readValue > maxValue)
    {
      /*record the maximum sensor value*/
      maxValue = readValue;
    }
    if (readValue < minValue)
    {
      /*record the maximum sensor value*/
      minValue = readValue;
    }
  }

  // Subtract min from max
  result = ((maxValue - minValue) * 5.0) / 1024.0;

  return result;
}

void closeGate(uint8_t num) {
  Serial.print("closeGate ");
  Serial.println(num);
  pwm.setPWM(num, 0, gateMinMax[num][1]);
}
void openGate(uint8_t num) {
  Serial.print("openGate ");
  Serial.println(num);
  pwm.setPWM(num, 0, gateMinMax[num][0]);
  delay(100);
  pwm.setPWM(num, 0, gateMinMax[num][0] - 5);
}

I am building a automated dust collector for my wood shop. I got the idea from a youtube video. I finally got everything working. Now I want to add one more feature.
I want to put in a switch to turn the dust collector on manually. There is a line in the code for it, but I dont know how to wire it in.

manual does not have to be done through the software, but can be done with a simple switch to the motor.

emergency stop SHOULD be done with a switch to the power.

did you ever make a sketch or drawing of your system ?

I would also suggest you edit the comment section of your sketch.
remove the fluff and keep the credit of the originator and preserve that link
ADD to it something like :
// refined with help of the Arduino forum on this thread
// Automated Dust Collection - Project Guidance - Arduino Forum

Yet another thread about the same project and still no circuit diagram? Without at least an up to date circuit diagram there's not much that can be done to help with wiring stuff in.

Looks like you have a variable called 'state' that you toggle with each press of the manual button. You then just need to set the dust collector to ON even if no device is powered up. This should do that:

  if (activeTool != 50 || state) {

I havent had time to draw a diagram yet but it is fairly simple. I have 4 servos, so far, there will be more.
One for each tool. I am using a adafruit 12 bit 16 pwm servo bridge to power the servos. I have a acs712 inline on each tool to detect that the tool has been turned on. Once the tool is turned on, the servo for that tool, opens and then I have a relay to turn on the vacuum system. The relay for the vacuum gets a signal from pin 11.The signal from each acs712 is connected to A0,A1,A2,and A3.When one tool is turned on, the servo for that tool will open and close the servo from the previous tool.
I have one blast gate that does not have a servo on it. It will be for a floor sweep.
I need to add a switch to turn the vacuum system on for the floor sweep. Before the vacuum system comes on ,I need it to close all of the blast gates.
I hope this is clear. I thinking and typing at the same time
I am also using a mega 2560 instead of a uno so I will have more A0 for tool expansion

/*
   This code is for the project at
   http://www.iliketomakestuff.com/how-to-automate-a-dust-collection-system-arduino
   All of the components are list on the url above.

  This script was created by Bob Clagett for I Like To Make Stuff
  For more projects, check out iliketomakestuff.com 

  Includes Modified version of "Measuring AC Current Using ACS712"
  http://henrysbench.capnfatz.com/henrys-bench/arduino-current-measurements/acs712-arduino-ac-current-tutorial/

  Parts of this sketch were taken from the keypad and servo sample sketches that comes with the keypad and servo libraries.

  Uses https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library
*/
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

// called this way, it uses the default address 0x40
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
// you can also call it with a different address you want
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);

// Depending on your servo make, the pulse width min and max may vary, you
// want these to be as small/large as possible without hitting the hard stop
// for max range. You'll have to tweak them as necessary to match the servos you
// have!
//servos used DS3218mg
// our servo # counter
uint8_t servoCount = 4;
uint8_t servonum = 0;

const int OPEN_ALL = 100;
const int CLOSE_ALL = 99;

boolean buttonTriggered = 0;
boolean powerDetected = 0;
boolean collectorIsOn = 0;
int vacuum_spindown = 4000;
//allow vacuum system to run after tool is turned off
const int NUMBER_OF_TOOLS = 4;
const int NUMBER_OF_GATES = 4;

String tools[NUMBER_OF_TOOLS] = {"Spindle sander", "Belt sander", "Miter saw", "Router"};
int voltSensor[NUMBER_OF_TOOLS] = {A0, A1, A2, A3};
long int voltBaseline[NUMBER_OF_TOOLS] = {0, 0, 0, 0};


//Set the throw of each gate separately, if needed
int gateMinMax[NUMBER_OF_GATES][4
                               ] = {

  /*open,close*/

  {225, 150}, //Spindle sander good numbers
  {300, 155}, //Belt sander good numbers
  {265, 130}, //Miter saw good numbers
  {260, 130}, //Router good numbers


};

//keep track of gates to be toggled ON/OFF for each tool
int gates[NUMBER_OF_TOOLS][NUMBER_OF_GATES] = {

  {1, 0, 0, 0},
  {0, 1, 0, 0},
  {0, 0, 1, 0},
  {0, 0, 0, 1},
  
};

const int dustCollectionRelayPin = 11;
const int manualSwitchPin = 12; //for button activated gate, currently NOT implemented

int mVperAmp = 185; // use 100 for 20A Module and 66 for 30A Module
double ampThreshold = .20;

double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;

//button debouncing
int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup() {
  Serial.begin(9600);
  pinMode(dustCollectionRelayPin, OUTPUT);
  pwm.begin();
  pwm.setPWMFreq(60);  // Default is 1000mS

  //record baseline sensor settings
  //currently unused, but could be used for voltage comparison if need be.
  delay(1000);
  for (int i = 0; i < NUMBER_OF_TOOLS; i++) {
    pinMode(voltSensor[i], INPUT);
    voltBaseline[i] = analogRead(voltSensor[i]);
  }

}

void loop() {
  // use later for button debouncing
  reading = digitalRead(manualSwitchPin);

  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH) {
      state = LOW;
      buttonTriggered = false;
    } else {
      state = HIGH;
      buttonTriggered = true;
      time = millis();
    }
  }
  previous = reading;
  Serial.println("----------");
  //loop through tools and check
  int activeTool = 50;// a number that will never happen
  for (int i = 0; i < NUMBER_OF_TOOLS; i++) {
    if ( checkForAmperageChange(i)) {
      activeTool = i;
      exit;
    }
    if ( i != 0) {
      if (checkForAmperageChange(0)) {
        activeTool = 0;
        exit;
      }
    }
  }
  if (activeTool != 50) {
    // use activeTool for gate processing
    if (collectorIsOn == false) {
      //manage all gate positions
      for (int s = 0; s < NUMBER_OF_GATES; s++) {
        int pos = gates[activeTool][s];
        if (pos == 1) {
          openGate(s);
        } else {
          closeGate(s);
        }
      }
      delay(2500);
      //delay added to allow gates to open before vacuum system comes on
      turnOnDustCollection();
    }
  } else {
    if (collectorIsOn == true) {
      delay(vacuum_spindown);
      turnOffDustCollection();
    }
  }
}
boolean checkForAmperageChange(int which) {
  Voltage = getVPP(voltSensor[which]);
  VRMS = (Voltage / 2.0) * 0.707;
  AmpsRMS = (VRMS * 1000) / mVperAmp;
  Serial.print(tools[which] + ": ");
  Serial.print(AmpsRMS);
  Serial.println(" Amps RMS");
  if (AmpsRMS > ampThreshold) {
    return true;
  } else {
    return false;
  }
}
void turnOnDustCollection() {
  Serial.println("turnOnDustCollection");
  digitalWrite(dustCollectionRelayPin, 1);
  collectorIsOn = true;
}
void turnOffDustCollection() {
  Serial.println("turnOffDustCollection");
  digitalWrite(dustCollectionRelayPin, 0);
  collectorIsOn = false;
}

float getVPP(int sensor)
{
  float result;

  int readValue;             //value read from the sensor
  int maxValue = 0;          // store max value here
  int minValue = 1024;          // store min value here

  uint32_t start_time = millis();
  while ((millis() - start_time) < 500) //sample for 1 Sec
  {
    readValue = analogRead(sensor);
    // see if you have a new maxValue
    if (readValue > maxValue)
    {
      /*record the maximum sensor value*/
      maxValue = readValue;
    }
    if (readValue < minValue)
    {
      /*record the maximum sensor value*/
      minValue = readValue;
    }
  }

  // Subtract min from max
  result = ((maxValue - minValue) * 5.0) / 1024.0;

  return result;
}

void closeGate(uint8_t num) {
  Serial.print("closeGate ");
  Serial.println(num);
  pwm.setPWM(num, 0, gateMinMax[num][1]);
}
void openGate(uint8_t num) {
  Serial.print("openGate ");
  Serial.println(num);
  pwm.setPWM(num, 0, gateMinMax[num][0]);
  delay(100);
  pwm.setPWM(num, 0, gateMinMax[num][0] - 5);
}

Could you add a tool (update NUMBER_OF_TOOLS) and add elements to the necessary arrays, voltSensor, voltBaseline and gates? Use a toggle switch (instead of a momentary) for the floor sweep function and tie the switch to, say, A4 in place of a current sensor. When you turn the switch "on" (5V) the program treats it like a tool has turned on and turns on the dust collector and positions the gates according to the new entry in the gates array.

I rewrote part of the code and added a tool to A4. Now I guess all I have to do is send 5 vts to pin A4. Is this what you have in mind? I havent went out to the shop yet to test it.

/*
   This code is for the project at
   http://www.iliketomakestuff.com/how-to-automate-a-dust-collection-system-arduino
   All of the components are list on the url above.

  This script was created by Bob Clagett for I Like To Make Stuff
  For more projects, check out iliketomakestuff.com 

  Includes Modified version of "Measuring AC Current Using ACS712"
  http://henrysbench.capnfatz.com/henrys-bench/arduino-current-measurements/acs712-arduino-ac-current-tutorial/

  Parts of this sketch were taken from the keypad and servo sample sketches that comes with the keypad and servo libraries.

  Uses https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library
*/
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

// called this way, it uses the default address 0x40
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
// you can also call it with a different address you want
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);

// Depending on your servo make, the pulse width min and max may vary, you
// want these to be as small/large as possible without hitting the hard stop
// for max range. You'll have to tweak them as necessary to match the servos you
// have!
//servos used DS3218mg
// our servo # counter
uint8_t servoCount = 5;
uint8_t servonum = 0;

const int OPEN_ALL = 100;
const int CLOSE_ALL = 99;

boolean buttonTriggered = 0;
boolean powerDetected = 0;
boolean collectorIsOn = 0;
int vacuum_spindown = 4000;
//allow vacuum system to run after tool is turned off
const int NUMBER_OF_TOOLS = 5;
const int NUMBER_OF_GATES = 5;

String tools[NUMBER_OF_TOOLS] = {"Spindle sander", "Belt sander", "Miter saw", "Router"};
int voltSensor[NUMBER_OF_TOOLS] = {A0, A1, A2, A3,A4};
long int voltBaseline[NUMBER_OF_TOOLS] = {0, 0, 0, 0,0};


//Set the throw of each gate separately, if needed
int gateMinMax[NUMBER_OF_GATES][5
                               ] = {

  /*open,close*/

  {225, 150}, //Spindle sander good numbers
  {300, 155}, //Belt sander good numbers
  {265, 130}, //Miter saw good numbers
  {260, 130}, //Router good numbers
  {000, 000}, //Floor sweep


};

//keep track of gates to be toggled ON/OFF for each tool
int gates[NUMBER_OF_TOOLS][NUMBER_OF_GATES] = {

  {1, 0, 0, 0},
  {0, 1, 0, 0},
  {0, 0, 1, 0},
  {0, 0, 0, 1},
  {0, 0, 0, 0}
};

const int dustCollectionRelayPin = 11;
const int manualSwitchPin = 12; //for button activated gate, currently NOT implemented

int mVperAmp = 185; // use 100 for 20A Module and 66 for 30A Module
double ampThreshold = .20;

double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;

//button debouncing
int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup() {
  Serial.begin(9600);
  pinMode(dustCollectionRelayPin, OUTPUT);
  pwm.begin();
  pwm.setPWMFreq(60);  // Default is 1000mS

  //record baseline sensor settings
  //currently unused, but could be used for voltage comparison if need be.
  delay(1000);
  for (int i = 0; i < NUMBER_OF_TOOLS; i++) {
    pinMode(voltSensor[i], INPUT);
    voltBaseline[i] = analogRead(voltSensor[i]);
  }

}

void loop() {
  // use later for button debouncing
  reading = digitalRead(manualSwitchPin);

  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH) {
      state = LOW;
      buttonTriggered = false;
    } else {
      state = HIGH;
      buttonTriggered = true;
      time = millis();
    }
  }
  previous = reading;
  Serial.println("----------");
  //loop through tools and check
  int activeTool = 50;// a number that will never happen
  for (int i = 0; i < NUMBER_OF_TOOLS; i++) {
    if ( checkForAmperageChange(i)) {
      activeTool = i;
      exit;
    }
    if ( i != 0) {
      if (checkForAmperageChange(0)) {
        activeTool = 0;
        exit;
      }
    }
  }
  if (activeTool != 50) {
    // use activeTool for gate processing
    if (collectorIsOn == false) {
      //manage all gate positions
      for (int s = 0; s < NUMBER_OF_GATES; s++) {
        int pos = gates[activeTool][s];
        if (pos == 1) {
          openGate(s);
        } else {
          closeGate(s);
        }
      }
      delay(2500);
      //delay added to allow gates to open before vacuum system comes on
      turnOnDustCollection();
    }
  } else {
    if (collectorIsOn == true) {
      delay(vacuum_spindown);
      turnOffDustCollection();
    }
  }
}
boolean checkForAmperageChange(int which) {
  Voltage = getVPP(voltSensor[which]);
  VRMS = (Voltage / 2.0) * 0.707;
  AmpsRMS = (VRMS * 1000) / mVperAmp;
  Serial.print(tools[which] + ": ");
  Serial.print(AmpsRMS);
  Serial.println(" Amps RMS");
  if (AmpsRMS > ampThreshold) {
    return true;
  } else {
    return false;
  }
}
void turnOnDustCollection() {
  Serial.println("turnOnDustCollection");
  digitalWrite(dustCollectionRelayPin, 1);
  collectorIsOn = true;
}
void turnOffDustCollection() {
  Serial.println("turnOffDustCollection");
  digitalWrite(dustCollectionRelayPin, 0);
  collectorIsOn = false;
}

float getVPP(int sensor)
{
  float result;

  int readValue;             //value read from the sensor
  int maxValue = 0;          // store max value here
  int minValue = 1024;          // store min value here

  uint32_t start_time = millis();
  while ((millis() - start_time) < 500) //sample for 1 Sec
  {
    readValue = analogRead(sensor);
    // see if you have a new maxValue
    if (readValue > maxValue)
    {
      /*record the maximum sensor value*/
      maxValue = readValue;
    }
    if (readValue < minValue)
    {
      /*record the maximum sensor value*/
      minValue = readValue;
    }
  }

  // Subtract min from max
  result = ((maxValue - minValue) * 5.0) / 1024.0;

  return result;
}

void closeGate(uint8_t num) {
  Serial.print("closeGate ");
  Serial.println(num);
  pwm.setPWM(num, 0, gateMinMax[num][1]);
}
void openGate(uint8_t num) {
  Serial.print("openGate ");
  Serial.println(num);
  pwm.setPWM(num, 0, gateMinMax[num][0]);
  delay(100);
  pwm.setPWM(num, 0, gateMinMax[num][0] - 5);
}

Maybe I did something wrong when I changed the code. It didnt work

You will need to hold the 5V on A4 for as long as you are using the floor sweep, hence the toggle switch instead of a momentary.

"It didnt work" does not supply any information that will help us help you. Does the dust collector come on? Do any servos move?

After I uploaded the new code, I took a wire from 5V + and pinned it on A4. It didnt do anything. It didnt change the servos or turn the vacuum system on.

Can you draw a schematic please.

gladewater2016:
I havent had time to draw a diagram yet but it is fairly simple. I have 4 servos, so far, there will be more.
One for each tool. I am using a adafruit 12 bit 16 pwm servo bridge to power the servos. I have a acs712 inline on each tool to detect that the tool has been turned on. Once the tool is turned on, the servo for that tool, opens and then I have a relay to turn on the vacuum system. The relay for the vacuum gets a signal from pin 11.The signal from each acs712 is connected to A0,A1,A2,and A3.When one tool is turned on, the servo for that tool will open and close the servo from the previous tool.
I have one blast gate that does not have a servo on it. It will be for a floor sweep.
I need to add a switch to turn the vacuum system on for the floor sweep. Before the vacuum system comes on ,I need it to close all of the blast gates.
I hope this is clear. I thinking and typing at the same time
I am also using a mega 2560 instead of a uno so I will have more A0 for tool expansion

Hi,
Sure I'm stupid ...
It is far from clear to me.
Regards

vffgaston:
Hi,
Sure I'm stupid ...
It is far from clear to me.
Regards

Maybe we should try and draw a schematic from the description? I'm a day rate contractor at work, 450GBP per day. I think it'll be about 6 hours work to translate and draft it from the description... Maybe your quote will be more competitive? LOL

fall-apart-dave:
Maybe your quote will be more competitive? LOL

Much more ... :confused:

/*
   This code is for the project at
   http://www.iliketomakestuff.com/how-to-automate-a-dust-collection-system-arduino
   All of the components are list on the url above.

  This script was created by Bob Clagett for I Like To Make Stuff
  For more projects, check out iliketomakestuff.com 
//  refined with help of the Arduino forum on this thread
//  https://forum.arduino.cc/index.php?topic=548018.0
  Includes Modified version of "Measuring AC Current Using ACS712"
  http://henrysbench.capnfatz.com/henrys-bench/arduino-current-measurements/acs712-arduino-ac-current-tutorial/

  Parts of this sketch were taken from the keypad and servo sample sketches that comes with the keypad and servo libraries.

  Uses https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library
*/
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

// called this way, it uses the default address 0x40
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
// you can also call it with a different address you want
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);

// Depending on your servo make, the pulse width min and max may vary, you
// want these to be as small/large as possible without hitting the hard stop
// for max range. You'll have to tweak them as necessary to match the servos you
// have!
//servos used DS3218mg
// our servo # counter
uint8_t servoCount = 4;
uint8_t servonum = 0;

const int OPEN_ALL = 100;
const int CLOSE_ALL = 99;

boolean buttonTriggered = 0;
boolean powerDetected = 0;
boolean collectorIsOn = 0;
int vacuum_spindown = 4000;
//allow vacuum system to run after tool is turned off
const int NUMBER_OF_TOOLS = 7;
const int NUMBER_OF_GATES = 7;

String tools[NUMBER_OF_TOOLS] = {"Spindle sander", "Belt sander", "Miter saw", "Router", "Table saw", "Planer", "Sweep"};
int voltSensor[NUMBER_OF_TOOLS] = {A0, A1, A2, A3, A4, A5, A6};
long int voltBaseline[NUMBER_OF_TOOLS] = {0, 0, 0, 0, 0, 0, 0};


//Set the throw of each gate separately, if needed
int gateMinMax[NUMBER_OF_GATES][7
                               ] = {

  /*open,close*/

  {225, 150}, //Spindle sander good numbers
  {300, 155}, //Belt sander good numbers
  {265, 130}, //Miter saw good numbers
  {260, 130}, //Router good numbers
  {260, 130}, //Table saw
  {260, 130}, //Planer
  {260, 130}, //Sweep
 
 


};

//keep track of gates to be toggled ON/OFF for each tool
int gates[NUMBER_OF_TOOLS][NUMBER_OF_GATES] = {

  {1, 0, 0, 0, 0, 0, 0},
  {0, 1, 0, 0, 0, 0, 0},
  {0, 0, 1, 0, 0, 0, 0},
  {0, 0, 0, 1, 0, 0, 0},
  {0, 0, 0, 0, 1, 0, 0},
  {0, 0, 0, 0, 0, 1, 0},
  {0, 0, 0, 0, 0, 0, 1},
  
 };

const int dustCollectionRelayPin = 11;
const int manualSwitchPin = 12; //for button activated gate, currently NOT implemented

int mVperAmp = 185; // use 100 for 20A Module and 66 for 30A Module
double ampThreshold = .20;

double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;

//button debouncing
int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup() {
  Serial.begin(9600);
  pinMode(dustCollectionRelayPin, OUTPUT);
  pwm.begin();
  pwm.setPWMFreq(60);  // Default is 1000mS

  //record baseline sensor settings
  //currently unused, but could be used for voltage comparison if need be.
  delay(1000);
  for (int i = 0; i < NUMBER_OF_TOOLS; i++) {
    pinMode(voltSensor[i], INPUT);
    voltBaseline[i] = analogRead(voltSensor[i]);
  }

}

void loop() {
  // use later for button debouncing
  reading = digitalRead(manualSwitchPin);

  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH) {
      state = LOW;
      buttonTriggered = false;
    } else {
      state = HIGH;
      buttonTriggered = true;
      time = millis();
    }
  }
  previous = reading;
  Serial.println("----------");
  //loop through tools and check
  int activeTool = 50;// a number that will never happen
  for (int i = 0; i < NUMBER_OF_TOOLS; i++) {
    if ( checkForAmperageChange(i)) {
      activeTool = i;
      exit;
    }
    if ( i != 0) {
      if (checkForAmperageChange(0)) {
        activeTool = 0;
        exit;
      }
    }
  }
  if (activeTool != 50) {
    // use activeTool for gate processing
    if (collectorIsOn == false) {
      //manage all gate positions
      for (int s = 0; s < NUMBER_OF_GATES; s++) {
        int pos = gates[activeTool][s];
        if (pos == 1) {
          openGate(s);
        } else {
          closeGate(s);
        }
      }
      delay(2500);
      //delay added to allow gates to open before vacuum system comes on
      turnOnDustCollection();
    }
  } else {
    if (collectorIsOn == true) {
      delay(vacuum_spindown);
      turnOffDustCollection();
    }
  }
}
boolean checkForAmperageChange(int which) {
  Voltage = getVPP(voltSensor[which]);
  VRMS = (Voltage / 2.0) * 0.707;
  AmpsRMS = (VRMS * 1000) / mVperAmp;
  Serial.print(tools[which] + ": ");
  Serial.print(AmpsRMS);
  Serial.println(" Amps RMS");
  if (AmpsRMS > ampThreshold) {
    return true;
  } else {
    return false;
  }
}
void turnOnDustCollection() {
  Serial.println("turnOnDustCollection");
  digitalWrite(dustCollectionRelayPin, 1);
  collectorIsOn = true;
}
void turnOffDustCollection() {
  Serial.println("turnOffDustCollection");
  digitalWrite(dustCollectionRelayPin, 0);
  collectorIsOn = false;
}

float getVPP(int sensor)
{
  float result;

  int readValue;             //value read from the sensor
  int maxValue = 0;          // store max value here
  int minValue = 1024;          // store min value here

  uint32_t start_time = millis();
  while ((millis() - start_time) < 500) //sample for 1 Sec
  {
    readValue = analogRead(sensor);
    // see if you have a new maxValue
    if (readValue > maxValue)
    {
      /*record the maximum sensor value*/
      maxValue = readValue;
    }
    if (readValue < minValue)
    {
      /*record the maximum sensor value*/
      minValue = readValue;
    }
  }

  // Subtract min from max
  result = ((maxValue - minValue) * 5.0) / 1024.0;

  return result;
}

void closeGate(uint8_t num) {
  Serial.print("closeGate ");
  Serial.println(num);
  pwm.setPWM(num, 0, gateMinMax[num][1]);
}
void openGate(uint8_t num) {
  Serial.print("openGate ");
  Serial.println(num);
  pwm.setPWM(num, 0, gateMinMax[num][0]);
  delay(100);
  pwm.setPWM(num, 0, gateMinMax[num][0] - 5);
}

I finally have the code written for all 6 of the tools with servos. The 7th tool is a manual on for a floor sweep. I figured that all I had to do was run A6 to ground and put in a switch. When the switch opens the ground, it closes all of the servos and turns the vacuum system on.