#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <Adafruit_PWMServoDriver.h>
#include <TimeLib.h>
LiquidCrystal_I2C lcd(0x27,20,4);                          //Sets the LCD address to 0x27 for a 20 chars and 4 line display
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
//=========================================================
//                     Auto Dust Collection
//                         Jim Anderson
//                          Houston TX. 
//                         February 2020
// --------------------------------------------------------
// Versions:
// Ver_1.0 Feb  1, 2020 - Removed loadcell (scale)
// Ver_2.0 Mar  4, 2020 - Removed loadcell (scale)
// Ver_3.0 Mar 23, 2020 - Added Filter Spinning
// Ver_3.0 Apr  1, 2020 - Added Help Menu
// Ver_3.5 Apr 10, 2020 - Changed the Dust collector run time
// Ver_3.6 Apr 17, 2020 - Changed the Dust collector startup
//=========================================================
int gate[17][13] = {
/*   Array   J45   But  Servo  LED  Sen/Act Servo Servo Port   Clear Dust  Dust  Port 
     Number  Port  Pin  Pin    Pin  Pin     Close Open  Active Relay Light Relay Type
     0       1     2    3      4    5       6     7     8      9     10    11    12  */
    {0,      1,    0,   1,     31,  A11,    100,  255,  1,     0,    0,    0,    1},  // * 12 disk Sand - Sensor (110)      
    {1,      2,    0,   2,     32,  A12,    350,  480,  1,     0,    0,    0,    1},  // * Belt Sander  - Sensor (110)      
    {2,      3,    0,   3,     33,  A13,    225,  338,  1,     0,    0,    0,    1},  // * Drum Sander  - Sensor (110)
    {3,      4,    0,   4,     34,  A14,    150,  300,  1,     0,    0,    0,    1},  // * 18 Band-saw   - Sensor (220)
    {4,      5,    0,   5,     35,  A15,    120,  250,  1,     0,    0,    0,    1},  // * Drill-Press   - Sensor (110)
    {5,      6,    0,   6,     36,  A10,    80,   250,  1,     0,    0,    0,    1},  // 4 inch Overhead Gate - Sensor (110)
    {6,      7,    47,  6,     37,  0,      80,   250,  1,     0,    0,    0,    2},  // 4 inch Overhead Gate - button  
    {7,      8,    48,  8,     38,  0,      125,  280,  1,     0,    0,    0,    2},  // 4 inch Flex hose - button
    {8,      9,    49,  9,     39,  0,      120,  225,  1,     0,    0,    0,    4},  // CNC Mill - Run Relay 
    {9,      10,   0,   0,     0,   0,      0,    0,    0,     0,    0,    0,    5},  // Open - TBD
    {10,     11,   0,   0,     0,   0,      0,    0,    0,     0,    0,    0,    5},  // Open - TBD
    {11,     12,   0,   14,    15,  0,      0,    0,    1,     16,   17,   18,   0},  // Dust Collector
    {12,     0,    20,  0,     0,   0,      0,    0,    0,     0,    0,    0,    2},  // CNC Mill - button 
    {13,     0,    5,   0,     0,   0,      0,    0,    1,     0,    0,    0,    3},  // A Control - Button (Green)
    {14,     0,    6,   0,     0,   0,      0,    0,    1,     0,    0,    0,    3},  // B Control - Button (Blue)
    {15,     0,    7,   0,     0,   0,      0,    0,    1,     0,    0,    0,    3},  // C Control - Button (Yellow)
    {16,     0,    24,  0,     0,   0,      0,    0,    0,     0,    0,    0,    5}   // Reset + Reset - Buttons (Red)
    };  
char * toolName[] = {
  //"12345678901234567890" 
    "12 inch Disk Sander ", //  0   
    "1.5 inch Belt Sander", //  1
    "Drum Sander         ", //  2
    "18 inch Bandsaw     ", //  3
    "14 inch Drillpress  ", //  4
    "4 inch Overhead Gate", //  5
    "4 inch Flex Hose    ", //  6
    "CNC Mill Dust Gate  ", //  7
    "Dust Collector      ", //  8
    "Port 9 not used     ", //  9 
    "Port 10 not used    ", // 10
    "Port 11 not used    ", // 11
    "Dust Collector      ", // 12
    "A - Button (Green)  ", // 13
    "B - Button (Blue)   ", // 14
    "C - Button (Yellow) ", // 15
    "All Gates Closed    ", // 16
    "Powered On          ", // 17
    "Powered Off         ", // 18
    "Pressed             ", // 19
    "====================", // 20
    "4 inch Gate Open    ", // 21
    "3 inch Gate Open    ", // 22
    "System              ", // 23
    "Loading...          ", // 24
    "Ready...            ", // 25
    "Gate Open           ", // 26
    "Gate Closed         ", // 27
    "Code:               ", // 28
    "All Gates Closed    ", // 29
    "Powered On          ", // 30
    "Powered Off         ", // 31
    "Pressed             ", // 32
    "====================", // 33
    "4 inch Gate         ", // 34
    "3 inch Gate         "  // 35
    };
//=========================================================
  float nVPP;                                              //Voltage measured across resistor
  float PressLen = 0;                
  float DustLevelCount = 100000;
  float DustLevel = 1;
  float DustLevelLenth = 300;
  float vNumber[6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
  int aValue = 99;
  String Button = "";
  int StayOn = 0;
  int CleanIt = 1;
  int Running = 0;
  String comdata = "";
  int debug = 0;                                           //Debug Levels (0 = Off, 1 = Min, 2, 3, 4, or 5 = All) 
// ========================================================
void setup() {                                             //Setup 
  pwm.begin();
  pwm.setPWMFreq(60);                                      //Default is 1000mS
  Serial.begin(9600);
  while (! Serial);
  debuger("Loading Setup Code", 1);
  Display4("","System", "Start up"); 
  debuger("Code ", 1);
  for(int i=0; i<18 ;i++){
    debuger("For loop: " + String(i), 2);
    if(gate[i][8] == 1 && gate[i][12] == 0){               //Dust Collector
      pinMode(gate[i][10], OUTPUT);                        //Relays
      pinMode(gate[i][11], OUTPUT);                        //Relays
    }
   debuger("Sensors", 2);
    if(gate[i][8] == 1 && gate[i][12] == 1){ 
      pinMode(gate[i][5], INPUT);                          //Sensor
    }
  debuger("Buttons", 2);
    if(gate[i][8] == 1 && gate[i][12] == 2){ 
      pinMode(gate[i][2], INPUT_PULLUP);                   //Buttons
    }
  debuger("Control Buttons", 2);
    if(gate[i][8] == 1 && gate[i][12] == 3){ 
      pinMode(gate[i][2], INPUT_PULLUP);                   //Control Buttons
    }
  debuger("CNC Relay", 2);
    if(gate[i][8] == 1 && gate[i][12] == 4){ 
      pinMode(gate[i][2], INPUT_PULLUP);                   //CNC Relay
    }
  debuger("LEDs", 2);
    if(gate[i][8] == 1 && gate[i][4] > 0){ 
      pinMode(gate[i][4], OUTPUT);                         //LEDs
    }
  }
  pinMode(gate[11][3],  OUTPUT);                           //CNC Relay
  pinMode(gate[11][4],  OUTPUT);                           //CNC Relay
  pinMode(gate[11][9],  OUTPUT);                           //CNC Relay
  pinMode(gate[11][10], OUTPUT);                           //CNC Relay
  pinMode(gate[11][11], OUTPUT);                           //CNC Relay
  delay(5000);
  debuger("Loaded Pin Settings", 2);
  ResetVolts();
  debuger("Loaded Setup Code", 1);
  Serial.println("Type [Menu] to show Serial menu"); 
}
//=========================================================
void debuger(String words, int Lev){
  if(Lev <= debug){
      Serial.println(words)  ; 
  }
}
//=========================================================
void loop(){                                               //Looping 
 if(Serial.available()>0)    // check if data has been sent from the computer
  {  
    Serial.println("available");
    comdata = "";
    while (Serial.available() > 0)  
    {        
      comdata += char(Serial.read());
      delay(2);
    }
    comdata = comdata.substring(0,comdata.length() -1);
    ActionMenu(comdata);
  }

  PressLen = 0; 
  if (StayOn == 0){                                        //Sensor Sweep
    debuger("***** Sensor Sweep *******", 1)  ; 
    Button = "";
    aValue = 13;       
    Display4("--------------------", "Sweeping", "Sensors");  
    debuger("aValue = " + String(aValue), 4);   
    for(int i=0; i<18; i++){                               //Sweeping Sensors
      debuger("aValue = " + String(aValue), 4);   
      if(gate[i][8] == 1 && gate[i][12] == 1){             //Must be a Sensors
        debuger("aValue = " + String(aValue), 4);   
        Running = 1;
        nVPP = getVPP(gate[i][5]);
        aValue = i;
        debuger("Sensor aValue: " + String(aValue), 4);
        debuger("nVPP: " + String(nVPP), 4);
        while(nVPP > vNumber[aValue]){
          Dust_On();                                       //Dust Collector On
          debuger("aValue = " + String(aValue), 4);   
          Ogate();                                         //Gate Open
          LED_On(aValue);                                  //LED On
          Running = 0;
          DisplaySencor(aValue);
          debuger("nVPP: " + String(nVPP), 4);
          Button = String(toolName[aValue]);               //Tool Name Running
          nVPP = getVPP(gate[aValue][5]);
          debuger("Running: " + String(aValue), 4);
        }  
        if (Running == 0){          
          debuger("Turn All Off", 4);
          Dust_Off();                                      //Dust Collector Off
          debuger("aValue = " + String(aValue), 4);   
          LED_Off();                                       //LED Off          
          Cgate();                                         //Gate Close 
        }
      }
    }
  }
  //"***** CNC Relay Sweep *******"
  debuger("***** CNC Relay Sweep *******", 1)  ; 
  debuger("aValue = " + String(aValue), 4);   
  for(int i=0; i<18; i++){                                 //CNC Relay
    debuger("aValue = " + String(aValue), 4);   
    if(gate[i][8] == 1 && gate[i][12] == 4){               //Must be a CNC Relay
      debuger("aValue = " + String(aValue), 4);   
      aValue = i;  
      debuger("aValue = " + String(aValue), 4);   
      debuger("***** CNC Relay *******", 2)  ;
      Running = 0;       
      while (digitalRead(gate[aValue][2]) == LOW){
        debuger("aValue = " + String(aValue), 4);   
        Button = String(toolName[aValue]);                 //Tool Name Running
        debuger("***** CNC Running *******", 2)  ;
        Display4(Button, Button, Button);
        if (Running == 0){
          Ogate();                                         //Gate Open
          debuger("aValue = " + String(aValue), 4);   
          LED_On(aValue);                                  //LED On  
          Dust_On();                                       //Dust Collector On
          Running = 2;
        }
      }      
      if(Running == 2){
        LED_Off();                                         //LED Off
        debuger("aValue = " + String(aValue), 4);   
        Dust_Off();                                        //Dust Collector Off      
        Cgate();                                           //Gate Close      
        Running = 0;
     }
    }
  }
  //***** Button Sweep *******  
  debuger("***** Button Sweep *******", 1); 
  Display4("--------------------", "Sweeping", "Buttons");
  debuger("aValue = " + String(aValue), 4);   
  for(int i=0; i<18; i++){                                 //Sweeping Buttons
    debuger("aValue = " + String(aValue), 4);   
    if(gate[i][8] == 1 && gate[i][12] == 2) { 
      aValue = i;
      debuger("aValue = " + String(aValue), 4);   
      debuger("Sweeping Button " + String(aValue), 2); 
      while (digitalRead(gate[aValue][2]) == LOW){
        PressLen = PressLen + 101;
        debuger("While " + String(PressLen), 2);
        debuger("aValue = " + String(aValue), 4);   
        delay(500);   
        if (PressLen >= 500){
          LEDs_On();                                       //All LED's On
        }else{
          LED_Off();
        }
      }  
      debuger("Button " + String(aValue) + ": ", 2); 
      debuger("Press Len: " + String(PressLen), 2);
      debuger("aValue = " + String(aValue), 4);   
      if (PressLen >= 500){
        debuger("aValue = " + String(aValue), 4);   
        if (StayOn == 0){
          debuger("aValue = " + String(aValue), 4);   
          StayOn = 1;            
          debuger("Stay On", 2);
          Ogate();                                         //Gate Open
          Dust_On();                                       //Dust Collector On
          PressLen = 0;
        }
      }
      if(PressLen > 1 && PressLen < 500){
        LED_Off();                                         //LED Off
        debuger("aValue = " + String(aValue), 4);   
        StayOn = 0;
        Dust_Off();                                        //Dust Collector Off  
        debuger("Stay Off", 2);          
        Cgate();                                           //Gate Close          
      }        
    }
  } 

  //***** Control Button Sweep *******
  debuger("***** Control Button Sweep *******", 1)  ; 
  for(int i=0; i<18; i++){                                 //Control Button
    debuger("aValue = " + String(aValue), 4);   
    if(gate[i][8] == 1 && gate[i][12] == 3){        
      aValue = i;
      debuger("aValue = " + String(aValue), 4);   
      if(digitalRead(gate[aValue][2]) == LOW){   
        debuger("aValue = " + String(aValue), 4);   
        debuger("Control Button", 1);
        debuger(String(aValue), 1);  
        ResetVolts();        
        Button = toolName[aValue];
        if (aValue == 13){                                 //Button A pressed
          debuger("aValue = " + String(aValue), 4);   
            debuger("Button A Value 13", 1);
            debuger("aValue = " + String(aValue), 4);   
            LEDs_On();                                     //All LED's On
            for(int x=0; x<18; x++){
              debuger("aValue = " + String(aValue), 4);   
              if(gate[x][12] == 1) {  
                 debuger(String(x), 1);
                 debuger("aValue = " + String(aValue), 4);   
                 nVPP = getVPP(gate[x][5]);
                 debuger(String(nVPP), 1);
                 aValue = 20;
                 Display4("- Sensor Value -", "Sensor Value: " + String(x), String(nVPP));
                 delay(2000);
              }
            }
            debuger("aValue = " + String(aValue), 4);   
            LED_Off();                                   //LED Off
            debuger("aValue = " + String(aValue), 4);   
          }
          if (aValue == 14){                             //Button B pressed
            debuger("Button B Value 14", 1);
            debuger("aValue = " + String(aValue), 4);   
            LEDs_On();                                   //All LED's On 
            Ogates();
            delay(5000);
            Cgate();
            delay(5000);
        for(int x=0; x<18; x++){
              debuger("aValue = " + String(aValue), 4);   
              if(gate[x][6] > 1) {  
                 debuger(String(x), 1);
                 debuger("aValue = " + String(aValue), 4);   
                 aValue = 20;
                 Display4("-Gate Value-", String(toolName[x]), "Gate Cycling");
                 debuger("aValue = " + String(aValue), 4);   
                 debuger("Gate Cycling the " + String(toolName[x]) ,1);
                 pwm.setPWM(gate[x][3], 0, gate[x][7]);    //Gate Open
                 delay(3000);
                 pwm.setPWM(gate[x][3], 0, gate[x][6]);    //Gate Closed
                 delay(3000);
              }
            }
            LED_Off();                                     //LED Off
            debuger("aValue = " + String(aValue), 4);   
          }
        if (aValue == 15){                                 //Button C pressed
          debuger("Button C Value 15", 1);
          LEDs_On();                                       //All LED's On   
          debuger("aValue = " + String(aValue), 4);          
          Ogates();                                        //Gates Open 
          Display4("-Gate Value-", "Dust Collector", "Run 15 Seconds");
          Dust_On();                                       //Dust Collector On    
          delay(15000);                                    //15 Seconds
          aValue = 20;
          Display4("-Gate Value-", "Dust Collector", "Off");
          debuger("aValue = " + String(aValue), 4);   
          Dust_Off();                                      //Dust Collector Off   
          delay(5000);                                     //5 Seconds
          Cgate();                                         //Gate Close    
          LED_Off();                                       //LED Off      
        }
      }
    }
  }
  debuger("aValue = " + String(aValue), 4);   
  if (CleanIt == 1){
    Ogates();                                              //Open All Gates
    DustClean_On() ;                                       //Dust Collector Clean Filter
    debuger("Dust Collector Clean Filter", 1);
    Cgate();                                               //Close All Gates
    CleanIt = 0;
  }
}
//=========================================================
void LED_Off(){                                            //All LED Off
  int X = 0;
  debuger("--- All LED's Off ---", 3);
  for(int i=0; i<18; i++){
    X = i;
    digitalWrite(gate[X][4], LOW); 
    debuger("LED Off: " + String(gate[X][4]), 4);
  }
  digitalWrite(gate[11][10], LOW);                         //Sets the Dust Collector LED Off
}
//=========================================================
void LEDs_On(){                                            //All LED's On
  debuger("--- All LED's On ---", 3);                   
  for(int i=0; i<10; i++){
     if(gate[i][4] > 1){ 
       digitalWrite(gate[i][4], HIGH);
       debuger("LED On: " + String(gate[i][4]), 4);
     }
  }
   digitalWrite(gate[11][10], HIGH);                       //Sets the Dust Collector LED On
}
//=========================================================
void ResetVolts(){                                         //Sets the Sensor Array
    debuger("--- Sets the Sensor Array ---", 3);
  for(int a=0; a<18; a++){
    if(gate[a][8] == 1 && gate[a][12] == 1){ 
      vNumber[a] = getVPP(gate[a][5]) + 0.2;
      debuger("Amp Value: " + String(vNumber[a]), 4);
    }
  }
}
//=========================================================
void Ogates(){                                             //Open All Gates 
  debuger("--- Open All Gates ---", 2);
  delay(2000);                                             //2 Seconds
  for(int a=0; a<18; a++){
    if(gate[a][6] > 1){ 
      pwm.setPWM(gate[a][3], 0, gate[a][7]);      
      debuger("Open Gate: " + String(gate[a][3])+ String(gate[a][7]), 4);
       delay(500);                                         //0.5 Seconds
    }
  }
}
//=========================================================
void Ogate(){                                              //Open a Gate
  debuger("--- Open a Gate ---", 2);
  debuger("Gate Open: " + String(aValue), 4);
  pwm.setPWM(gate[aValue][3], 0, gate[aValue][7]);         //Gate Open
  delay(500);
  pwm.setPWM(gate[aValue][3], 0, gate[aValue][7]);         //Gate Open
  debuger("Open Gate: " + String(gate[aValue][3])+ String(gate[aValue][7]), 4);
  delay(500);
}
//=========================================================
void Opengate(int GateNo){                                 //Open a Gate
  debuger("--- Open a Gate ---", 2);
  debuger("Gate Open: " + String(GateNo), 4);
  pwm.setPWM(gate[GateNo][3], 0, gate[GateNo][7]);         //Gate Open
  delay(5000);
  pwm.setPWM(gate[GateNo][3], 0, gate[GateNo][7]);         //Gate Open
  debuger("Open Gate: " + String(gate[GateNo][3])+ String(gate[GateNo][7]), 4);
  delay(500);
}
//=========================================================
void Cgate(){                                              //Close All Gates 
  debuger("--- Closing Gates ---", 2);
  delay(2000);                                             //2 Seconds
  for(int a=0; a<18; a++){
    if((gate[a][3] > 0) && (gate[a][3] < 17)){ 
      debuger("Gate Closing: " + String(gate[a][6]), 4);
      debuger("Gate Port: " + String(gate[a][3]), 4);
      pwm.setPWM(gate[a][3], 0, gate[a][6]);
        delay(500);                                        //0.5 seconds
    }
  }
  delay(2000);                                             //2 seconds
}
//=========================================================
void LED_On(int x){                                        //LED On
  debuger("LED On:" + String(x) + " ", 2);                 //Log
  digitalWrite(gate[x][4], HIGH);                          //Pin setting
  delay(250);                                              //0.25 seconds
}
//=========================================================
void Dust_On(){                                            //Dust Collector On
  debuger("Dust Collector On", 2);
  //digitalWrite(gate[11][11],  HIGH);                       //Lights On
  //digitalWrite(gate[11][4],  HIGH);                        //Lights On
  //digitalWrite(gate[11][9],  HIGH);                        //Lights On
  digitalWrite(gate[11][10], HIGH);                          //Lights On
  Display4("--------------------","Dust Collector", "On ");
  digitalWrite(gate[11][3], HIGH);                           //Sets the Dust Collector On
}
//=========================================================
void Dust_Off(){                                           //Dust Collector Off  
  debuger("== Dust Collector Off ==", 2);
  debuger("Dust Collector Off = 10 seconds", 3)  ;
  Display4("--------------------", "Dust Collector", "Off in 10 seconds");
  digitalWrite(gate[11][4],  LOW);                         //Light Off
  delay(1500);                                             //Time to clear the line 
  debuger("Dust Collector Off = 5 seconds", 3)  ;
  digitalWrite(gate[11][1],  LOW);                         //Light Off
  Display4("--------------------", "Dust Collector", "Off in 5 seconds");
  delay(1500);                                             //Time to clear the line 
  debuger("Dust Collector Off", 2);
  digitalWrite(gate[11][9],  LOW);                         //Light Off
  Display4("--------------------", "Dust Collector", "Off ");
  digitalWrite(gate[11][10], LOW);                         //Light Off
  digitalWrite(gate[11][3], LOW);                          //Sets the Dust Collector Off
}
//=========================================================
void DustClean_On(){                                       //Dust Collector Light On  
  debuger("Dust Collector Cleaning On", 2) ; 
  Display4("--------------------", "Dust Collector", "Filter Cleaning");
  digitalWrite(gate[11][4],  HIGH);                        //Lights On
  digitalWrite(gate[11][9],  HIGH);                        //Lights On
  digitalWrite(gate[11][10], HIGH);                        //Lights On
  digitalWrite(gate[11][11], HIGH);                        //Turn On Filter Spin
  delay(20000);                                            //Time to clear the line 
  digitalWrite(gate[11][11],  LOW);                        //Light Off
  delay(20000);                                            //Time to clear the line 
  digitalWrite(gate[11][9],   LOW);                        //Light Off
  delay(20000);                                            //Time to clear the line 
  digitalWrite(gate[11][10],  LOW);                        //Turn Off Blue Light
  delay(20000); 
  digitalWrite(gate[11][4],   LOW);                        //Turn Off Spinner and Blue Light
}
//=========================================================
void DisplaySencor(int X){                                 //Displays the sensor informations 
  debuger("Displays the sensor informations", 3);
  uint32_t now_time = millis();
  lcd.init();                                              //Print a message to the LCD.
  lcd.backlight();
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("-- Voltage Sensor --");
  lcd.setCursor(0,1);
  String hr = String(hour());
  String mn = String(minute());
  String sc = String(second());
  if (hr.length()==1){hr = "0" + hr;}  
  if (mn.length()==1){mn = "0" + mn;}  
  if (sc.length()==1){sc = "0" + sc;}
  lcd.print("Run Time: " + hr + ":" + mn + ":" + sc);
  debuger("Run Time: " + hr + ":" + mn + ":" + sc, 4);
  lcd.setCursor(0,2);
  lcd.print(toolName[X]);
  lcd.setCursor(0,3);
  lcd.print(toolName[26]);  
}
//=========================================================
void Display4(String Word1, String Word2, String Word3) {  //Post info to LCD Display
  uint32_t now_time = millis();
  lcd.init();                                              //Print a message to the LCD.
  debuger("== Display4 ==", 2);
  lcd.backlight();
  lcd.setCursor(0,0);  
  DustLevelCount = DustLevelCount + 1;
  lcd.print("Dust Level = " + String(DustLevel));
  lcd.setCursor(0,1);
  String hr = String(hour());
  String mn = String(minute());
  String sc = String(second());
  if (hr.length()==1){hr = "0" + hr;}
  if (mn.length()==1){mn = "0" + mn;}
  if (sc.length()==1){sc = "0" + sc;}
  lcd.print("Run Time: " + hr + ":" + mn + ":" + sc);
  lcd.setCursor(0,2);
  lcd.print(Word2);
  lcd.setCursor(0,3);
  lcd.print(Word3);  
}
//=========================================================
float getVPP(int Pin){                                    //Get Amp Level Reader
  float result;
  int readValue;                                          //Value read from the sensor
  int maxValue = 0;                                       //Store max value here
  debuger("== Get Amp Level Reader ==", 3); 
  uint32_t start_time = millis();
  while((millis()-start_time) < 50){                      //Sample for 1 Sec
    readValue = analogRead(Pin);                          //See if you have a new maxValue
    if (readValue > maxValue){           
      maxValue = readValue;                               //Record the maximum sensor value
    }
  }     
  result = (maxValue * 5.0)/1024.0;                       //Convert the digital data to a voltage
  return result;
}
//=========================================================
void ActionMenu(String InPut){
  InPut.toLowerCase();
  if (InPut.substring(0, 4) == "menu"){
    Serial.println("==========================================="); 
    Serial.println("|============= User InPut Menu ============"); 
    Serial.println("| Menu = Shows this menu"); 
    Serial.println("| DustSpin = Spin the Filter Cleaner Logic"); 
    Serial.println("| DustSpinOn = Turn On Filer Cleaner"); 
    Serial.println("| DustSpinOff = Turn Off Filer Cleaner"); 
    Serial.println("| Gate# = (Gate0-8) Open and Close Gate No. for 5 seconds"); 
    Serial.println("| Button# = (ButtonA, B, or C) do button"); 
    Serial.println("| DustOn = Spin the Filter Cleaner"); 
    Serial.println("| DustOff = Spin the Filter Cleaner"); 
    Serial.println("| SetGate#_pos = (Gate0-8_110-600) Moves Servo to postion 220");
    Serial.println("| CloseAll = Close all gates"); 
    Serial.println("| Debug# = (0-5) Sets the debug display level"); 
    Serial.println("|=========================================="); 
  }
  else if (InPut == "dustspin"){
    Serial.println("==========================================="); 
    Serial.println("| Spin the Filter Cleaner"); 
    Serial.println("==========================================="); 
    DustClean_On();
  }
  else if (InPut.substring(0, 4) == "gate"){
    String GateNumA =  InPut.substring(4);
    int GateNum =  GateNumA.toInt();
    Serial.println("==========================================="); 
    Serial.println("| Cycling Gate " + String(GateNum)); 
    Serial.println("==========================================="); 
    Opengate(GateNum);
    Cgate();
  }
  else if (InPut.substring(0, 7) == "setgate"){
    String GateNumB =  InPut.substring(7, 8);
    String GatePosB =  InPut.substring(9);
    int GateNumber =  GateNumB.toInt();
    int GatePosition =  GatePosB.toInt();
    Serial.println("==========================================="); 
    Serial.println("| Set Gate " + String(GateNumber) + " to: " + String(GatePosition)); 
    Serial.println("==========================================="); 
    pwm.setPWM(gate[GateNumber][3], 0, GatePosition);            //Gate Open
    delay(5000);
    pwm.setPWM(gate[GateNumber][3], 0, GatePosition);            //Gate Open
  }
  else if (InPut == "closeall"){
    Serial.println("==========================================="); 
    Serial.println("| Close All Gates "); 
    Serial.println("===========================================");     
    Cgate();                                                     //Close All Gates
  }
  else if (InPut == "duston"){
    Serial.println("==========================================="); 
    Serial.println("| Turn On the Dust Collector "); 
    Serial.println("===========================================");     
    digitalWrite(gate[11][3],  HIGH);                            //Dust Collector On
  }
  else if (InPut == "dustoff"){
    Serial.println("==========================================="); 
    Serial.println("| Turn Off the Dust Collector "); 
    Serial.println("===========================================");     
    digitalWrite(gate[11][3],  LOW);                             //Dust Collector Off
  }
  else if (InPut == "dustspinon"){
    Serial.println("==========================================="); 
    Serial.println("| Turn On the Filter Cleaner "); 
    Serial.println("===========================================");     
    digitalWrite(gate[11][4],  HIGH);                            //Filter Cleaner On 
  }
  else if (InPut == "dustspinoff"){
    Serial.println("==========================================="); 
    Serial.println("| Turn Off the Filter Cleaner "); 
    Serial.println("===========================================");     
    digitalWrite(gate[11][4],  LOW);                             //Filter Cleaner Off    
  }  
  else if (InPut.substring(0, 5) == "debug"){
    String DStr =  InPut.substring(5, 6);
    int DNum =  DStr.toInt();
    Serial.println("==========================================="); 
    Serial.println("| Debug display level = " + DStr) ; 
    Serial.println("==========================================="); 
    debug = DNum;                                                //Set debug display Level   
  }
}
//=========================================================
