Advertisement
macca-nz

Workshop_Auto_Vac_System

Feb 3rd, 2021
77
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * This code is for the project at
  3.  * http://www.iliketomakestuff.com/how-to-automate-a-dust-collection-system-arduino
  4.  * All of the components are list on the url above.
  5.  *
  6. This script was created by Bob Clagett for I Like To Make Stuff
  7. For more projects, check out iliketomakestuff.com
  8. Includes Modified version of "Measuring AC Current Using ACS712"
  9. http://henrysbench.capnfatz.com/henrys-bench/arduino-current-measurements/acs712-arduino-ac-current-tutorial/
  10. Parts of this sketch were taken from the keypad and servo sample sketches that comes with the keypad and servo libraries.
  11. Uses https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library
  12. */
  13. #include <Wire.h>
  14. #include <Adafruit_PWMServoDriver.h>
  15.  
  16. // called this way, it uses the default address 0x40
  17. Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
  18. // you can also call it with a different address you want
  19. //Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);
  20.  
  21. // Depending on your servo make, the pulse width min and max may vary, you
  22. // want these to be as small/large as possible without hitting the hard stop
  23. // for max range. You'll have to tweak them as necessary to match the servos you
  24. // have!
  25.  
  26. // our servo # counter
  27. uint8_t servoCount = 6;
  28. uint8_t servonum = 0;
  29.  
  30. const int OPEN_ALL = 100;
  31. const int CLOSE_ALL = 99;
  32.  
  33. boolean buttonTriggered = 0;
  34. boolean powerDetected = 0;
  35. boolean collectorIsOn = 0;
  36. int DC_spindown = 3000;
  37.  
  38. const int NUMBER_OF_TOOLS = 3;
  39. const int NUMBER_OF_GATES = 6;
  40.  
  41. String tools[NUMBER_OF_TOOLS] = {"Miter Saw","Table Saw","Band Saw"}; //, "Floor Sweep"
  42. int voltSensor[NUMBER_OF_TOOLS] = {A1,A2,A3};
  43. long int voltBaseline[NUMBER_OF_TOOLS] = {0,0,0};
  44.  
  45. //DC right, Y, miter, bandsaw, saw Y, tablesaw, floor sweep
  46. //Set the throw of each gate separately, if needed
  47. int gateMinMax[NUMBER_OF_GATES][2] = {
  48.   /*open, close*/
  49.   {250,415},//DC right
  50.   {230,405},//Y
  51.   {230,405},//miter
  52.   {285,425},//bandsaw
  53.   {250,405},//saw y
  54.   {250,415},//floor sweep
  55. };
  56.  
  57. //keep track of gates to be toggled ON/OFF for each tool
  58. int gates[NUMBER_OF_TOOLS][NUMBER_OF_GATES] = {
  59.   {1,0,1,0,0,0},
  60.   {1,1,0,0,1,1},
  61.   {1,1,0,1,0,0},
  62. };
  63.  
  64. const int dustCollectionRelayPin = 11;
  65. const int manualSwitchPin = 12; //for button activated gate, currently NOT implemented
  66.  
  67. int mVperAmp = 185; // use 100 for 20A Module and 66 for 30A Module
  68. double ampThreshold = .20;
  69.  
  70. double Voltage = 0;
  71. double VRMS = 0;
  72. double AmpsRMS = 0;
  73.  
  74. //button debouncing
  75. int state = HIGH;      // the current state of the output pin
  76. int reading;           // the current reading from the input pin
  77. int previous = LOW;    // the previous reading from the input pin
  78.  
  79. // the follow variables are long's because the time, measured in miliseconds,
  80. // will quickly become a bigger number than can be stored in an int.
  81. long time = 0;         // the last time the output pin was toggled
  82. long debounce = 200;   // the debounce time, increase if the output flickers
  83.  
  84. void setup(){
  85.   Serial.begin(9600);
  86.   pinMode(dustCollectionRelayPin,OUTPUT);
  87.   pwm.begin();
  88.   pwm.setPWMFreq(60);  // Default is 1000mS
  89.  
  90.  //record baseline sensor settings
  91.  //currently unused, but could be used for voltage comparison if need be.
  92.   delay(1000);
  93.   for(int i=0;i<NUMBER_OF_TOOLS;i++){
  94.     pinMode(voltSensor[i],INPUT);
  95.     voltBaseline[i] = analogRead(voltSensor[i]);
  96.   }
  97.  
  98. }
  99.  
  100. void loop(){
  101.   // use later for button debouncing
  102.   reading = digitalRead(manualSwitchPin);
  103.  
  104.   if (reading == HIGH && previous == LOW && millis() - time > debounce) {
  105.     if (state == HIGH){
  106.       state = LOW;
  107.      buttonTriggered = false;
  108.     } else{
  109.       state = HIGH;
  110.      buttonTriggered = true;
  111.     time = millis();    
  112.     }
  113.   }
  114.   previous = reading;
  115.    Serial.println("----------");
  116.    //loop through tools and check
  117.    int activeTool = 50;// a number that will never happen
  118.    for(int i=0;i<NUMBER_OF_TOOLS;i++){
  119.       if( checkForAmperageChange(i)){
  120.         activeTool = i;
  121.         exit;
  122.       }
  123.       if( i!=0){
  124.         if(checkForAmperageChange(0)){
  125.           activeTool = 0;
  126.           exit;
  127.         }
  128.       }
  129.    }
  130.   if(activeTool != 50){
  131.     // use activeTool for gate processing
  132.     if(collectorIsOn == false){
  133.       //manage all gate positions
  134.       for(int s=0;s<NUMBER_OF_GATES;s++){
  135.         int pos = gates[activeTool][s];
  136.         if(pos == 1){
  137.           openGate(s);    
  138.         } else {
  139.           closeGate(s);
  140.         }
  141.       }
  142.       turnOnDustCollection();
  143.     }
  144.   } else{
  145.     if(collectorIsOn == true){
  146.         delay(DC_spindown);
  147.       turnOffDustCollection();  
  148.     }
  149.   }
  150. }
  151. boolean checkForAmperageChange(int which){
  152.    Voltage = getVPP(voltSensor[which]);
  153.    VRMS = (Voltage/2.0) *0.707;
  154.    AmpsRMS = (VRMS * 1000)/mVperAmp;
  155.    Serial.print(tools[which]+": ");
  156.    Serial.print(AmpsRMS);
  157.    Serial.println(" Amps RMS");
  158.    if(AmpsRMS>ampThreshold){
  159.     return true;
  160.    }else{
  161.     return false;
  162.    }
  163. }
  164. void turnOnDustCollection(){
  165.   Serial.println("turnOnDustCollection");
  166.   digitalWrite(dustCollectionRelayPin,1);
  167.   collectorIsOn = true;
  168. }
  169. void turnOffDustCollection(){
  170.   Serial.println("turnOffDustCollection");
  171.   digitalWrite(dustCollectionRelayPin,0);
  172.   collectorIsOn = false;
  173. }
  174.  
  175. float getVPP(int sensor)
  176. {
  177.   float result;
  178.  
  179.   int readValue;             //value read from the sensor
  180.   int maxValue = 0;          // store max value here
  181.   int minValue = 1024;          // store min value here
  182.  
  183.    uint32_t start_time = millis();
  184.    while((millis()-start_time) < 500) //sample for 1 Sec
  185.    {
  186.        readValue = analogRead(sensor);
  187.        // see if you have a new maxValue
  188.        if (readValue > maxValue)
  189.        {
  190.            /*record the maximum sensor value*/
  191.            maxValue = readValue;
  192.        }
  193.        if (readValue < minValue)
  194.        {
  195.            /*record the maximum sensor value*/
  196.            minValue = readValue;
  197.        }
  198.    }
  199.    
  200.    // Subtract min from max
  201.    result = ((maxValue - minValue) * 5.0)/1024.0;
  202.      
  203.    return result;
  204.  }
  205.  
  206. void closeGate(uint8_t num){
  207.   Serial.print("closeGate ");
  208.   Serial.println(num);
  209.   pwm.setPWM(num, 0, gateMinMax[num][1]);
  210. }
  211. void openGate(uint8_t num){
  212.   Serial.print("openGate ");
  213.   Serial.println(num);
  214.     pwm.setPWM(num, 0, gateMinMax[num][0]);
  215.     delay(100);
  216.     pwm.setPWM(num, 0, gateMinMax[num][0]-5);
  217. }
Advertisement
RAW Paste Data Copied
Advertisement