From: Razvan Stanga Date: Fri, 24 Oct 2014 10:51:32 +0000 Subject: calculate average voltage for the last 120 values for better accuracy X-Git-Url: https://git.razvi.ro/?p=arduino%2Farduino-lipo-discharger.git&a=commitdiff&h=841cac6d452bd7979803ca6b8faeb83f5a1b6e84 --- calculate average voltage for the last 120 values for better accuracy --- --- a/LipoStorage/LipoStorage.ino +++ b/LipoStorage/LipoStorage.ino @@ -8,17 +8,21 @@ #include -#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