Help to make my BMS code more efficient / simpler

The following code uses a mega to measure the voltage of 16 cells in a lithium battery, one by one, maintaining independent grounding (via solid state relays), and can instruct to discharge any cell that is above average. Data outputs via bluetooth to an android device.

I would prefer a discharge to last 3 seconds but Im not going to do the enormous amount of blinkwithoutdelay coding for that. There is a small (unknown) bug, every now and then ALL alarms trigger for no reason. I have attempted to patch this bug by checking how many alarms are triggered before triggering any.

The “impossible” BMS:

#include <Wire.h>
int del = 30;
int Pins[] = {
  A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, 2
};       // an array of pin numbers to which relays are attached
int BP[] = {
  23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53
};       // an array of pin numbers to which Resistors are attached
int bat[] = {
  1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 //cells to measure
};
int batV[] = {
  1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 //stored cell voltage
};
int pinCount = 16;           // the number of cells
int VB = 0;
int b48, ba;
int lead = 10;
String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete
int top = 0;
int bot = 0;
int all = 0;
int alarms = 0;
void setup()
{
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {//set up each pin
    pinMode(Pins[thisPin], OUTPUT);
    pinMode(BP[thisPin], OUTPUT);
    pinMode(BP[thisPin], HIGH);
  }
  pinMode(A0, INPUT);//set up A0 pin for reading voltage
  Serial.begin(9600);
  inputString.reserve(10);
}
void serialEvent() {
  while (Serial.available()) {//listen for a command from the android
    char inChar = (char)Serial.read();//store the character
    inputString += inChar;//add the characters together
    if (inChar == 'A') {//if A command, it is changing top calibration number
      top = Serial.parseInt();
      top = top - 50;
      top = top / 5;
      Serial.print("*>");
      Serial.print(top);
      Serial.print("*");
    }
    if (inChar == 'B') {//if B command, it is changing all calibration number
      all = Serial.parseInt();
      all = all - 50;
      all = all / 5;
      Serial.print("*|");
      Serial.print(all);
      Serial.print("*");
    }
    if (inChar == 'C') {//if C command, it is changing bottom calibration number
      bot = Serial.parseInt();
      bot = bot - 50;
      bot = bot / 5;
      Serial.print("*<");
      Serial.print(bot);
      Serial.print("*");
    }
    if (inChar == 'D') {//if D command, it is changing lead number
      lead = Serial.parseInt();
      lead = lead / 5;
      Serial.print("*~");
      Serial.print(lead);
      Serial.print("*");
    }
    if (inChar == '\n') {//check for end of command
      stringComplete = true;
    }
  }
}
void loop()
{
  if (stringComplete) {
    Serial.println(inputString);
    inputString = "";
    stringComplete = false;
  }
  for (int thisPin = 0; thisPin < pinCount; thisPin++) { //this loop will go through each cell one by one
    digitalWrite(Pins[thisPin], HIGH); //turn on the relay so we can measure the cell
    delay(del);
    for (int i = 0; i < 20; i++) { //measure the voltage of each cell 20x
      int V = analogRead(A0); //measure the voltage on the analog pin
      VB = VB + V; //add the 20 measurements together
    }
    bat[thisPin] = VB / 20; //take average of the 20 measurements
    VB = 0;
    bat[thisPin] = constrain(bat[thisPin], 400, 900); //check for error measurements
    batV[thisPin] = bat[thisPin];//store the voltage reading
    batV[thisPin] = map(bat[thisPin], 434, 867, (200 + bot), (400 + top)); //convert the analog reading into mv
    batV[thisPin] = (batV[thisPin] + all); //calibrate the voltage
    b48 = b48 + batV[thisPin]; //add the cell voltages together to give pack voltage
    digitalWrite(Pins[thisPin], LOW); //turn off the relay, we have finished measuring that cell
  } //check for an abnormal number of alarms, first count how many alarms
  if (batV[0] - lead > ba) { //is battery voltage above average? if yes, track the alarms
    alarms++;
  }
  if (batV[1] - lead > ba) {
    alarms++;
  }
  if (batV[2] - lead > ba) {
    alarms++;
  }
  if (batV[3] - lead > ba) {
    alarms++;
  }
  if (batV[4] - lead > ba) {
    alarms++;
  }
  if (batV[5] - lead > ba) {
    alarms++;
  }
  if (batV[6] - lead > ba) {
    alarms++;
  }
  if (batV[7] - lead > ba) {
    alarms++;
  }
  if (batV[8] - lead > ba) {
    alarms++;
  }
  if (batV[9] - lead > ba) {
    alarms++;
  }
  if (batV[10] - lead > ba) {
    alarms++;
  }
  if (batV[11] - lead > ba) {
    alarms++;
  }
  if (batV[12] - lead > ba) {
    alarms++;
  }
  if (batV[13] - lead > ba) {
    alarms++;
  }
  if (batV[14] - lead > ba) {
    alarms++;
  }
  if (batV[15] - lead > ba) {
    alarms++;
  }
if (alarms < 4) {//if normal number of alarms, proceed
      if (batV[0] - lead > ba) {//is cell 1 above average? if yes, turn on discharge circuit 
    Serial.print("*AR255G0B0");//tell the light A to turn red
    Serial.print("*q*");//tell the buzzer q to sound
    
    digitalWrite(BP[1], LOW);//tell the discharge circuit to turn on by making pin go low
    alarms++;
  }
  else  {//if not above average, turn off the discharge circuit
    Serial.print("*AR0G0B0*");//tell the light to turn off
    digitalWrite(BP[1], HIGH);//tell the discharge circuit to turn off by making pin go high
  }
  if (batV[1] - lead > ba) {
    Serial.print("*QR255G0B0*");
    Serial.print("*r*");
    digitalWrite(BP[2], LOW);
    alarms++;
  }
  else  {
    Serial.print("*QR0G0B0*");
    digitalWrite(BP[2], HIGH);
  }
  if (batV[2] - lead > ba) {
    Serial.print("*CR255G0B0*");
    Serial.print("*s*");
    digitalWrite(BP[3], LOW);
    alarms++;
  }
  else  {
    Serial.print("*CR0G0B0*");
    digitalWrite(BP[3], HIGH);
  }
  if (batV[3] - lead > ba) {
    Serial.print("*DR255G0B0*");
    Serial.print("*t*");
    digitalWrite(BP[4], LOW);
    alarms++;
  }
  else  {
    Serial.print("*DR0G0B0*");
    digitalWrite(BP[4], HIGH);
  }
  if (batV[4] - lead > ba) {
    Serial.print("*ER255G0B0*");
    Serial.print("*u*");
    digitalWrite(BP[5], LOW);
    alarms++;
  }
  else  {
    Serial.print("*ER0G0B0*");
    digitalWrite(BP[5], HIGH);
  }
  if (batV[5] - lead > ba) {
    Serial.print("*FR255G0B0*");
    Serial.print("*v*");
    digitalWrite(BP[6], LOW);
    alarms++;
  }
  else  {
    Serial.print("*FR0G0B0*");
    digitalWrite(BP[6], HIGH);
  }
  if (batV[6] - lead > ba) {
    Serial.print("*SR255G0B0*");
    Serial.print("*!*");
    digitalWrite(BP[7], LOW);
    alarms++;
  }
  else  {
    Serial.print("*SR0G0B0*");
    digitalWrite(BP[7], HIGH);
  }
  if (batV[7] - lead > ba) {
    Serial.print("*HR255G0B0*");
    Serial.print("*@*");
    digitalWrite(BP[8], LOW);
    alarms++;
  }
  else  {
    Serial.print("*HR0G0B0*");
    digitalWrite(BP[8], HIGH);
  }
  if (batV[8] - lead > ba) {
    Serial.print("*IR255G0B0*");
    Serial.print("*#*");
    digitalWrite(BP[9], LOW);
    alarms++;
  }
  else  {
    Serial.print("*IR0G0B0*");
    digitalWrite(BP[9], HIGH);
  }
  if (batV[9] - lead > ba) {
    Serial.print("*JR255G0B0*");
    Serial.print("*$*");
    digitalWrite(BP[10], LOW);
    alarms++;
  }
  else  {
    Serial.print("*JR0G0B0*");
    digitalWrite(BP[10], HIGH);
  }
  if (batV[10] - lead > ba) {
    Serial.print("*KR255G0B0*");
    Serial.print("*?*");
    digitalWrite(BP[11], LOW);
    alarms++;
  }
  else  {
    Serial.print("*KR0G0B0*");
    digitalWrite(BP[11], HIGH);
  }
  if (batV[11] - lead > ba) {
    Serial.print("*LR255G0B0*");
    Serial.print("*%*");
    digitalWrite(BP[12], LOW);
    alarms++;
  }
  else  {
    Serial.print("*LR0G0B0*");
    digitalWrite(BP[12], HIGH);
  }
  if (batV[12] - lead > ba) {
    Serial.print("*MR255G0B0*");
    Serial.print("*^*");
    digitalWrite(BP[13], LOW);
    alarms++;
  }
  else  {
    Serial.print("*MR0G0B0*");
    digitalWrite(BP[13], HIGH);
  }
  if (batV[13] - lead > ba) {
    Serial.print("*NR255G0B0*");
    Serial.print("*&*");
    digitalWrite(BP[14], LOW);
    alarms++;
  }
  else  {
    Serial.print("*NR0G0B0*");
    digitalWrite(BP[14], HIGH);
  }
  if (batV[14] - lead > ba) {
    Serial.print("*OR255G0B0*");
    Serial.print("*+*");
    digitalWrite(BP[15], LOW);
    alarms++;
  }
  else  {
    Serial.print("*OR0G0B0*");
    digitalWrite(BP[15], HIGH);
  }
  if (batV[15] - lead - 1 > ba) {
    Serial.print("*PR255G0B0*");
    Serial.print("*-*");
    digitalWrite(BP[16], LOW);
    alarms++;
  }
  else  {
    Serial.print("*PR0G0B0*");
    digitalWrite(BP[16], HIGH);
  }
  }
  if (alarms > 3) {//if abnormal number of alarms, allow a bigger difference to average
    lead = lead + 2;
    Serial.print("*~");
    Serial.print(lead);
    Serial.print("*");
  }
  else if (alarms > 0) {//if any alarms active, give the discharge circuits some time to discharge before checking voltage again
  delay(1000);
  }
  else if (alarms < 1 && lead > 3) {//if no alarms and lead higher than normal, reduce lead
    lead--;
    Serial.print("*~");
    Serial.print(lead);
    Serial.print("*");
  }
  alarms = 0;
  ba = b48 / pinCount; //calculate average cell voltage
  Serial.print("*Z");
  Serial.print(b48);//display the pack voltage
  Serial1.print(b48);//output the pack voltage to serial2
  Serial1.println(",");
  Serial.print("*z");//display the average cell voltage
  Serial.print(ba);
  Serial.println("*");
  b48 = 0;
  
  VB = 0;
}

There are some things that are being done in loop() that do not make sense.

For instance, take a bunch of readings, adding the reading to the value in VB, making a copy of VB, and then setting VB to 0. VB should be set to ) BEFORE accumulating the readings.

    batV[thisPin] = (batV[thisPin] + all); //calibrate the voltage

(Are) (those) (parentheses) (really) (necessary) (?)

After that loop, why on earth are you repeating code 16 times?
After that loop, why on earth are you repeating code 16 times?
After that loop, why on earth are you repeating code 16 times?
After that loop, why on earth are you repeating code 16 times?
After that loop, why on earth are you repeating code 16 times?

Itisagoodoracticetousesomespacesbeforeandafterthecommentsymbol,tomakethecodeeasiertoread.

Longer names for variables might be a better idea. What does ba mean, anyway?

Proper indenting is ALWAYS a good idea.

There are 16 cells to measure, and 16 cells to discharge. So anything that cannot be in the loop must be repeated 16x.

VB can be reset before or after, it makes no difference since it is a loop. If vb was not stored in another variable it would be lost in the next loop. The goal is to measure, average and store the voltage of 16 cells.

ba = battery average

So anything that cannot be in the loop must be repeated 16x.

I don't see anything in the 16X code that "cannot be in the loop", given just a little thought.