#define bat_volt_sense 0 //defining the analog pin A1 to read battery voltage
#define AVG_NUM 10
#define offsetVoltage 2.5 // for ACS712 sensor
#define Sensitivity 0.066 // 185mV/A for ACS712-5A variant
#define gradDetect 26.3157894373 // Gradient for SoC Detect
#define Vb_FULL_SCALE 13.8 //SoC 100% Baterai 13.8 Volt
unsigned long last_time=0;
unsigned long current_time=0;
float bat_volt=0; // solar panel voltage
float bat_volt_k_1 =0;
float SoC_Detect = 0;
float temp_SoC_Detect =0;
float SoCCorr_k_1 =0;
float SoC_VB=0;
//**************SoC Detect*************************************************
double SOC_Detect()
{
SoC_Detect = (gradDetect*(bat_volt - 10.0));
return SoC_Detect;
}
void SoC_Estimator(float SOCCOOR_K_1, float BAT_VOLT_K_1, float BAT_VOLT,float *SOC_VB)
{
*SOC_VB = SOCCOOR_K_1 + ((BAT_VOLT - BAT_VOLT_K_1)/(Vb_FULL_SCALE - BAT_VOLT_K_1 )*(100 - SOCCOOR_K_1));
}
int read_adc(int channel){
int sum = 0;
int temp;
int i;
for (i=0; i<AVG_NUM; i++) { // loop through reading raw adc values AVG_NUM number of times
temp = analogRead(channel); // read the input pin
sum += temp; // store sum for averaging
delayMicroseconds(50); // pauses for 50 microseconds
}
return(sum / AVG_NUM); // divide sum by AVG_NUM to get average and return it
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
bat_volt= read_adc(bat_volt_sense)*0.0048828*(37.5/7.5);
temp_SoC_Detect = SOC_Detect();
if(temp_SoC_Detect < 0)
{
temp_SoC_Detect = 0;
}
current_time = millis();
if(current_time - last_time >= 1000)
{
bat_volt_k_1 = bat_volt;
SoCCorr_k_1 = temp_SoC_Detect;
last_time = current_time;
}
SoC_Estimator(SoCCorr_k_1,bat_volt_k_1,bat_volt,&SoC_VB);
Serial.print(bat_volt);
Serial.print("; ");
Serial.print(bat_volt_k_1);
Serial.print("; ");
Serial.print(temp_SoC_Detect);
Serial.print("; ");
Serial.print(SoCCorr_k_1);
Serial.print("; ");
Serial.print(SoC_VB);
Serial.print("\n");
}