calculate average voltage for the last 120 values for better accuracy master
calculate average voltage for the last 120 values for better accuracy

/** /**
* Arduino lipo battery discharger to storage minimumVoltage=3.85v * Arduino lipo battery discharger to storage minimumVoltage=3.85v
* For this you need : * For this you need :
* - arduino board. I used arduino micro * - arduino board. I used arduino micro
* - voltage sensor - http://www.aliexpress.com/store/product/10pcs-New-Voltage-Sensor-Module-For-Robot-Arduino/341125_1352801407.html * - voltage sensor - http://www.aliexpress.com/store/product/10pcs-New-Voltage-Sensor-Module-For-Robot-Arduino/341125_1352801407.html
* - relay shield - http://www.aliexpress.com/item/2-Channel-2-channel-2-way-5V-Relay-Shield-Module-for-ARM-PIC-AVR-DSP/1971554348.html * - relay shield - http://www.aliexpress.com/item/2-Channel-2-channel-2-way-5V-Relay-Shield-Module-for-ARM-PIC-AVR-DSP/1971554348.html
*/ */
   
#include <Wire.h> #include <Wire.h>
   
#define RELAY 4 #define VOLTAGEPIN 1
  #define RELAYPIN 4
int debug = 1; int debug = 1;
   
int analogValue;  
float voltage; float voltage;
float minimumVoltage = 11.98; float minimumVoltage = 3.85;
int timesUnderMinimumVoltage = 0; int timesUnderMinimumVoltage = 0;
int maxUnderMinimumVoltage = 5; int maxUnderMinimumVoltage = 10;
bool relayActivated = false; bool relayActivated = false;
   
void setup() int i = 0, totalVoltages=120;
  bool passTotalVotages = false;
  float voltages[120];
   
  void setup()
{ {
if ( debug ) { if ( debug ) {
Serial.begin(9600); Serial.begin(9600);
} }
if ( debug >= 3 ) { if ( debug >= 3 ) {
Serial.println("Voltage: x.xx V"); Serial.println("Voltage: x.xx V");
} }
pinMode(RELAY, OUTPUT); pinMode(RELAYPIN, OUTPUT);
} }
   
void loop() void loop()
{ {
analogValue=analogRead(1); int analogValue;
  float voltageTemp;
  analogValue=analogRead(VOLTAGEPIN);
if ( debug >= 3 ) { if ( debug >= 3 ) {
Serial.print("analogValue : "); Serial.print("analogValue : ");
Serial.println(analogValue); Serial.println(analogValue);
} }
voltage=analogValue/4.092/10; voltageTemp=analogValue/4.092/10;
if ( debug >= 1 ) { if ( debug >= 1 ) {
Serial.print("Voltage : "); Serial.print("Voltage : ");
  Serial.print(voltageTemp);
  Serial.println(" V");
  }
  voltages[i] = voltageTemp;
  voltage = calcAverageVoltage ();
  if ( debug >= 1 ) {
  Serial.print("Voltage average : ");
Serial.print(voltage); Serial.print(voltage);
Serial.println(" V"); Serial.println(" V");
} }
if ( relayActivated == false ) { if ( relayActivated == false ) {
if ( voltage <= minimumVoltage ) { if ( voltage <= minimumVoltage ) {
timesUnderMinimumVoltage++; timesUnderMinimumVoltage++;
if ( debug >= 2) { if ( debug >= 2) {
Serial.print("timesUnderMinimumVoltage : "); Serial.print("timesUnderMinimumVoltage : ");
Serial.println(timesUnderMinimumVoltage); Serial.println(timesUnderMinimumVoltage);
} }
  } else if ( voltage > minimumVoltage && timesUnderMinimumVoltage > 0 ) {
  timesUnderMinimumVoltage--;
  if ( debug >= 2) {
  Serial.print("timesUnderMinimumVoltage : ");
  Serial.println(timesUnderMinimumVoltage);
  }
} }
if ( timesUnderMinimumVoltage >= maxUnderMinimumVoltage ) { if ( timesUnderMinimumVoltage >= maxUnderMinimumVoltage ) {
relayActivated = true; relayActivated = true;
digitalWrite(RELAY, HIGH); digitalWrite(RELAYPIN, HIGH);
if ( debug >= 1 ) { if ( debug >= 1 ) {
Serial.println("Send signal to relay to disconnect battery"); Serial.println("Send signal to relay to disconnect battery");
} }
} }
} else { } else {
  if ( debug >= 2) {
  Serial.print("timesUnderMinimumVoltage : ");
  Serial.println(timesUnderMinimumVoltage);
  }
if ( voltage > minimumVoltage && timesUnderMinimumVoltage > 0 ) { if ( voltage > minimumVoltage && timesUnderMinimumVoltage > 0 ) {
timesUnderMinimumVoltage--; timesUnderMinimumVoltage--;
if ( debug >= 2) {  
Serial.print("timesUnderMinimumVoltage : ");  
Serial.println(timesUnderMinimumVoltage);  
}  
} }
if ( timesUnderMinimumVoltage == 0 ) { if ( timesUnderMinimumVoltage == 0 ) {
relayActivated = false; relayActivated = false;
digitalWrite(RELAY, LOW); digitalWrite(RELAYPIN, LOW);
if ( debug >= 1 ) { if ( debug >= 1 ) {
Serial.println("Send signal to relay to reconnect battery"); Serial.println("Send signal to relay to reconnect battery");
} }
} }
} }
  i++;
  if (i>totalVoltages) {
  i = 0;
  passTotalVotages = true;
  }
delay(500); delay(500);
  }
   
  float calcAverageVoltage () {
  int _j; float _sum = 0.0;
  for (_j=0;_j<totalVoltages;_j++) {
  _sum+= (float) voltages[_j];
  }
  if ( i < totalVoltages && passTotalVotages == false ) {
  return _sum/i;
  } else {
  return _sum/totalVoltages;
  }
} }
   
comments