#define TOTAL_CELLS 4
float cellVoltage[TOTAL_CELLS] = {3.12,3.23,3.34,3.45};
float batterySoC = 80.0;
float previousImbalance = 0.0;
boolean firstAnalysis = true;
float calculateAdaptiveThreshold(float soc)
{
if(soc >= 80.0)
{
return 0.05;
}else if(soc >= 50.0)
{
return 0.07;
}else
{
return 0.10;
}
}
void updateBatteryStatus()
{
int weakestCell = 0;
int strongestCell = 0;
for(int i = 1;i < TOTAL_CELLS;i++)
{
if(cellVoltage[i] < cellVoltage[weakestCell]) weakestCell = i;
if(cellVoltage[i] > cellVoltage[strongestCell]) strongestCell = i;
}
float currentImbalance = cellVoltage[strongestCell] - cellVoltage[weakestCell];
float adaptiveThreshold = calculateAdaptiveThreshold(batterySoC);
Serial.print("Battery SoC: ");
Serial.print(batterySoC, 1);
Serial.println(" %");
Serial.print("Adaptive Threshold: ");
Serial.print(adaptiveThreshold, 2);
Serial.println(" V");
if(firstAnalysis)
{
previousImbalance = currentImbalance;
firstAnalysis = false;
Serial.println("Imbalance Trend: First Analysis");
}
else
{
if(currentImbalance > previousImbalance)
{
Serial.println("Imbalance Trend: Increasing");
}
else if(currentImbalance < previousImbalance)
{
Serial.println("Imbalance Trend: Decreasing");
}
else
{
Serial.println("Imbalance Trend: Stable");
}
previousImbalance = currentImbalance;
}
Serial.println("------ BMS Analysis ------");
Serial.print("Weakest Cell: ");
Serial.println(weakestCell+1);
Serial.print("Weakest Voltage: ");
Serial.print(cellVoltage[weakestCell], 2);
Serial.println(" V");
Serial.print("Strongest Cell: ");
Serial.println(strongestCell+1);
Serial.print("Strongest Voltage: ");
Serial.print(cellVoltage[strongestCell], 2);
Serial.println(" V");
Serial.print("Current Imbalance: ");
Serial.print(currentImbalance, 2);
Serial.println(" V");
//Serial.println("---------------------------");
}
void setup()
{
Serial.begin(115200);
}
void loop()
{
updateBatteryStatus();
delay(1000);
}