-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurrent_loop.ino
More file actions
44 lines (33 loc) · 1.09 KB
/
Copy pathcurrent_loop.ino
File metadata and controls
44 lines (33 loc) · 1.09 KB
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
void current_loop() {
currentArray[0] = ina219.shuntCurrent() * 1000;
voltageArray[0] = ina219.busVoltage();
for (int i = 0; i < 9; i++) {
cTotal = cTotal + currentArray[i];
vTotal = vTotal + voltageArray[i] ;
currentArray[i + 1] = currentArray[i];
voltageArray[i + 1] = voltageArray[i];
}
inaCurrent = cTotal / 10;
inaVoltage = vTotal / 10;
cTotal = 0;
vTotal = 0;
inaCurrent = ina219.shuntCurrent() * 1000;
inaVoltage = ina219.busVoltage();
if (inaCurrent < currentTarget) {
//Serial.print("Going up ");
//Serial.print("Current: "); Serial.print(current_mA); Serial.println(" mA");
pulseWidth++;
if (pulseWidth > 1023) pulseWidth = 1023;
if (inaVoltage < 2.8) pulseWidth = 0;
}
else if (inaCurrent > currentTarget) {
///Serial.print("Going down");
//Serial.print("Current: "); Serial.print(current_mA); Serial.println(" mA");
pulseWidth--;
if (pulseWidth < 0) pulseWidth = 0;
if (inaVoltage < 2.8) pulseWidth = 0;
}
analogWrite(pwmPin, pulseWidth);
ina219.recalibrate();
ina219.reconfig();
}