calculate average voltage for the last 120 values for better accuracy master
[arduino/arduino-lipo-discharger.git] / LipoStorage / LipoStorage.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* Arduino lipo battery discharger to storage minimumVoltage=3.85v
* For this you need :
* - 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
* - 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>

#define VOLTAGEPIN 1
#define RELAYPIN 4
int debug = 1;

float voltage;
float minimumVoltage = 3.85;
int timesUnderMinimumVoltage = 0;
int maxUnderMinimumVoltage = 10;
bool relayActivated = false;

int i = 0, totalVoltages=120;
bool passTotalVotages = false;
float voltages[120];

void setup()
{
  if ( debug ) {
    Serial.begin(9600);
  }
  if ( debug >= 3 ) {
    Serial.println("Voltage: x.xx V");
  }
  pinMode(RELAYPIN, OUTPUT);
}

void loop() 
{
  int analogValue;
  float voltageTemp;
  analogValue=analogRead(VOLTAGEPIN);
  if ( debug >= 3 ) {
    Serial.print("analogValue : ");
    Serial.println(analogValue);
  }
  
  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 ) {
      timesUnderMinimumVoltage++;
      if ( debug >= 2) {
        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(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 ( timesUnderMinimumVoltage == 0 ) {
      relayActivated = false;
      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