Hi All,
I'm trying to code up a little voltage / current monitor that will monitor 3 voltages. For the prototyping stages I'm using the Adafruit INI219 breakout boards.
Because it will make the code more consise I'd like to have some generic read the sensor, print the value code. To this end I have created a typedefed struct containing the Adafruit_INI219 object for the sensor and the last read voltage and current (as integers)). I have then made an array of these, so that I can just pass the sensor number to my functions and have it select the right one from the array.
However when I do this the first access to the first sensor just seems to casue the board to reset. I'm guessing because the pointer to the Adafruit_INI219 is uninitialized. So basically how should I go about initializing the Adafruit_INI219 objects.
Code included below :
//------------------------------- librarys ----------------------------------
#include <Wire.h>
#include <LiquidCrystal_PCF8574.h>
#include <Adafruit_INA219.h>
//------------------------------- lcd ----------------------------------
LiquidCrystal_PCF8574 lcd(0x27);
static int LCD_TxByte(char DataByte, FILE *Stream);
FILE LCDStream; // = FDEV_SETUP_STREAM(LCD_TxByte0,NULL,_FDEV_SETUP_WRITE);
#define LCDPrintAt(xc,yc,format,...) { lcd.setCursor(xc,yc); fprintf_P(&LCDStream,PSTR(format),##__VA_ARGS__); }
//------------------------------- Current probes ------------------------
#define NO_PROBES 3
typedef struct
{
Adafruit_INA219 *probe;
int Voltage;
int Current;
} VIProbe;
VIProbe probes[NO_PROBES];
//------------------------------- input ----------------------------------
#define feedButton 2
#define leftButton 3
#define downButton 4
#define upButton 5
#define rightButton 6
#define auxButton 7
#define DebounceDelay 200
//------------------------------- system settings ----------------------------------
#define LCDLineBuffLen 80
int LCDLine;
void setup()
{
int error;
Serial.begin(9600);
Wire.begin();
Wire.beginTransmission(0x27);
error = Wire.endTransmission();
lcd.begin(20, 4); //LCD columns and rows
lcd.setBacklight(1);
// setup stream for stdio.
fdev_setup_stream(&LCDStream,LCD_TxByte,NULL,_FDEV_SETUP_WRITE);
// 12345678901234567890
LCDPrintAt(0,0,"Dragon Monitor v1.0");
LCDPrintAt(0,1,__DATE__);
LCDPrintAt(0,2,__TIME__);
// Init buttons
pinMode(upButton, INPUT_PULLUP);
pinMode(downButton, INPUT_PULLUP);
pinMode(leftButton, INPUT_PULLUP);
pinMode(rightButton, INPUT_PULLUP);
pinMode(feedButton, INPUT_PULLUP);
InitProbe(0,0x40);
// InitProbe(1,0x41);
// InitProbe(2,0x44);
delay(4000);
lcd.clear();
}
void loop()
{
int probeno;
while (1)
{
for(probeno=0 ; NO_PROBES > probeno; probeno++)
{
ReadProbe(probeno);
DisplayProbe(probeno);
}
LCDPrintAt(0,3,"All done!");
delay(1000);
}
}
void InitProbe(uint8_t probeno,
uint8_t address)
{
if (NO_PROBES > probeno)
{
if (!probes[probeno].probe->begin(address))
LCDPrintAt(0,3,"Probe %d:%d failed",probeno,address)
else
probes[probeno].probe->setCalibration_16V_400mA();
}
else
LCDPrintAt(0,3,"Invalid probe no %d",probeno);
delay(2000);
}
void ReadProbe(int probeno)
{
probes[probeno].Voltage=probes[probeno].probe->getBusVoltage_V();
probes[probeno].Current=probes[probeno].probe->getCurrent_mA();
}
void DisplayProbe(int probeno)
{
int amps;
int miliamps;
amps = probes[probeno].Current / 1000;
miliamps = probes[probeno].Current % 1000;
if (amps > 0)
LCDPrintAt(0,probeno,"%d:%dV %d.%3.3dA",probeno,probes[probeno].Voltage,amps,miliamps)
else
LCDPrintAt(0,probeno,"%d:%dV %dmA",probeno,probes[probeno].Voltage,miliamps)
}
static int LCD_TxByte(char DataByte, FILE *Stream)
{
lcd.write(DataByte);
}