View difference between Paste ID: QykHAXwU and 5hb5kjEL
SHOW: | | - or go back to the newest paste.
1
//Ratios between voltage dividers
2
// R# = (R1+R2)/R2
3
float R1 = 3.128;
4
float R2 = 6.255;
5
float R3 = 9.383;
6
float R4 = 12.511;
7
float R5 = 15.638;
8
float R6 = 18.766;
9
float R7 = 21.894;
10
float R8 = 25.021;
11
12
// % Variables
13
int P8 = 0;
14
int P7 = 0;
15
int P6 = 0;
16
int P5 = 0;
17
int P4 = 0;
18
int P3 = 0;
19
int P2 = 0;
20
int P1 = 0;
21
22
long readVcc() {
23
  // Read 1.1V reference against AVcc
24
  // set the reference to Vcc and the measurement to the internal 1.1V reference
25
#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
26
  ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
27
#elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
28
  ADMUX = _BV(MUX5) | _BV(MUX0) ;
29
#else
30
  ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
31
#endif
32
33
  delay(2); // Wait for Vref to settle
34
  ADCSRA |= _BV(ADSC); // Start conversion
35
  while (bit_is_set(ADCSRA, ADSC)); // measuring
36
37
  uint8_t low  = ADCL; // must read ADCL first - it then locks ADCH
38
  uint8_t high = ADCH; // unlocks both
39
40
  long result = (high << 8) | low;
41
42
  result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
43
  return result; // Vcc in millivolts
44
}
45
46
void setup() {
47
  // initialize serial port
48
  Serial.begin(9600);
49
}
50
51
void loop() {
52
53
  
54
  //Battery Voltage
55
  float B1Voltage = 0;
56
  float B2Voltage = 0;
57
  float B3Voltage = 0;
58
  float B4Voltage = 0;
59
  float B5Voltage = 0;
60
  float B6Voltage = 0;
61
  float B7Voltage = 0;
62
  float B8Voltage = 0;
63
64
65
  //converted signal in 5v domain
66
  float RealBat1V = 0;
67
  float RealBat2V = 0;
68
  float RealBat3V = 0;
69
  float RealBat4V = 0;
70
  float RealBat5V = 0;
71
  float RealBat6V = 0;
72
  float RealBat7V = 0;
73
  float RealBat8V = 0;
74
75
76
  //Analog channel raw value
77
  float VolBat1 = 0;
78
  float VolBat2 = 0;
79
  float VolBat3 = 0;
80
  float VolBat4 = 0;
81
  float VolBat5 = 0;
82
  float VolBat6 = 0;
83
  float VolBat7 = 0;
84
  float VolBat8 = 0;
85
86
  float vccValue = readVcc() / 1000.0; //Arduino refence voltage
87
88
89
  // Reading battery voltages
90
  VolBat8 = analogRead(A8);
91
  VolBat7 = analogRead(A7);
92
  VolBat6 = analogRead(A6);
93
  VolBat5 = analogRead(A5);
94
  VolBat4 = analogRead(A4);
95
  VolBat3 = analogRead(A3);
96
  VolBat2 = analogRead(A2);
97
  VolBat1 = analogRead(A1);
98
99
100
  //Converting raw value in 5v domian
101
  RealBat1V = VolBat1 * vccValue / 1024.0;
102
  RealBat2V = VolBat2 * vccValue / 1024.0;
103
  RealBat3V = VolBat3 * vccValue / 1024.0;
104
  RealBat4V = VolBat4 * vccValue / 1024.0;
105
  RealBat5V = VolBat5 * vccValue / 1024.0;
106
  RealBat6V = VolBat6 * vccValue / 1024.0;
107
  RealBat7V = VolBat7 * vccValue / 1024.0;
108
  RealBat8V = VolBat8 * vccValue / 1024.0;
109
110
111
  //Calculating actual voltages
112
  B8Voltage = RealBat4V * R8;
113
  B7Voltage = RealBat3V * R7 - B8Voltage;
114
  B6Voltage = RealBat2V * R6 - B8Voltage - B7Voltage;
115
  B5Voltage = RealBat1V * R5 - B8Voltage - B7Voltage - B6Voltage;
116
  B4Voltage = RealBat4V * R4 - B8Voltage - B7Voltage - B6Voltage - B5Voltage;
117
  B3Voltage = RealBat3V * R3 - B8Voltage - B7Voltage - B6Voltage - B5Voltage - B4Voltage;
118
  B2Voltage = RealBat2V * R2 - B8Voltage - B7Voltage - B6Voltage - B5Voltage - B4Voltage - B3Voltage;
119
  B1Voltage = RealBat1V * R1 - B8Voltage - B7Voltage - B6Voltage - B5Voltage - B4Voltage - B3Voltage - B2Voltage;
120
121
122
  //Convert to %
123
  // map(value, fromLow, fromHigh, toLow, toHigh)
124
  P8 = map(B8Voltage, 0, 12, 0, 100);
125
  P7 = map(B7Voltage, 0, 12, 0, 100);
126
  P6 = map(B6Voltage, 0, 12, 0, 100);
127
  P5 = map(B5Voltage, 0, 12, 0, 100);
128
  P4 = map(B4Voltage, 0, 12, 0, 100);
129
  P3 = map(B3Voltage, 0, 12, 0, 100);
130
  P2 = map(B2Voltage, 0, 12, 0, 100);
131
  P1 = map(B1Voltage, 0, 12, 0, 100);
132
133
  
134
  //Print voltages on serial monitor
135
  Serial.print("Battery-1 Voltage=");
136
  Serial.println(B1Voltage);
137
  Serial.print("Battery-2 Voltage=");
138
  Serial.println(B2Voltage);
139
  Serial.print("Battery-3 Voltage=");
140
  Serial.println(B3Voltage);
141
  Serial.print("Battery-4 Voltage=");
142
  Serial.println(B4Voltage);
143
  Serial.print("Battery-5 Voltage=");
144
  Serial.println(B5Voltage);
145
  Serial.print("Battery-6 Voltage=");
146
  Serial.println(B6Voltage);
147
  Serial.print("Battery-7 Voltage=");
148
  Serial.println(B7Voltage);
149
  Serial.print("Battery-8 Voltage=");
150
  Serial.println(B8Voltage);
151
152
  delay(2000); //2 Seconds delay and then start again
153
}