Intelligent charger for 9v nimh battery by v1

Time.h library error in this project code
please give me solution

It seems that line 76 of the code is incorrect.

No I thought it was line 106

Anymore bets

//THis for charging 9V NiMH battery 
//Battery can have have 6,7 or 8 1.25V cells
//This makes nominal voltage between 7.5 and 10V

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Time.h> 
#include <TimerOne.h>
#include <elapsedMillis.h>

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // set the LCD address to 0x27 for a 16 chars and 2 line display

//Hardware defenitions
#define MCP4725_ADDR 0x60 //DAC address
#define DAC_REF_VOLTAGE 5.0
#define CHARGE_RELAY_PIN 2
#define DISCONNECT_CHARGE_RELAY HIGH
#define CONNECT_CHARGE_RELAY LOW
#define CURRENT_SENSING_SESISTOR_PIN A2
#define CURRENT_SENSING_SESISTOR 10.2 //OHm
#define BATTERY_VOLTAGE_PIN A0
#define R1 5090 //Low side resistor of voltage sensing divider
#define R2 19910//High side resistor of voltage sensing divider
#define ADC_REF_VOLTAGE 4.89 //Arduino real supply voltage or AREF voltage for proper ADC to voltage translation
#define R3 3300.0
#define R4 6800.0
#define AMP_GAIN (1+R4/R3)

//Various definitions
//#define MINIMUM_VOLTAGE 8.1 //Minimum voltage after regulator
#define BATTERY_GOOD_VOLTAGE_THRESHOLD 7.0
#define MAXIMIM_ALLOWED_CURRENT 200 //Maximum allowed battery current mA (hard cut-off)
#define MAXIMUM_BATTERY_VOLTAGE 15.0 //Maximum allowed battery voltage V (hard cut-off)
#define VOLTAGE_STEP 0.001 //Step for voltage regulation
#define POINTS_FOR_AVERAGING 100 //How many points taken for averaging
#define MEASURE2MEASURE_US 5000 //Time between measurments in microseconds (need be more then 200 becouse of two analog reads)
#define VOLTAGE_LOG_TIME_MIN 30.0 //Time in minutes to save voltage for dV/dT cutoff
#define MINUTES_TO_LOG 5
#define TRICKLE_CURRENT_RATIO 40

//Charger states
#define INITIALIZATION 0
#define NO_BATTERY 5
#define BATTERY_CONNECTED 10
#define BATTERY_VOLTAGE_LOW 15
#define CURRENT_RAMP_UP 20
#define CHARGING 30
#define CURRENT_RAMP_DOWN 32
#define TRICKLE 35
#define END_OF_TRICKLE 37
#define UNEXPECTED_CURRENT_FALL 40
#define UNABLE_TO_REACH_CURRENT 60
#define FULLY_CHARGED 70
#define CURRENT_TOO_HIGH 80
#define REGULATION_FAILED 90
#define OVERCURRENT 100
#define OVERVOLTAGE 101
#define FINAL_STATE 200


///
unsigned int voltage_readings[POINTS_FOR_AVERAGING],current_sensing_resistor_readings[POINTS_FOR_AVERAGING],averaging_index;//For averaging
long unsigned int voltage_sum,current_sum;//Sum of averaging
float regulator_voltage_code,resistor_voltage,csr_voltage_code,regulator_voltage,current_mA,battery_voltage;//Measurments
float tmp_resistor_voltage,tmp_current_mA,tmp_regulator_voltage,tmp_battery_voltage;
int i,j,charger_state;
short unsigned int last_second,lcd_last_second,log_last_second;
String lcd_last_string1,lcd_last_string2;
String empty_string="";
String msg,eoc_line1,eoc_line2;
unsigned char sec_index,min_index;
//long long int charging_started;
float sec_log[60],min_log[MINUTES_TO_LOG],last_blf;
float trickle_current_mA;
int total_minutes_average=0;

elapsedMillis ChargingTimeMillis,TrickleChargingTimeMillis;

float wanted_dac_voltage=0;
float last_dac_voltage=0;

//Messages
const char msg_battery_detected[] PROGMEM 		="Battery detected";
const char msg_no_battery[] PROGMEM				="No battery";
const char msg_battery_ok[] PROGMEM				="Battery ok";
const char msg_voltage_too_low[] PROGMEM		="Battery voltage too low";
const char msg_voltage_too_low_short[] PROGMEM	="V Battery low";
const char msg_ramp_up[] PROGMEM				="Ramp up";
const char msg_charging[] PROGMEM				="Charging";
const char msg_space[] PROGMEM					=" ";
const char msg_ramp_down[] PROGMEM				="Ramp down";
const char msg_trickle_charge[] PROGMEM			="Trickle charge";
const char msg_no_current[] PROGMEM				="No current";
const char msg_current_unreachable[] PROGMEM	="I unreachable";
const char msg_current_unreachable_long[] PROGMEM	="Unable to reach desired current";
const char msg_completed[] PROGMEM				="Completed";
const char msg_charge[] PROGMEM					="Charge";
const char msg_high_current[] PROGMEM			="High current";
const char msg_regulation_fault[] PROGMEM		="Regulation fault";
const char msg_overcurrent[] PROGMEM			="Current too high";
const char msg_overvoltage[] PROGMEM			="Voltage too high";
const char msg_trickle_completed[] PROGMEM		="Trickle finished";

//*************************** Charging parameters *****************************************
//*****************************************************************************************
float target_current_mA=40;				//Charging current mA
float battery_nominal_capacity_mA=170;	//Nominal capacity of battery mA
float max_time_for_trickle_charge=6;  //Maximum trickle charge time in minutes
//*****************************************************************************************
//*****************************************************************************************

struct mytime {
    unsigned char hours;
    unsigned char minutes;
	unsigned int total_minutes;
} elapsed_time;

void setup() {
	pinMode(CHARGE_RELAY_PIN, OUTPUT);
	disconnect_charging_circuit();	//Disconnect charger ftom battery
	Wire.begin();//I2C
	dac_write_voltage(0);//Make sure lower possible current set
	
	Serial.begin(115200);
	last_second=second();
	lcd_last_second=second();
	log_last_second=second();
	
	Timer1.initialize(MEASURE2MEASURE_US); //Will use to measure voltage and current of battery (microseconds)
	Timer1.attachInterrupt(read_hw);  // attaches read_hw() as a timer overflow interrupt
	averaging_index=0;
	sec_index=0;
	min_index=0;
	charger_state=0;//Initial state of state machine
	wanted_dac_voltage=0;//Make shure minimum voltage outout
	last_blf=1.0;
	trickle_current_mA=battery_nominal_capacity_mA/TRICKLE_CURRENT_RATIO;
	//ChargingTimeMillis=0;
	//LCD
	lcd.begin(16,2);
	lcd.backlight();
	lcd.clear();
	update_lcd(F("Power up..."),empty_string);
	delay(1000);
}



float log_battery_voltage(){
	//Log only once per second
	if (log_last_second==second()) return last_blf;
	else log_last_second=second();
	
	sec_log[sec_index]=battery_voltage;
	if (sec_index<59) {
		sec_index++;
	}
	else {//If one minute logged
		//Calculate avetage per minute
		if (min_index>=MINUTES_TO_LOG) min_index=0;
		
		sec_index=0;
		float sum_v=0;
		for (i=0;i<60;i++){
			sum_v+=sec_log[i];
		}
		float min_average=sum_v/60.0;
		
		for(i=1;i<MINUTES_TO_LOG;i++) min_log[i-1]=min_log[i];
		
		min_log[MINUTES_TO_LOG-1]=min_average;//Save minute average
		
		//Serial.print(min_index);
		//Serial.print(" ");
		//Serial.print(min_average);
		//Serial.print(" arr: ");
		//Serial.print(min_log[0]);
		//Serial.print(" ");
		//Serial.print(min_log[1]);
		//Serial.print(" ");
		//Serial.print(min_log[2]);
		//Serial.print(" ");
		//Serial.print(min_log[3]);
		//Serial.print(" ");
		//Serial.print(min_log[4]);
		last_blf=best_linear_fit(min_log);
		//Serial.print("Slope ");
		//Serial.println(last_blf);
		min_index++;
		total_minutes_average++;
	}
	return last_blf;
}

I've just deleted two near-identical reposts of this topic.

Don't do it again.

Try this
http://playground.arduino.cc/uploads/Code/Time.zip

thanks but this library not work in this code
so any idea

Divya20:
thanks but this library not work in this code
so any idea

How about you define what "not work" means in this context, and how about you post actual code and error messages?
Just a thought.

Hi,
What IDE version are yo using?
What model Arduino controller are you using?

Thanks... Tom... :slight_smile:

Arduino 1.0.6 version i used

Hi,
Try a later version 1.5.something or 1.6.something

Tom... :slight_smile: