code
code
#include <Keypad.h>
//http://playground.arduino.cc/Code/Keypad
byte rowPins[ROWS] = {5, 6, 7, 8}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 10, 11, 12}; //connect to the column pinouts of the keypad
const byte MCP79410_ADDRESS = 0x6f; //0x6f is the default address for the
MCP79410 Real Time Clock IC
MCP79410_Timer timer = MCP79410_Timer(MCP79410_ADDRESS);
//Set the pins on the I2C chip used for LCD connections
//ADDR,EN,R/W,RS,D4,D5,D6,D7
LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7); //0x27 is the default address of the
LCD with I2C bus module
float BatteryLife = 0; //
float BatteryLifePrevious = 0; //
float Seconds = 0; //
float BatteryCutoffVolts; //used to set battery discharge cut-
off voltage (was 3.0)
float MaxBatteryCurrent = 1.0; //maximum battery current allowed for
Battery Capacity Testing
int setReading = 0; //
byte exitMode = 0; //used to exit battery selection menu and return to CC Mode
lcd.setCursor(8,0);
lcd.print("OFF"); //indicate that LOAD is
off at start up
Current(); //sets current mode to CC
(Constant Current) at Power Up
customKey = customKeypad.getKey();
}
//------------------------------------------Main Program
Loop---------------------------------
void loop() {
readKeypadInput(); //read Keypad entry
dacControl();
dacControlVoltage(); //sets the drive voltage
to control the MOSFET
batteryCapacity(); //test if Battery Capacity
(BC) mode is selected - if so action
fanControl(); //call heatsink fan
control
}
//----------------------Read Keypad
Input-----------------------------------------------------
void readKeypadInput (void) {
customKey = customKeypad.getKey();
//--------------------------Cursor
Position-------------------------------------------------------
//Change the position routine
void CursorPosition(void) {
if (digitalRead(CursorPos) == LOW) {
if (CP == 8) { //
factor = 10000; //
}
}
MCP342x::Config status;
// Initiate a conversion; convertAndRead() will wait until it can be read
adc.convertAndRead(MCP342x::channel1, MCP342x::oneShot,
MCP342x::resolution16, MCP342x::gain1, //"gain1"
means we have select the input amp of the ADC to x1
1000000, voltage, status);
if (ActualVoltage <10) {
VoltsDecimalPlaces = 3;
}else{
VoltsDecimalPlaces = 2;
}
lcd.setCursor(0,1);
lcd.print(ActualCurrent,3);
lcd.print("A");
lcd.print(" "); //two spaces between
actual current and voltage readings
lcd.print(ActualVoltage,VoltsDecimalPlaces);
lcd.print("V");
lcd.print(" ");
lcd.print(ActualPower,2);
lcd.print("W");
lcd.print(" ");
}
if (Mode == "CP"){
setPower = reading*1000; //in Watts
setReading = setPower;
setCurrent = setPower/ActualVoltage;
setControlCurrent = (setCurrent * setCurrentCalibrationFactor)*
dacCalibrationFactor;
controlVoltage = setControlCurrent; //
}
if (Mode == "CR"){
setResistance = reading; //in ohms
setReading = setResistance;
setCurrent = (ActualVoltage)/setResistance*1000;
setControlCurrent = (setCurrent * setCurrentCalibrationFactor )*
dacCalibrationFactor;
controlVoltage = setControlCurrent;
}
}
lcd.setCursor(0,3);
lcd.print (timer.getTime()); //start clock and print
clock time
lcd.print(BatteryLife,0);
lcd.setCursor(13,3);
lcd.print("mAh");
BatteryLifePrevious = BatteryLife; //update displayed
battery capacity on LCD
}
}
BatteryCurrent = ActualCurrent;
dac.setVoltage(0,false); //reset DAC to zero for
no output current set at switch on
//Load = 0; //Load is toggled OFF
toggle = false;
lcd.setCursor(8,0);
lcd.print("OFF"); //indicate that LOAD is
off at start up
timer.stop();
}
}
//-----------------------Fan
Control----------------------------------------------------------
void fanControl (void) {
temp = analogRead(temperature);
temp = temp * 0.107421875; // convert to Celsius
if (customKey == '1'){
BatteryCutoffVolts = LiPoCutOffVoltage;
BatteryType = ("LiPo");
}
if (customKey == '2'){
BatteryCutoffVolts = LiFeCutOffVoltage;
BatteryType = ("LiFe");
}
if (customKey == '3'){
BatteryCutoffVolts = NiCdCutOffVoltage;
BatteryType = ("NiCd");
}
if (customKey == '4'){
BatteryCutoffVolts = ZiZnCutOffVoltage;
BatteryType = ("ZiZn");
}
if (customKey == '5'){
BatteryCutoffVolts = PbAcCutOffVoltage;
BatteryType = ("PbAc");
}
}else{
dac.setVoltage(controlVoltage,false); //set DAC output voltage
for Range selected
if(Mode == "BC" && ActualVoltage >= BatteryCutoffVolts && timer.status() != 1){
timer.start();
}
}
}
//--------------------------Battery Selected
Information--------------------------------------------
void batteryTypeSelected (void) {
if (exitMode !=1){ //if battery selection
was EXIT then skip this routine
lcd.clear();
lcd.setCursor(2,0);
lcd.print("Battery Selected");
lcd.setCursor(8,1);
lcd.print(BatteryType); //display battery type
selected
lcd.setCursor(2,2);
lcd.print("Discharge Cutoff");
lcd.setCursor(6,3);
lcd.print(BatteryCutoffVolts); //display battery
discharge cut off voltage
lcd.print(" volts");
delay(3000);
}
}
//---------------------------------------------------------------------------------
-----------------