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

--- a/LipoStorage/LipoStorage.ino
+++ b/LipoStorage/LipoStorage.ino
@@ -8,17 +8,21 @@
 
 #include <Wire.h>
 
-#define RELAY 4
+#define VOLTAGEPIN 1
+#define RELAYPIN 4
 int debug = 1;
 
-int analogValue;
 float voltage;
-float minimumVoltage = 11.98;
+float minimumVoltage = 3.85;
 int timesUnderMinimumVoltage = 0;
-int maxUnderMinimumVoltage = 5;
+int maxUnderMinimumVoltage = 10;
 bool relayActivated = false;
 
-void setup() 
+int i = 0, totalVoltages=120;
+bool passTotalVotages = false;
+float voltages[120];
+
+void setup()
 {
   if ( debug ) {
     Serial.begin(9600);
@@ -26,23 +30,32 @@
   if ( debug >= 3 ) {
     Serial.println("Voltage: x.xx V");
   }
-  pinMode(RELAY, OUTPUT);
+  pinMode(RELAYPIN, OUTPUT);
 }
 
 void loop() 
 {
-  analogValue=analogRead(1);
+  int analogValue;
+  float voltageTemp;
+  analogValue=analogRead(VOLTAGEPIN);
   if ( debug >= 3 ) {
     Serial.print("analogValue : ");
     Serial.println(analogValue);
   }
   
-  voltage=analogValue/4.092/10;  
+  voltageTemp=analogValue/4.092/10;  
   if ( debug >= 1 ) {
     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.println(" V");
-  }
+  }  
   
   if ( relayActivated == false ) {
     if ( voltage <= minimumVoltage ) {
@@ -51,31 +64,53 @@
         Serial.print("timesUnderMinimumVoltage : ");
         Serial.println(timesUnderMinimumVoltage);
       }
+    } else if ( voltage > minimumVoltage && timesUnderMinimumVoltage > 0 ) {
+      timesUnderMinimumVoltage--;      
+      if ( debug >= 2) {
+        Serial.print("timesUnderMinimumVoltage : ");
+        Serial.println(timesUnderMinimumVoltage);
+      }      
     }
     if ( timesUnderMinimumVoltage >= maxUnderMinimumVoltage ) {
       relayActivated = true;
-      digitalWrite(RELAY, HIGH);
+      digitalWrite(RELAYPIN, HIGH);
       if ( debug >= 1 ) {
         Serial.println("Send signal to relay to disconnect battery");
       }
     }    
   } else {
+    if ( debug >= 2) {
+      Serial.print("timesUnderMinimumVoltage : ");
+      Serial.println(timesUnderMinimumVoltage);
+    }
     if ( voltage > minimumVoltage && timesUnderMinimumVoltage > 0 ) {
       timesUnderMinimumVoltage--;
-      if ( debug >= 2) {
-        Serial.print("timesUnderMinimumVoltage : ");
-        Serial.println(timesUnderMinimumVoltage);
-      }
     }
     if ( timesUnderMinimumVoltage == 0 ) {
       relayActivated = false;
-      digitalWrite(RELAY, LOW);
+      digitalWrite(RELAYPIN, LOW);
       if ( debug >= 1 ) {
         Serial.println("Send signal to relay to reconnect battery");
       }      
     }
   }
-  
+  i++;
+  if (i>totalVoltages) {
+    i = 0;
+    passTotalVotages = true;
+  }  
   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