float timevalue, current, elapsed, analogvalue5, voltage, milliamphours, cutoffvoltage, finalmah, slope,lastvolt;
float analogvalue, vcell1, vcell2, vcell3, vcell4;
int dacount, lock1, lock2, lock3, lock4;
void setup()
{
Serial.begin(9600); //turn on serial monitor
pinMode(8, OUTPUT); // enable pin 8 for output, just change the number for other available pins
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
digitalWrite(8, HIGH); // turn off relay connected to pin 8 and may need to be reversed for FET control
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
delay(10000); //delay ten seconds for oops time and uploads
digitalWrite(8, LOW); // turn on relay connected to pin 8 and may need to be reversed for FET control
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
lock1=0; // these hold the final mah value for each cell
lock2=0; // set when voltage goes below threshold
lock3=0;
lock4=0;
}
int getanalogvalue() // function to read A/D, takes "dacount" modifies "voltage"
{
for(int x;x<5000;x++) // this loop gets the analog value of A/D (X) 5000 times and adds it to itself
{
analogvalue=analogvalue+analogRead(dacount);
}
analogvalue=analogvalue/5000; // averages the A/D value
voltage=analogvalue*0.005664; // calculates voltage from A/D value
}
///////////////// start main program ///////////////////////////
void loop()
{
cutoffvoltage =2.8; // Your cell cutoff low voltage goes here
delay(7800); // Delay to slow down the readout and make it more legible
// updates every ten seconds or so with this number
analogvalue=0;
dacount=1; // gets the cell voltage and stores it..one for each channel
getanalogvalue();
vcell1=voltage;
dacount=2;
getanalogvalue();
vcell2=voltage;
dacount=3;
getanalogvalue();
vcell3=voltage;
dacount=4;
getanalogvalue();
vcell4=voltage;
elapsed=millis()/1000; // calculates seconds since program started
Serial.print(elapsed);
Serial.println(" seconds discharging");
milliamphours=elapsed*.297; // use this number for a Sylvania 1156L 12V lamp as a load for one cell,
// different lamps will require different numbers
Serial.print(vcell1);
Serial.print(" ");
Serial.print(vcell2);
Serial.print(" ");
Serial.print(vcell3);
Serial.print(" ");
Serial.print(vcell4);
Serial.println(" Voltage ");
//Serial.println();
if (vcell1<cutoffvoltage) //makes the decision to cut off off if below threshold
{
if (lock1==0)
{
lock1=milliamphours;
}
digitalWrite(8, HIGH); //turns off relay and may need to be reversed for FET control
}
if (vcell2<cutoffvoltage)
{
if (lock2==0)lock2=milliamphours;
digitalWrite(9, HIGH);
}
if (vcell3<cutoffvoltage)
{
if (lock3==0)lock3=milliamphours;
digitalWrite(10, HIGH);
}
if (vcell4<cutoffvoltage)
{
if (lock4==0)lock4=milliamphours;
digitalWrite(11, HIGH);
}
if (lock1>0)
{
Serial.print(lock1);
Serial.print(" ");
}
else
{
Serial.print(milliamphours);
Serial.print(" ");
}
if (lock2>0)
{
Serial.print(lock2);
Serial.print(" ");
}
else
{
Serial.print(milliamphours);
Serial.print(" ");
}
if (lock3>0)
{
Serial.print(lock3);
Serial.print(" ");
}
else
{
Serial.print(milliamphours);
Serial.print(" ");
}
if (lock4>0)
{
Serial.print(lock4);
Serial.print(" ");
}
else
{
Serial.print(milliamphours);
Serial.print(" ");
}
Serial.println(" Milliamphours");
if (lock1>0 && lock2>0 && lock3>0 && lock4>0)
{
Serial.println();
Serial.println("Done discharging");
while(1){}
}
}