#include <Wire.h>
#include <Adafruit_INA3221.h>

Adafruit_INA3221 ina3221;

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    delay(10); // wait for serial monitor
  }

  Serial.println("Initializing INA3221...");
  if (!ina3221.begin()) {
    Serial.println("Failed to find INA3221 chip");
    while (1);
  }
  Serial.println("Found INA3221 chip");

  // By default the INA3221 is in continuous mode with a range of +-320 milliamps.
  // These settings can be adjusted using the setCalibration() and setConversionMode() functions.
  ina3221.setCalibration_32V_2A();
}

void loop() {
  // Read the current and voltage values from all three channels
  float shuntvoltage1 = 0;
  float busvoltage1 = 0;
  float current_mA1 = 0;
  float loadvoltage1 = 0;
  float shuntvoltage2 = 0;
  float busvoltage2 = 0;
  float current_mA2 = 0;
  float loadvoltage2 = 0;
  float shuntvoltage3 = 0;
  float busvoltage3 = 0;
  float current_mA3 = 0;
  float loadvoltage3 = 0;

  shuntvoltage1 = ina3221.getShuntVoltage_mV(1);
  busvoltage1 = ina3221.getBusVoltage_V(1);
  current_mA1 = ina3221.getCurrent_mA(1);
  loadvoltage1 = busvoltage1 + (shuntvoltage1 / 1000);

  shuntvoltage2 = ina3221.getShuntVoltage_mV(2);
  busvoltage2 = ina3221.getBusVoltage_V(2);
  current_mA2 = ina3221.getCurrent_mA(2);
  loadvoltage2 = busvoltage2 + (shuntvoltage2 / 1000);

  shuntvoltage3 = ina3221.getShuntVoltage_mV(3);
  busvoltage3 = ina3221.getBusVoltage_V(3);
  current_mA3 = ina3221.getCurrent_mA(3);
  loadvoltage3 = busvoltage3 + (shuntvoltage3 / 1000);

  // Print the readings
  Serial.print("Channel 1: ");
  Serial.print("Bus Voltage: "); Serial.print(busvoltage1); Serial.print(" V ");
  Serial.print("Shunt Voltage: "); Serial.print(shuntvoltage1); Serial.print(" mV ");
  Serial.print("Load Voltage: "); Serial.print(loadvoltage1); Serial.print(" V ");
  Serial.print("Current: "); Serial.print(current_mA1); Serial.println(" mA");
  Serial.print("Channel 2: ");
  Serial.print("Bus Voltage: "); Serial.print(busvoltage2); Serial.print(" V ");
  Serial.print("Shunt Voltage: "); Serial.print(shuntvoltage2); Serial.print(" mV ");
  Serial.print("Load Voltage: "); Serial.print(loadvoltage2); Serial.print(" V ");
  Serial.print("Current: "); Serial.print(current_mA2); Serial.println(" mA");
  Serial.print("Channel 3: ");
  Serial.print("Bus Voltage: "); Serial.print(busvoltage3); Serial.print(" V ");
  Serial.print("Shunt Voltage: "); Serial.print(shuntvoltage3); Serial.print(" mV ");
  Serial.print("Load Voltage: "); Serial.print(loadvoltage3); Serial.print(" V ");
  Serial.print("Current: "); Serial.print(current_mA3); Serial.println(" mA");
  Serial.println();

  delay(1000); // Delay for readability
}
nano:12
nano:11
nano:10
nano:9
nano:8
nano:7
nano:6
nano:5
nano:4
nano:3
nano:2
nano:GND.2
nano:RESET.2
nano:0
nano:1
nano:13
nano:3.3V
nano:AREF
nano:A0
nano:A1
nano:A2
nano:A3
nano:A4
nano:A5
nano:A6
nano:A7
nano:5V
nano:RESET
nano:GND.1
nano:VIN
nano:12.2
nano:5V.2
nano:13.2
nano:11.2
nano:RESET.3
nano:GND.3