#include <LiquidCrystal.h> float aref = 1.237;
int interval = 10; // Connections:
// rs (LCD pin 4) to Arduino pin 12
// rw (LCD pin 5) to Arduino pin 11
// enable (LCD pin 6) to Arduino pin 10
// LCD pin 15 to Arduino pin 13
// LCD pins d4, d5, d6, d7 to Arduino pins 5, 4, 3, 2
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2); void setup()
{
analogReference(EXTERNAL);
// start serial port at 9600 bps:
Serial.begin(9600);
lcd.begin(16, 2);
lcd.clear(); // start with a blank screen
} class Battery {
public:
enum States { EndDischarge, Discharging };
Battery(int pinin, float res) {
pin = pinin;
resistance = res;
reset();
}; void reset() {
state = EndDischarge;
as = 0;
elapsed = 0;
};
float getmAh() {
return float(as*interval)/3600.0*1000.0;
};
States getstate() {
return state;
}
int getpin() {
return pin;
}
void tick() {
int val = analogRead(pin);
float volts = float(val)/1024.0*aref;
float current = volts/resistance;
switch (state) {
case EndDischarge:
if (volts > .8) {
reset();
state = Discharging;
Serial << pin << ” battery connectedn”;
break;
}
return;
break;
case Discharging:
if (volts < .6) {
Serial << pin << ” battery discharged with ” << getmAh() << ” mAhn”;
state = EndDischarge;
return;
}
break;
}
elapsed += interval;
as += current;
Serial << pin << ” volts ” << volts << ” current ” << current << ” mAh ” << getmAh() << “n”;
};
private:
int pin;
float as;
long elapsed;
float resistance;
States state;
}; Battery batteries[] = {
Battery(0, 1.0),
Battery(1, 1.0),
Battery(2, 1.0),
Battery(3, 1.0)
}; void loop()
{
delay(interval*1000);
lcd.clear();
for(int i=0; i<sizeof(batteries)/sizeof(Battery); i++) {
batteries[i].tick();
lcd.setCursor((batteries[i].getpin()%2)*5, batteries[i].getpin()/2);
lcd.print(int(batteries[i].getmAh()));
if (batteries[i].getstate() == Battery::EndDischarge) {
lcd.print(“d”);
}
}
}
Arduino based NiMh AA battery tester