-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcurrent.ino
52 lines (39 loc) · 1.35 KB
/
current.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
#include <Wire.h>
#include <Adafruit_INA219.h>
Adafruit_INA219 ina219;
void setupCurrent(void)
{
// Initialize the INA219.
// By default the initialization will use the largest range (32V, 2A). However
// you can call a setCalibration function to change this range (see comments).
ina219.begin();
// To use a slightly lower 32V, 1A range (higher precision on amps):
//ina219.setCalibration_32V_1A();
// Or to use a lower 16V, 400mA range (higher precision on volts and amps):
//ina219.setCalibration_16V_400mA();
//Serial.println("Measuring voltage and current with INA219 ...");
for( int i = 0; i < U8_Width; i ++ )
currentLog[i] = 100;
}
void loopCurrent(void)
{
float shuntvoltage = 0;
float busvoltage = 0;
shuntvoltage = ina219.getShuntVoltage_mV();
busvoltage = ina219.getBusVoltage_V();
current_mA = ina219.getCurrent_mA();
loadvoltage = busvoltage + (shuntvoltage / 1000);
smoothedCurrent_mA = (1.0-currentSmoother) * smoothedCurrent_mA + currentSmoother * current_mA;
long now = millis();
if( now - lastCurrentLog > currentLogInterval)
{
// shift array down
for( int i = 0; i < U8_Width - 1; i ++ )
currentLog[i] = currentLog[i+1];
// add new entry
currentLog[U8_Width - 1] = smoothedCurrent_mA;
lastCurrentLog = now;
setCurrentGraphDirty();
numCurrentSamples++;
}
}