// 18650 battery capacity tester 
// gives a serial out of the capacity of up to 16 batteries via a RS232 serial port

// port settings are 115200/8/n/1

// video of build and circuit drawing @
// https://youtu.be/w0O0Es9belg
// http://martinelectronics.co.uk/wp/?p=136

#include <Wire.h>  // Comes with Arduino IDE
// Get the LCD I2C Library here: 
// https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads
// Move any other LCD libraries to another folder or delete them
// See Library "Docs" folder for possible commands etc.
#include <LiquidCrystal_I2C.h>
// set the LCD address to 0x27 for a 20 chars 4 line display
// you may need to run an I2C address program to find the address for your LCD
// Set the pins on the I2C chip used for LCD connections:
//                    addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
//LiquidCrystal_I2C lcd(0x27, 4, 5, 6, 0, 1, 2, 3, 7, NEGATIVE);  // Set the LCD I2C address
//LiquidCrystal_I2C lcd(0x27, 4, 5, 6, 0, 1, 2, 3, 7, POSITIVE);  // Set the LCD I2C address
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Addr, En, Rw, Rs, d4, d5, d6, d7, backlighpin, polarity


#include <TimerOne.h>

//Setup port pins
  
  int FetGate0 = 2;
  int FetGate1 = 3;
  int FetGate2 = 4;
  int FetGate3 = 5;
  
  int FetGate4 = 6;
  int FetGate5 = 7;
  int FetGate6 = 8;
  int FetGate7 = 9;

  int FetGate8 = 10;
  int FetGate9 = 11;
  int FetGate10 = 12;
  int FetGate11 = 13;

  int FetGate12 = 14;
  int FetGate13 = 15;
  int FetGate14 = 16;
  int FetGate15 = 17;


  int Switch0 = 30;         // switch for menu up (connect to ground to function)
  int Switch1 = 32;         // switch for menu down (connect to ground to function)
//-------------------------
  float Voltage0 = 0;       //Memory location to store current battery voltage
  float Voltage1 = 0;
  float Voltage2 = 0;
  float Voltage3 = 0;

  float Voltage4 = 0;
  float Voltage5 = 0;
  float Voltage6 = 0;
  float Voltage7 = 0;

  float Voltage8 = 0;
  float Voltage9 = 0;
  float Voltage10 = 0;
  float Voltage11 = 0;

  float Voltage12 = 0;
  float Voltage13 = 0;
  float Voltage14 = 0;
  float Voltage15 = 0;
//-------------------------
  float FirstVoltage0 = 0;      //Memory location to store the first recorded voltage of the cell when first plugged in.
  float FirstVoltage1 = 0;
  float FirstVoltage2 = 0;
  float FirstVoltage3 = 0;

  float FirstVoltage4 = 0;
  float FirstVoltage5 = 0;
  float FirstVoltage6 = 0;
  float FirstVoltage7 = 0;

  float FirstVoltage8 = 0;
  float FirstVoltage9 = 0;
  float FirstVoltage10 = 0;
  float FirstVoltage11 = 0;

  float FirstVoltage12 = 0;
  float FirstVoltage13 = 0;
  float FirstVoltage14 = 0;
  float FirstVoltage15 = 0;

  byte enableFirstRead0 = 0;   // Memory location for recording if we have taken first voltage recording, saved in bits
  byte enableFirstRead1 = 0;
//-------------------------

  float Current0 = 0;
  float Current1 = 0;
  float Current2 = 0;
  float Current3 = 0;

  float Current4 = 0;
  float Current5 = 0;
  float Current6 = 0;
  float Current7 = 0;

  float Current8 = 0;
  float Current9 = 0;
  float Current10 = 0;
  float Current11 = 0;

  float Current12 = 0;
  float Current13 = 0;
  float Current14 = 0;
  float Current15 = 0;
//-------------------------
  float Capacity0 = 0;
  float Capacity1 = 0;
  float Capacity2 = 0;
  float Capacity3 = 0;

  float Capacity4 = 0;
  float Capacity5 = 0;
  float Capacity6 = 0;
  float Capacity7 = 0;

  float Capacity8 = 0;
  float Capacity9 = 0;
  float Capacity10 = 0;
  float Capacity11 = 0;

  float Capacity12 = 0;
  float Capacity13 = 0;
  float Capacity14 = 0;
  float Capacity15 = 0;
//-------------------------
#define resistor 4     // sets the ohms of the load resistor
#define cutOff 3.0      // sets the cut off point to prevent over discharge
//-------------------------
  int Batt0 = 0;      //A0
  int Batt1 = 1;      //A1
  int Batt2 = 2;      //A2
  int Batt3 = 3;      //A3

  int Batt4 = 4;      //A4
  int Batt5 = 5;      //A5
  int Batt6 = 6;      //A6
  int Batt7 = 7;      //A7

  int Batt8 = 8;      //A8
  int Batt9 = 9;      //A9
  int Batt10 = 10;      //A10
  int Batt11 = 11;      //A11

  int Batt12 = 12;      //A12
  int Batt13 = 13;      //A13
  int Batt14 = 14;      //A14
  int Batt15 = 15;      //A15
//-------------------------
  float Vin = 4.98;       //input voltage to arduino board, measure with multi meter and change the value here - important to get good results
  float adcBits = 1024;   //number of bits in ADC, arduinos are 10 bit
  float vFactor = 0;          // memory for voltage calculation 
//-------------------------  
  float Time0=0;
  float Time1=0;
  float Time2=0;
  float Time3=0;

  float Time4=0;
  float Time5=0;
  float Time6=0;
  float Time7=0;

  float Time8=0;
  float Time9=0;
  float Time10=0;
  float Time11=0;

  float Time12=0;
  float Time13=0;
  float Time14=0;
  float Time15=0;
//-------------------------
  
  int SerialCount = 9;
  int SerialCheck = 10;

  byte lcdSwitch = 0;
//===========================================  
void setup() {
  // put your setup code here, to run once:
//Setup serial comms
Serial.begin (115200);
//------------------------------------------------------------------------------------------------------
//Send message out via LCD and Serial port about what we are
   vFactor = Vin/adcBits;   //work out the voltage factor
   Serial.println ("StartUp......");
   Serial.println ("18650 Capacity Tester - by Alex R Martin @ http://ev-north.co.uk");
   Serial.print ("Voltage Factor = ");
    Serial.println (vFactor, 4);
    Serial.print ("Load Resistor Value = ");
    Serial.println (resistor);
    Serial.println ("Program start...... ");

lcd.begin(20,4);         // initialize the lcd for 20 chars 4 lines

// NOTE: Cursor Position: CHAR, LINE) start at 0  
  lcd.setCursor(3,0); //Start at character 4 on line 0
  lcd.print("18650 Capacity");
  delay(500);
  lcd.setCursor(7,1);
  lcd.print("Tester");
  delay(500);  
  lcd.setCursor(3,2);
  lcd.print("EV-North.co.uk");
  delay(1000);
  lcd.clear();

  lcd.setCursor(0,0);
  lcd.print ("StartUp......");
  lcd.setCursor(0,1);
  lcd.print ("V Factor = ");
  lcd.print (vFactor, 4);
  lcd.setCursor(0,2);
  lcd.print ("Load Value = ");
  lcd.print (resistor);
  lcd.print (" Ohm");
  lcd.setCursor(0,7);

  lcd.print ("Program start...... ");
  delay(4000);
  lcd.clear();
  
// Wait and then clear the screen ready for testing
//------------------------------------------------------------------------------------------------------
//Setup I/O ports

  pinMode(FetGate0, OUTPUT);
  pinMode(FetGate1, OUTPUT);
  pinMode(FetGate2, OUTPUT);
  pinMode(FetGate3, OUTPUT);

  pinMode(FetGate4, OUTPUT);
  pinMode(FetGate5, OUTPUT);
  pinMode(FetGate6, OUTPUT);
  pinMode(FetGate7, OUTPUT);

  pinMode(FetGate8, OUTPUT);
  pinMode(FetGate9, OUTPUT);
  pinMode(FetGate10, OUTPUT);
  pinMode(FetGate11, OUTPUT);

  pinMode(FetGate12, OUTPUT);
  pinMode(FetGate13, OUTPUT);
  pinMode(FetGate14, OUTPUT);
  pinMode(FetGate15, OUTPUT);

//Ensure all FETs are off
  digitalWrite (FetGate0, LOW);
  digitalWrite (FetGate1, LOW);
  digitalWrite (FetGate2, LOW);
  digitalWrite (FetGate3, LOW);

  digitalWrite (FetGate4, LOW);
  digitalWrite (FetGate5, LOW);
  digitalWrite (FetGate6, LOW);
  digitalWrite (FetGate7, LOW);

  digitalWrite (FetGate8, LOW);
  digitalWrite (FetGate9, LOW);
  digitalWrite (FetGate10, LOW);
  digitalWrite (FetGate11, LOW);

  digitalWrite (FetGate12, LOW);
  digitalWrite (FetGate13, LOW);
  digitalWrite (FetGate14, LOW);
  digitalWrite (FetGate15, LOW);
//------------------------------------------------------------------------------------------------------

  pinMode(Switch0, INPUT_PULLUP);
  pinMode(Switch1, INPUT_PULLUP);
//------------------------------------------------------------------------------------------------------
//Setup timer
Timer1.initialize(1000000);
Timer1.attachInterrupt(checkBatt);

}
//=========================================== 
void loop() {
  // put your main code here, to run repeatedly:

    if (digitalRead (Switch0) == LOW){
      delay (20);
        // {
        lcdSwitch++;    //increment counter
          if (lcdSwitch == 16){
            lcdSwitch = 0;    //zero counter if we have got to 15
            
          }
     
       // }
    }
do{} // waste time until switch is released.
            while (digitalRead (Switch0) == LOW);

            
    if (digitalRead (Switch1) == LOW){
      delay (20);
        // {
        lcdSwitch--;    //increment counter
          if (lcdSwitch == 255){
            lcdSwitch = 15;    //zero counter if we have got to 15
            
          }
     
       // }
    }
do{} // waste time until switch is released.
            while (digitalRead (Switch1) == LOW);
            
    lcdUpdate();     //endlessly check and update the LCD
}
//=========================================== 
void checkBatt() {
 
 SerialCount++;             //increment seconds counter
//-------------- Batt 0 ---------------------
  Voltage0 = analogRead(Batt0);
  Voltage0 = (Voltage0*vFactor);
  delay (10);     //allow ADC to settle
  Voltage0 = analogRead(Batt0);     // read adc for second time to get stable reading
  Voltage0 = (Voltage0*vFactor);    // calculat voltage
  if (Voltage0 > cutOff){
          if (bitRead (enableFirstRead0,0) == 0){

            FirstVoltage0 = Voltage0;         // save voltage
            bitSet (enableFirstRead0,0);      // set the bit so we do not take another voltage reading (so we know the original battery voltage)
          }
    
    digitalWrite (FetGate0, HIGH);
    Current0 = Voltage0/resistor;
    Capacity0 = Capacity0+Current0/3600;          //3600 seconds in an hour
    Time0++;             //increment seconds counter
  }
  if (Voltage0 < cutOff){  
    digitalWrite (FetGate0, LOW);
  }
  //Print Results to Serial
  if (SerialCount > SerialCheck ){        //prints every 10 seconds
  Serial.print ("Battery 0 Voltage  = ");
  Serial.print (Voltage0, 4);
  Serial.print (", Current = ");
  Serial.print (Current0, 4);
  Serial.print (", Capacity = ");
  Serial.print (Capacity0, 4);
  Serial.print ("Ah");
  Serial.print (", Seconds Passed = ");
  Serial.println (Time0);
  }
  
//-------------- Batt 1 ---------------------
  Voltage1 = analogRead(Batt1);
  Voltage1 = (Voltage1*vFactor);
    delay (10);     //allow ADC to settle
  Voltage1 = analogRead(Batt1);     // read adc for second time to get stable reading
  Voltage1 = (Voltage1*vFactor);    // calculate  voltage
  if (Voltage1 > cutOff){
    if (bitRead (enableFirstRead0,1) == 0){
            FirstVoltage1 = Voltage1;
            bitSet (enableFirstRead0,1);      // set the bit so we do not take another voltage reading (so we know the original battery voltage)
          }
    digitalWrite (FetGate1, HIGH);
    Current1 = Voltage1/resistor;
    Capacity1 = Capacity1+Current1/3600;
    Time1++;             //increment seconds counter
  }
  if (Voltage1 < cutOff){  
    digitalWrite (FetGate1, LOW);
  }
  //Print Results
  if (SerialCount > SerialCheck ){        //prints every 10 seconds
  Serial.print ("Battery 1 Voltage  = ");
  Serial.print (Voltage1, 4);
  Serial.print (", Current = ");
  Serial.print (Current1, 4);
  Serial.print (", Capacity = ");
  Serial.print (Capacity1, 4);
  Serial.print ("Ah");
  Serial.print (", Seconds Passed = ");
  Serial.println (Time1);
  }
//-------------- Batt 2 ---------------------
  Voltage2 = analogRead(Batt2);
  Voltage2 = (Voltage2*vFactor);
    delay (10);     //allow ADC to settle
  Voltage2 = analogRead(Batt2);     // read adc for second time to get stable reading
  Voltage2 = (Voltage2*vFactor);    // calculate  voltage
  if (Voltage2 > cutOff){
    if (bitRead (enableFirstRead0,2) == 0){
            FirstVoltage2 = Voltage2;
            bitSet (enableFirstRead0,2);      // set the bit so we do not take another voltage reading (so we know the original battery voltage)
          }
    digitalWrite (FetGate2, HIGH);
    Current2 = Voltage2/resistor;
    Capacity2 = Capacity2+Current2/3600;
    Time2++;             //increment seconds counter
  }
  if (Voltage2 < cutOff){  
    digitalWrite (FetGate2, LOW);
  }
  //Print Results
  if (SerialCount > SerialCheck ){        //prints every 10 seconds
  Serial.print ("Battery 2 Voltage  = ");
  Serial.print (Voltage2, 4);
  Serial.print (", Current = ");
  Serial.print (Current2, 4);
  Serial.print (", Capacity = ");
  Serial.print (Capacity2, 4);
  Serial.print ("Ah");
  Serial.print (", Seconds Passed = ");
  Serial.println (Time2);
  }
//-------------- Batt 3 ---------------------
  Voltage3 = analogRead(Batt3);
  Voltage3 = (Voltage3*vFactor);
    delay (10);     //allow ADC to settle
  Voltage3 = analogRead(Batt3);     // read adc for second time to get stable reading
  Voltage3 = (Voltage3*vFactor);    // calculate  voltage
  if (Voltage3 > cutOff){
    if (bitRead (enableFirstRead0,3) == 0){
            FirstVoltage3 = Voltage3;
            bitSet (enableFirstRead0,3);      // set the bit so we do not take another voltage reading (so we know the original battery voltage)
          }
    digitalWrite (FetGate3, HIGH);
    Current3 = Voltage3/resistor;
    Capacity3 = Capacity3+Current3/3600;
    Time3++;             //increment seconds counter
  }
  if (Voltage3 < cutOff){  
    digitalWrite (FetGate3, LOW);
  }
  //Print Results
  if (SerialCount > SerialCheck ){        //prints every 10 seconds
  Serial.print ("Battery 3 Voltage  = ");
  Serial.print (Voltage3, 4);
  Serial.print (", Current = ");
  Serial.print (Current3, 4);
  Serial.print (", Capacity = ");
  Serial.print (Capacity3, 4);
  Serial.print ("Ah");
  Serial.print (", Seconds Passed = ");
  Serial.println (Time3);
  
  }
//-------------- Batt 4 ---------------------
  Voltage4 = analogRead(Batt4);
  Voltage4 = (Voltage4*vFactor);
    delay (10);     //allow ADC to settle
  Voltage4 = analogRead(Batt4);     // read adc for second time to get stable reading
  Voltage4 = (Voltage4*vFactor);    // calculate  voltage
  if (Voltage4 > cutOff){
    if (bitRead (enableFirstRead0,4) == 0){
            FirstVoltage4 = Voltage4;
            bitSet (enableFirstRead0,4);      // set the bit so we do not take another voltage reading (so we know the original battery voltage)
          }
    digitalWrite (FetGate4, HIGH);
    Current4 = Voltage4/resistor;
    Capacity4 = Capacity4+Current4/3600;
    Time4++;             //increment seconds counter
  }
  if (Voltage4 < cutOff){  
    digitalWrite (FetGate4, LOW);
  }
  //Print Results
  if (SerialCount > SerialCheck ){        //prints every 10 seconds
  Serial.print ("Battery 4 Voltage  = ");
  Serial.print (Voltage4, 4);
  Serial.print (", Current = ");
  Serial.print (Current4, 4);
  Serial.print (", Capacity = ");
  Serial.print (Capacity4, 4);
  Serial.print ("Ah");
  Serial.print (", Seconds Passed = ");
  Serial.println (Time4);
 
  }
//-------------- Batt 5 ---------------------
  Voltage5 = analogRead(Batt5);
  Voltage5 = (Voltage5*vFactor);
    delay (10);     //allow ADC to settle
  Voltage5 = analogRead(Batt5);     // read adc for second time to get stable reading
  Voltage5 = (Voltage5*vFactor);    // calculate  voltage
  if (Voltage5 > cutOff){
    if (bitRead (enableFirstRead0,5) == 0){
            FirstVoltage5 = Voltage5;
            bitSet (enableFirstRead0,5);      // set the bit so we do not take another voltage reading (so we know the original battery voltage)
          }
    digitalWrite (FetGate5, HIGH);
    Current5 = Voltage5/resistor;
    Capacity5 = Capacity5+Current5/3600;
    Time5++;             //increment seconds counter
  }
  if (Voltage5 < cutOff){  
    digitalWrite (FetGate5, LOW);
  }
  //Print Results
  if (SerialCount > SerialCheck ){        //prints every 10 seconds
  Serial.print ("Battery 5 Voltage  = ");
  Serial.print (Voltage5, 4);
  Serial.print (", Current = ");
  Serial.print (Current5, 4);
  Serial.print (", Capacity = ");
  Serial.print (Capacity5, 4);
  Serial.print ("Ah");
  Serial.print (", Seconds Passed = ");
  Serial.println (Time5);
  
  }
//-------------- Batt 6 ---------------------
  Voltage6 = analogRead(Batt6);
  Voltage6 = (Voltage6*vFactor);
    delay (10);     //allow ADC to settle
  Voltage6 = analogRead(Batt6);     // read adc for second time to get stable reading
  Voltage6 = (Voltage6*vFactor);    // calculate  voltage
  if (Voltage6 > cutOff){
    if (bitRead (enableFirstRead0,6) == 0){
            FirstVoltage6 = Voltage6;
            bitSet (enableFirstRead0,6);      // set the bit so we do not take another voltage reading (so we know the original battery voltage)
          }
    digitalWrite (FetGate6, HIGH);
    Current6 = Voltage6/resistor;
    Capacity6 = Capacity6+Current6/3600;
    Time6++;             //increment seconds counter
  }
  if (Voltage6 < cutOff){  
    digitalWrite (FetGate6, LOW);
  }
  //Print Results
  if (SerialCount > SerialCheck ){        //prints every 10 seconds
  Serial.print ("Battery 6 Voltage  = ");
  Serial.print (Voltage6, 4);
  Serial.print (", Current = ");
  Serial.print (Current6, 4);
  Serial.print (", Capacity = ");
  Serial.print (Capacity6, 4);
  Serial.print ("Ah");
  Serial.print (", Seconds Passed = ");
  Serial.println (Time6);
 
  }
//-------------- Batt 7 ---------------------
  Voltage7 = analogRead(Batt7);
  Voltage7 = (Voltage7*vFactor);
    delay (10);     //allow ADC to settle
  Voltage7 = analogRead(Batt7);     // read adc for second time to get stable reading
  Voltage7 = (Voltage7*vFactor);    // calculate  voltage
  if (Voltage7 > cutOff){
    if (bitRead (enableFirstRead0,7) == 0){
            FirstVoltage7 = Voltage7;
            bitSet (enableFirstRead0,7);      // set the bit so we do not take another voltage reading (so we know the original battery voltage)
          }
    digitalWrite (FetGate7, HIGH);
    Current7 = Voltage7/resistor;
    Capacity7 = Capacity7+Current7/3600;
    Time7++;             //increment seconds counter
  }
  if (Voltage7 < cutOff){  
    digitalWrite (FetGate7, LOW);
  }
  //Print Results
  if (SerialCount > SerialCheck ){        //prints every 10 seconds
  Serial.print ("Battery 7 Voltage  = ");
  Serial.print (Voltage7, 4);
  Serial.print (", Current = ");
  Serial.print (Current7, 4);
  Serial.print (", Capacity = ");
  Serial.print (Capacity7, 4);
  Serial.print ("Ah");
  Serial.print (", Seconds Passed = ");
  Serial.println (Time7);
  
  }
//-------------- Batt 8 ---------------------
  Voltage8 = analogRead(Batt8);
  Voltage8 = (Voltage8*vFactor);
    delay (10);     //allow ADC to settle
  Voltage8 = analogRead(Batt8);     // read adc for second time to get stable reading
  Voltage8 = (Voltage8*vFactor);    //calculate  voltage
  if (Voltage8 > cutOff){
    if (bitRead (enableFirstRead1,0) == 0){
            FirstVoltage8 = Voltage8;
            bitSet (enableFirstRead1,0);      // set the bit so we do not take another voltage reading (so we know the original battery voltage)
          }
    digitalWrite (FetGate8, HIGH);
    Current8 = Voltage8/resistor;
    Capacity8 = Capacity8+Current8/3600;
    Time8++;             //increment seconds counter
  }
  if (Voltage8 < cutOff){  
    digitalWrite (FetGate8, LOW);
  }
  //Print Results
  if (SerialCount > SerialCheck ){        //prints every 10 seconds
  Serial.print ("Battery 8 Voltage  = ");
  Serial.print (Voltage8, 4);
  Serial.print (", Current = ");
  Serial.print (Current8, 4);
  Serial.print (", Capacity = ");
  Serial.print (Capacity8, 4);
  Serial.print ("Ah");
  Serial.print (", Seconds Passed = ");
  Serial.println (Time8);
  
  }
//-------------- Batt 9 ---------------------
  Voltage9 = analogRead(Batt9);
  Voltage9 = (Voltage9*vFactor);
    delay (10);     //allow ADC to settle
  Voltage9 = analogRead(Batt9);     // read adc for second time to get stable reading
  Voltage9 = (Voltage9*vFactor);    // calculate  voltage
  if (Voltage9 > cutOff){
        if (bitRead (enableFirstRead1,1) == 0){
            FirstVoltage9 = Voltage9;
            bitSet (enableFirstRead1,1);      // set the bit so we do not take another voltage reading (so we know the original battery voltage)
          }
    digitalWrite (FetGate9, HIGH);
    Current9 = Voltage9/resistor;
    Capacity9 = Capacity9+Current9/3600;
    Time9++;             //increment seconds counter
  }
  if (Voltage9 < cutOff){  
    digitalWrite (FetGate9, LOW);
  }
  //Print Results
  if (SerialCount > SerialCheck ){        //prints every 10 seconds
  Serial.print ("Battery 9 Voltage  = ");
  Serial.print (Voltage9, 4);
  Serial.print (", Current = ");
  Serial.print (Current9, 4);
  Serial.print (", Capacity = ");
  Serial.print (Capacity9, 4);
  Serial.print ("Ah");
  Serial.print (", Seconds Passed = ");
  Serial.println (Time9);
 
  }
//-------------- Batt 10 ---------------------
  Voltage10 = analogRead(Batt10);
  Voltage10 = (Voltage10*vFactor);
    delay (10);     //allow ADC to settle
  Voltage10 = analogRead(Batt10);     // read adc for second time to get stable reading
  Voltage10 = (Voltage10*vFactor);    // calculate  voltage
  if (Voltage10 > cutOff){
        if (bitRead (enableFirstRead1,2) == 0){
            FirstVoltage10 = Voltage10;
            bitSet (enableFirstRead1,2);      // set the bit so we do not take another voltage reading (so we know the original battery voltage)
          }
    digitalWrite (FetGate10, HIGH);
    Current10 = Voltage10/resistor;
    Capacity10 = Capacity10+Current10/3600;
    Time10++;             //increment seconds counter
  }
  if (Voltage10 < cutOff){  
    digitalWrite (FetGate10, LOW);
  }
  //Print Results
  if (SerialCount > SerialCheck ){        //prints every 10 seconds
  Serial.print ("Battery 10 Voltage = ");
  Serial.print (Voltage10, 4);
  Serial.print (", Current = ");
  Serial.print (Current10, 4);
  Serial.print (", Capacity = ");
  Serial.print (Capacity10, 4);
  Serial.print ("Ah");
  Serial.print (", Seconds Passed = ");
  Serial.println (Time10);
  
  }
//-------------- Batt 11 ---------------------
  Voltage11 = analogRead(Batt11);
  Voltage11 = (Voltage11*vFactor);
    delay (10);     //allow ADC to settle
  Voltage11 = analogRead(Batt11);     // read adc for second time to get stable reading
  Voltage11 = (Voltage11*vFactor);    // calculate  voltage
  if (Voltage11 > cutOff){
        if (bitRead (enableFirstRead1,3) == 0){
            FirstVoltage11 = Voltage11;
            bitSet (enableFirstRead1,3);      // set the bit so we do not take another voltage reading (so we know the original battery voltage)
          }
    digitalWrite (FetGate11, HIGH);
    Current11 = Voltage11/resistor;
    Capacity11 = Capacity11+Current11/3600;
    Time11++;             //increment seconds counter
  }
  if (Voltage11 < cutOff){  
    digitalWrite (FetGate11, LOW);
  }
  //Print Results
  if (SerialCount > SerialCheck ){        //prints every 10 seconds
  Serial.print ("Battery 11 Voltage = ");
  Serial.print (Voltage11, 4);
  Serial.print (", Current = ");
  Serial.print (Current11, 4);
  Serial.print (", Capacity = ");
  Serial.print (Capacity11, 4);
  Serial.print ("Ah");
  Serial.print (", Seconds Passed = ");
  Serial.println (Time11);
  
  }
//-------------- Batt 12 ---------------------
  Voltage12 = analogRead(Batt12);
  Voltage12 = (Voltage12*vFactor);
    delay (10);     //allow ADC to settle
  Voltage12 = analogRead(Batt12);     // read adc for second time to get stable reading
  Voltage12 = (Voltage12*vFactor);    // calculate  voltage
  if (Voltage12 > cutOff){
        if (bitRead (enableFirstRead1,4) == 0){
            FirstVoltage12 = Voltage12;
            bitSet (enableFirstRead1,4);      // set the bit so we do not take another voltage reading (so we know the original battery voltage)
          }
    digitalWrite (FetGate12, HIGH);
    Current12 = Voltage12/resistor;
    Capacity12 = Capacity12+Current12/3600;
    Time12++;             //increment seconds counter
  }
  if (Voltage12 < cutOff){  
    digitalWrite (FetGate12, LOW);
  }
  //Print Results
  if (SerialCount > SerialCheck ){        //prints every 10 seconds
  Serial.print ("Battery 12 Voltage = ");
  Serial.print (Voltage12, 4);
  Serial.print (", Current = ");
  Serial.print (Current12, 4);
  Serial.print (", Capacity = ");
  Serial.print (Capacity12, 4);
  Serial.print ("Ah");
  Serial.print (", Seconds Passed = ");
  Serial.println (Time12);
  
  }
//-------------- Batt 13 ---------------------
  Voltage13 = analogRead(Batt13);
  Voltage13 = (Voltage13*vFactor);
    delay (10);     //allow ADC to settle
  Voltage13 = analogRead(Batt13);     // read adc for second time to get stable reading
  Voltage13 = (Voltage13*vFactor);    // calculate  voltage
  if (Voltage13 > cutOff){
        if (bitRead (enableFirstRead1,5) == 0){
            FirstVoltage13 = Voltage13;
            bitSet (enableFirstRead1,5);      // set the bit so we do not take another voltage reading (so we know the original battery voltage)
          }
    digitalWrite (FetGate13, HIGH);
    Current13 = Voltage13/resistor;
    Capacity13 = Capacity13+Current13/3600;
    Time13++;             //increment seconds counter
  }
  if (Voltage13 < cutOff){  
    digitalWrite (FetGate13, LOW);
  }
  //Print Results
  if (SerialCount > SerialCheck ){        //prints every 10 seconds
  Serial.print ("Battery 13 Voltage = ");
  Serial.print (Voltage13, 4);
  Serial.print (", Current = ");
  Serial.print (Current13, 4);
  Serial.print (", Capacity = ");
  Serial.print (Capacity13, 4);
  Serial.print ("Ah");
  Serial.print (", Seconds Passed = ");
  Serial.println (Time13);
  
  }
//-------------- Batt 14 ---------------------
  Voltage14 = analogRead(Batt14);
  Voltage14 = (Voltage14*vFactor);
    delay (10);     //allow ADC to settle
  Voltage14 = analogRead(Batt14);     // read adc for second time to get stable reading
  Voltage14 = (Voltage14*vFactor);    // calculate  voltage
  if (Voltage14 > cutOff){
        if (bitRead (enableFirstRead1,6) == 0){
            FirstVoltage14 = Voltage14;
            bitSet (enableFirstRead1,6);      // set the bit so we do not take another voltage reading (so we know the original battery voltage)
          }
    digitalWrite (FetGate14, HIGH);
    Current14 = Voltage14/resistor;
    Capacity14 = Capacity14+Current14/3600;
    Time14++;             //increment seconds counter
  }
  if (Voltage14 < cutOff){  
    digitalWrite (FetGate14, LOW);
  }
  //Print Results
  if (SerialCount > SerialCheck ){        //prints every 10 seconds
  Serial.print ("Battery 14 Voltage = ");
  Serial.print (Voltage14, 4);
  Serial.print (", Current = ");
  Serial.print (Current14, 4);
  Serial.print (", Capacity = ");
  Serial.print (Capacity14, 4);
  Serial.print ("Ah");
  Serial.print (", Seconds Passed = ");
  Serial.println (Time14);
  
  }
//-------------- Batt 15 ---------------------
  Voltage15 = analogRead(Batt15);
  Voltage15 = (Voltage15*vFactor);
    delay (10);     //allow ADC to settle
  Voltage15 = analogRead(Batt15);     // read adc for second time to get stable reading
  Voltage15 = (Voltage15*vFactor);    // calculate  voltage
  if (Voltage15 > cutOff){
        if (bitRead (enableFirstRead1,7) == 0){
            FirstVoltage15 = Voltage15;
            bitSet (enableFirstRead1,7);      // set the bit so we do not take another voltage reading (so we know the original battery voltage)
          }
    digitalWrite (FetGate15, HIGH);
    Current15 = Voltage15/resistor;
    Capacity15 = Capacity15+Current15/3600;
    Time15++;             //increment seconds counter
  }
  if (Voltage15 < cutOff){  
    digitalWrite (FetGate15, LOW);
  }
  //Print Results
  if (SerialCount > SerialCheck ){        //prints every 10 seconds
  Serial.print ("Battery 15 Voltage = ");
  Serial.print (Voltage15, 4);
  Serial.print (", Current = ");
  Serial.print (Current15, 4);
  Serial.print (", Capacity = ");
  Serial.print (Capacity15, 4);
  Serial.print ("Ah");
  Serial.print (", Seconds Passed = ");
  Serial.println (Time15);
  SerialCount = 0;        //clear counter
  }
  
}
//====================================================================================================
void lcdUpdate(){
//----------------------------------------------
//Display stats for Battery 0 to LCD
  if (lcdSwitch == 0){
//  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print ("Battery 0 Stats ");
  
  lcd.setCursor(0,1);
  lcd.print ("Original V = ");
  lcd.print (FirstVoltage0, 4);
  lcd.print ("V");
  lcd.setCursor(0,2);
  lcd.print ("Live V = ");
  lcd.print (Voltage0, 4);
  lcd.print ("V");
  lcd.setCursor(0,7);
  lcd.print ("Capacity = ");
  lcd.print (Capacity0, 4);
  lcd.print ("Ah");
  }
//----------------------------------------------
//Display stats for Battery 1 to LCD
  if (lcdSwitch == 1){
//  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print ("Battery 1 Stats ");
  
  lcd.setCursor(0,1);
  lcd.print ("Original V = ");
  lcd.print (FirstVoltage1, 4);
  lcd.print ("V");
  lcd.setCursor(0,2);
  lcd.print ("Live V = ");
  lcd.print (Voltage1, 4);
  lcd.print ("V");
  lcd.setCursor(0,7);
  lcd.print ("Capacity = ");
  lcd.print (Capacity1, 4);
  lcd.print ("Ah");
  }
//----------------------------------------------
//Display stats for Battery 2 to LCD
  if (lcdSwitch == 2){
//  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print ("Battery 2 Stats ");
  
  lcd.setCursor(0,1);
  lcd.print ("Original V = ");
  lcd.print (FirstVoltage2, 4);
  lcd.print ("V");
  lcd.setCursor(0,2);
  lcd.print ("Live V = ");
  lcd.print (Voltage2, 4);
  lcd.print ("V");
  lcd.setCursor(0,7);
  lcd.print ("Capacity = ");
  lcd.print (Capacity2, 4);
  lcd.print ("Ah");
  }
//----------------------------------------------
//Display stats for Battery 3 to LCD
  if (lcdSwitch == 3){
//  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print ("Battery 3 Stats ");
  
  lcd.setCursor(0,1);
  lcd.print ("Original V = ");
  lcd.print (FirstVoltage3, 4);
  lcd.print ("V");
  lcd.setCursor(0,2);
  lcd.print ("Live V = ");
  lcd.print (Voltage3, 4);
  lcd.print ("V");
  lcd.setCursor(0,7);
  lcd.print ("Capacity = ");
  lcd.print (Capacity3, 4);
  lcd.print ("Ah");
  }
//----------------------------------------------
//Display stats for Battery 4 to LCD
  if (lcdSwitch == 4){
//  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print ("Battery 4 Stats ");
  
  lcd.setCursor(0,1);
  lcd.print ("Original V = ");
  lcd.print (FirstVoltage4, 4);
  lcd.print ("V");
  lcd.setCursor(0,2);
  lcd.print ("Live V = ");
  lcd.print (Voltage4, 4);
  lcd.print ("V");
  lcd.setCursor(0,7);
  lcd.print ("Capacity = ");
  lcd.print (Capacity4, 4);
  lcd.print ("Ah");
  }
//----------------------------------------------
//Display stats for Battery 5 to LCD
  if (lcdSwitch == 5){
//  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print ("Battery 5 Stats ");
  
  lcd.setCursor(0,1);
  lcd.print ("Original V = ");
  lcd.print (FirstVoltage5, 4);
  lcd.print ("V");
  lcd.setCursor(0,2);
  lcd.print ("Live V = ");
  lcd.print (Voltage5, 4);
  lcd.print ("V");
  lcd.setCursor(0,7);
  lcd.print ("Capacity = ");
  lcd.print (Capacity5, 4);
  lcd.print ("Ah");
  }
//----------------------------------------------
//Display stats for Battery 6 to LCD
  if (lcdSwitch == 6){
//    lcd.clear();
  lcd.setCursor(0,0);
  lcd.print ("Battery 6 Stats ");
  
  lcd.setCursor(0,1);
  lcd.print ("Original V = ");
  lcd.print (FirstVoltage6, 4);
  lcd.print ("V");
  lcd.setCursor(0,2);
  lcd.print ("Live V = ");
  lcd.print (Voltage6, 4);
  lcd.print ("V");
  lcd.setCursor(0,7);
  lcd.print ("Capacity = ");
  lcd.print (Capacity6, 4);
  lcd.print ("Ah");
  }
//----------------------------------------------
//Display stats for Battery 7 to LCD
  if (lcdSwitch == 7){
//  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print ("Battery 7 Stats ");
  
  lcd.setCursor(0,1);
  lcd.print ("Original V = ");
  lcd.print (FirstVoltage7, 4);
  lcd.print ("V");
  lcd.setCursor(0,2);
  lcd.print ("Live V = ");
  lcd.print (Voltage7, 4);
  lcd.print ("V");
  lcd.setCursor(0,7);
  lcd.print ("Capacity = ");
  lcd.print (Capacity7, 4);
  lcd.print ("Ah");
  }
//----------------------------------------------
//Display stats for Battery 8 to LCD
  if (lcdSwitch == 8){
//  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print ("Battery 8 Stats ");
  
  lcd.setCursor(0,1);
  lcd.print ("Original V = ");
  lcd.print (FirstVoltage8, 4);
  lcd.print ("V");
  lcd.setCursor(0,2);
  lcd.print ("Live V = ");
  lcd.print (Voltage8, 4);
  lcd.print ("V");
  lcd.setCursor(0,7);
  lcd.print ("Capacity = ");
  lcd.print (Capacity8, 4);
  lcd.print ("Ah");
  }
//----------------------------------------------
//Display stats for Battery 9 to LCD
  if (lcdSwitch == 9){
//  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print ("Battery 9 Stats ");
  
  lcd.setCursor(0,1);
  lcd.print ("Original V = ");
  lcd.print (FirstVoltage9, 4);
  lcd.print ("V");
  lcd.setCursor(0,2);
  lcd.print ("Live V = ");
  lcd.print (Voltage9, 4);
  lcd.print ("V");
  lcd.setCursor(0,7);
  lcd.print ("Capacity = ");
  lcd.print (Capacity9, 4);
  lcd.print ("Ah");
  }
//----------------------------------------------
//Display stats for Battery 10 to LCD
  if (lcdSwitch == 10){
//  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print ("Battery 10 Stats");
  
  lcd.setCursor(0,1);
  lcd.print ("Original V = ");
  lcd.print (FirstVoltage10, 4);
  lcd.print ("V");
  lcd.setCursor(0,2);
  lcd.print ("Live V = ");
  lcd.print (Voltage10, 4);
  lcd.print ("V");
  lcd.setCursor(0,7);
  lcd.print ("Capacity = ");
  lcd.print (Capacity10, 4);
  lcd.print ("Ah");
  }
//----------------------------------------------
//Display stats for Battery 11 to LCD
  if (lcdSwitch == 11){
//  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print ("Battery 11 Stats");
  
  lcd.setCursor(0,1);
  lcd.print ("Original V = ");
  lcd.print (FirstVoltage11, 4);
  lcd.print ("V");
  lcd.setCursor(0,2);
  lcd.print ("Live V = ");
  lcd.print (Voltage11, 4);
  lcd.print ("V");
  lcd.setCursor(0,7);
  lcd.print ("Capacity = ");
  lcd.print (Capacity11, 4);
  lcd.print ("Ah");
  }
//----------------------------------------------
//Display stats for Battery 12 to LCD
  if (lcdSwitch == 12){
//  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print ("Battery 12 Stats");
  
  lcd.setCursor(0,1);
  lcd.print ("Original V = ");
  lcd.print (FirstVoltage12, 4);
  lcd.print ("V");
  lcd.setCursor(0,2);
  lcd.print ("Live V = ");
  lcd.print (Voltage12, 4);
  lcd.print ("V");
  lcd.setCursor(0,7);
  lcd.print ("Capacity = ");
  lcd.print (Capacity12, 4);
  lcd.print ("Ah");
  }
//----------------------------------------------
//Display stats for Battery 13 to LCD
  if (lcdSwitch == 13){
//  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print ("Battery 13 Stats");
  
  lcd.setCursor(0,1);
  lcd.print ("Original V = ");
  lcd.print (FirstVoltage13, 4);
  lcd.print ("V");
  lcd.setCursor(0,2);
  lcd.print ("Live V = ");
  lcd.print (Voltage13, 4);
  lcd.print ("V");
  lcd.setCursor(0,7);
  lcd.print ("Capacity = ");
  lcd.print (Capacity13, 4);
  lcd.print ("Ah");
  }
//----------------------------------------------
//Display stats for Battery 14 to LCD
  if (lcdSwitch == 14){
//  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print ("Battery 14 Stats");
  
  lcd.setCursor(0,1);
  lcd.print ("Original V = ");
  lcd.print (FirstVoltage14, 4);
  lcd.print ("V");
  lcd.setCursor(0,2);
  lcd.print ("Live V = ");
  lcd.print (Voltage14, 4);
  lcd.print ("V");
  lcd.setCursor(0,7);
  lcd.print ("Capacity = ");
  lcd.print (Capacity14, 4);
  lcd.print ("Ah");
  }
  //----------------------------------------------
//Display stats for Battery 15 to LCD
  if (lcdSwitch == 15){
//  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print ("Battery 15 Stats");
  
  lcd.setCursor(0,1);
  lcd.print ("Original V = ");
  lcd.print (FirstVoltage15, 4);
  lcd.print ("V");
  lcd.setCursor(0,2);
  lcd.print ("Live V = ");
  lcd.print (Voltage15, 4);
  lcd.print ("V");
  lcd.setCursor(0,7);
  lcd.print ("Capacity = ");
  lcd.print (Capacity15, 4);
  lcd.print ("Ah");
  }
}
