calculate average voltage for the last 120 values for better accuracy master
[arduino/arduino-lipo-discharger.git] / LipoStorage / LipoStorage.ino
1 /**
2 * Arduino lipo battery discharger to storage minimumVoltage=3.85v
3 * For this you need :
4 * - arduino board. I used arduino micro
5 * - voltage sensor - http://www.aliexpress.com/store/product/10pcs-New-Voltage-Sensor-Module-For-Robot-Arduino/341125_1352801407.html
6 * - relay shield - http://www.aliexpress.com/item/2-Channel-2-channel-2-way-5V-Relay-Shield-Module-for-ARM-PIC-AVR-DSP/1971554348.html
7 */
8
9 #include <Wire.h>
10
Razvan Stanga 11 #define VOLTAGEPIN 1
12 #define RELAYPIN 4
13 int debug = 1;
14
15 float voltage;
16 float minimumVoltage = 3.85;
17 int timesUnderMinimumVoltage = 0;
18 int maxUnderMinimumVoltage = 10;
19 bool relayActivated = false;
20
21 int i = 0, totalVoltages=120;
22 bool passTotalVotages = false;
23 float voltages[120];
24
25 void setup()
26 {
27 if ( debug ) {
28 Serial.begin(9600);
29 }
30 if ( debug >= 3 ) {
31 Serial.println("Voltage: x.xx V");
32 }
33 pinMode(RELAYPIN, OUTPUT);
34 }
35
36 void loop()
37 {
38 int analogValue;
39 float voltageTemp;
40 analogValue=analogRead(VOLTAGEPIN);
41 if ( debug >= 3 ) {
42 Serial.print("analogValue : ");
43 Serial.println(analogValue);
44 }
45
46 voltageTemp=analogValue/4.092/10;
47 if ( debug >= 1 ) {
48 Serial.print("Voltage : ");
49 Serial.print(voltageTemp);
50 Serial.println(" V");
51 }
52 voltages[i] = voltageTemp;
53 voltage = calcAverageVoltage ();
54 if ( debug >= 1 ) {
55 Serial.print("Voltage average : ");
56 Serial.print(voltage);
57 Serial.println(" V");
58 }
59
60 if ( relayActivated == false ) {
61 if ( voltage <= minimumVoltage ) {
62 timesUnderMinimumVoltage++;
63 if ( debug >= 2) {
64 Serial.print("timesUnderMinimumVoltage : ");
65 Serial.println(timesUnderMinimumVoltage);
66 }
67 } else if ( voltage > minimumVoltage && timesUnderMinimumVoltage > 0 ) {
68 timesUnderMinimumVoltage--;
69 if ( debug >= 2) {
70 Serial.print("timesUnderMinimumVoltage : ");
71 Serial.println(timesUnderMinimumVoltage);
72 }
73 }
74 if ( timesUnderMinimumVoltage >= maxUnderMinimumVoltage ) {
75 relayActivated = true;
76 digitalWrite(RELAYPIN, HIGH);
77 if ( debug >= 1 ) {
78 Serial.println("Send signal to relay to disconnect battery");
79 }
80 }
81 } else {
82 if ( debug >= 2) {
83 Serial.print("timesUnderMinimumVoltage : ");
84 Serial.println(timesUnderMinimumVoltage);
85 }
86 if ( voltage > minimumVoltage && timesUnderMinimumVoltage > 0 ) {
87 timesUnderMinimumVoltage--;
88 }
89 if ( timesUnderMinimumVoltage == 0 ) {
90 relayActivated = false;
91 digitalWrite(RELAYPIN, LOW);
92 if ( debug >= 1 ) {
93 Serial.println("Send signal to relay to reconnect battery");
94 }
95 }
96 }
97 i++;
98 if (i>totalVoltages) {
99 i = 0;
100 passTotalVotages = true;
101 }
102 delay(500);
103 }
104
105 float calcAverageVoltage () {
106 int _j; float _sum = 0.0;
107 for (_j=0;_j<totalVoltages;_j++) {
108 _sum+= (float) voltages[_j];
109 }
110 if ( i < totalVoltages && passTotalVotages == false ) {
111 return _sum/i;
112 } else {
113 return _sum/totalVoltages;
114 }
115 }
116
comments