I am developing a program to log and eventually store data and I am utilizing an array, but some of my data is not being stored. Specifically the current and volts are not being stored in the array, the max9611_addr is being stored in the array just fine. Any thoughts would be very helpful. Thank you.
#include <MAX9611.h>
#include <Wire.h>
/*
Analog pins 4 (SDA),5(SCL)
*/
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
int msb;
int lsb;
int lcd_disp[16][3] = {
0};
double volts;
double current;
byte max9611_addr;
int current_sense_gain;
void setup()
{
lcd.begin(16, 2);
Serial.begin(9600);
delay(1); //wait for serial bus to settle
}
void loop()
{
lcd.setCursor(0,1);
while (1) {
Wire.begin();
for (max9611_addr = 0x70; max9611_addr <= 0x7F; max9611_addr++) //scan I2c bus for potential MAX9611 devices
{
Wire.beginTransmission (max9611_addr);
if (Wire.endTransmission () == 0)
{
volts = get_MAX9611_voltage(max9611_addr);//get the voltage for this device the parameter is the address
lcd.setCursor(3,1);
lcd.print(volts, 2); //print the current with 2 decimal places
lcd.print(" V");
current_sense_gain = 1;
current = get_MAX9611_current(max9611_addr, current_sense_gain); //get the current for this device (the first parameter is the address and the second{future} is the gain)
// if(volts == 0)
// {
// current = 0;
// }
lcd.setCursor(3,0);
lcd.print(current, 6); //print the current with 4 decimal places
lcd.print(" A");
lcd.setCursor(0,0);
lcd.print(max9611_addr<<1,HEX); //print address
switch (max9611_addr) {
case 0x70:
lcd_disp[0][0] = max9611_addr;
lcd_disp[0][1] = current;
lcd_disp[0][2] = volts;
break;
case 0x71:
lcd_disp[1][0] = max9611_addr;
lcd_disp[1][1] = current;
lcd_disp[1][2] = volts;
break;
case 0x72:
lcd_disp[2][0] = max9611_addr;
lcd_disp[2][1] = current;
lcd_disp[2][2] = volts;
break;
case 0x73:
lcd_disp[3][0] = max9611_addr;
lcd_disp[3][1] = current;
lcd_disp[3][2] = volts;
break;
case 0x74:
lcd_disp[4][0] = max9611_addr;
lcd_disp[4][1] = current;
lcd_disp[4][2] = volts;
break;
case 0x75:
lcd_disp[5][0] = max9611_addr;
lcd_disp[5][1] = current;
lcd_disp[5][2] = volts;
break;
case 0x76:
lcd_disp[6][0] = max9611_addr;
lcd_disp[6][1] = current;
lcd_disp[6][2] = volts;
break;
case 0x77:
lcd_disp[7][0] = max9611_addr;
lcd_disp[7][1] = current;
lcd_disp[7][2] = volts;
break;
case 0x78:
lcd_disp[8][0] = max9611_addr;
lcd_disp[8][1] = current;
lcd_disp[8][2] = volts;
break;
case 0x79:
lcd_disp[9][0] = max9611_addr;
lcd_disp[9][1] = current;
lcd_disp[9][2] = volts;
break;
case 0x7A:
lcd_disp[10][0] = max9611_addr;
lcd_disp[10][1] = current;
lcd_disp[10][2] = volts;
break;
case 0x7B:
lcd_disp[11][0] = max9611_addr;
lcd_disp[11][1] = current;
lcd_disp[11][2] = volts;
break;
case 0x7C:
lcd_disp[12][0] = max9611_addr;
lcd_disp[12][1] = current;
lcd_disp[12][2] = volts;
break;
case 0x7D:
lcd_disp[13][0] = max9611_addr;
lcd_disp[13][1] = current;
lcd_disp[13][2] = volts;
break;
case 0x7E:
lcd_disp[14][0] = max9611_addr;
lcd_disp[14][1] = current;
lcd_disp[14][2] = volts;
break;
case 0x7F:
lcd_disp[15][0] = max9611_addr;
lcd_disp[15][1] = current;
lcd_disp[15][2] = volts;
break;
}
Serial.print (lcd_disp[0][0]<<1,HEX);
Serial.print (" ");
Serial.print (current = (float)lcd_disp[0][1],6);
Serial.print (" ");
Serial.print (volts = (float)lcd_disp[0][2],2);
Serial.print ("\n");
}
}
delay (1); // maybe unneeded?
} // end of good response
} // end of for loop
/*********************************************************************
* FileName: MAX9611.h
* Dependencies: None
* Overview: Defines function prototypes, I2C address, global
* variables.
********************************************************************/
// ***************** User-configurable parameters ********************
// main voltage range
#define voltage_max 56.3
#define main_voltage_min 1.0
// main current range
#define current_max 10000
#define current_min 0
// I2C addresses
//From function The address comes from the function, which allows many different //address MAX9611 to be used
// Sense resistor values
#define sense_resistor 0.05
// *******************************************************************
// Function prototypes
double get_MAX9611_voltage(int address);
double get_MAX9611_current(int address, int gain);
/*********************************************************************
* FileName: MAX9611.cpp
* Dependencies: MAX9611.h (include here and in main)
* Wire.h (include here and in main)
********************************************************************/
/*********************************************************************
* Function: void get_MAX9611_voltage (void),
* void get_MAX9611_current (void)
* PreCondition: None.
* Input: None.
* Output: None.
* Side Effects: None.
* Overview: Function for obtaining MAX9611 voltage via I2C
* bus.
*
********************************************************************/
#include "MAX9611.h"
#include "Wire.h"
double MAX9611_voltage;
double MAX9611_current;
int address;
int gain;
int result;
double get_MAX9611_voltage(int address){
int msb;
int lsb;
float multiplier = 0.014; //voltage sense LSB
Wire.beginTransmission(address);
Wire.write(0x0A);
Wire.write(0x03);
Wire.endTransmission();
Wire.beginTransmission(address);
Wire.write(0x02);
Wire.endTransmission();
Wire.requestFrom(address,2);
msb = Wire.read();
lsb = Wire.read();
Wire.endTransmission();
MAX9611_voltage = ((msb)<<4)+(lsb>>4);
MAX9611_voltage = (MAX9611_voltage*multiplier);
return MAX9611_voltage;
}
double get_MAX9611_current(int address, int gain){
int msb;
int lsb;
float multiplier; //calculation multiplier
int reg_add; //register address
switch (gain) {
case 1: //gain of 1
reg_add = 0x00;
multiplier = 0.0001075;
break;
case 4: //gain of 4
reg_add = 0x01;
multiplier = 0.00002688;
break;
case 8: //gain of 8
reg_add = 0x02;
multiplier = 0.00001344;
break;
default: //gain of 1
reg_add = 0x00;
multiplier = 0.0001075;
}
Wire.beginTransmission(address);
Wire.write(0x0A);
Wire.write(reg_add);
Wire.endTransmission();
Wire.beginTransmission(address);
Wire.write(0x00);
Wire.endTransmission();
Wire.requestFrom(address,2);
msb = Wire.read();
lsb = Wire.read();
Wire.endTransmission();
result = (msb<<4)+(lsb>>4);
MAX9611_current = ((result*multiplier)/0.05);
return MAX9611_current;
}