initial commit
[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
11 int debug = 1;
12
13 int analogValue;
14 float voltage;
15 float minimumVoltage = 3.85;
16 int timesUnderMinimumVoltage = 0;
17 int maxUnderMinimumVoltage = 5;
18 bool relayActivated = false;
19
20 void setup()
21 {
22 if ( debug ) {
23 Serial.begin(9600);
24 }
25 if ( debug >= 3 ) {
26 Serial.println("Voltage: x.xx V");
27 }
28 }
29
30 void loop()
31 {
32 analogValue=analogRead(1);
33 if ( debug >= 3 ) {
34 Serial.print("analogValue : ");
35 Serial.println(analogValue);
36 }
37
38 voltage=analogValue/4.092/10;
39 if ( debug >= 1 ) {
40 Serial.print("Voltage : ");
41 Serial.print(voltage);
42 Serial.println(" V");
43 }
44
45 if ( relayActivated == false ) {
46 if ( voltage <= minimumVoltage ) {
47 timesUnderMinimumVoltage++;
48 if ( debug >= 2) {
49 Serial.print("timesUnderMinimumVoltage : ");
50 Serial.println(timesUnderMinimumVoltage);
51 }
52 }
53 if ( timesUnderMinimumVoltage >= maxUnderMinimumVoltage ) {
54 relayActivated = true;
55 if ( debug >= 1 ) {
56 Serial.println("Send signal to relay to disconnect battery");
57 // to do relay part
58 }
59 }
60 } else {
61 if ( voltage > minimumVoltage && timesUnderMinimumVoltage > 0 ) {
62 timesUnderMinimumVoltage--;
63 if ( debug >= 2) {
64 Serial.print("timesUnderMinimumVoltage : ");
65 Serial.println(timesUnderMinimumVoltage);
66 }
67 }
68 if ( timesUnderMinimumVoltage == 0 ) {
69 relayActivated = false;
70 if ( debug >= 1 ) {
71 Serial.println("Send signal to relay to reconnect battery");
72 // to do relay part
73 }
74 }
75 }
76
77 delay(500);
78 }
79
comments