RobTillaart / INA3221_RT

Arduino library for the I2C INA3221 3 channel voltage and current sensor.
MIT License
12 stars 1 forks source link

Slow reacton time bug #4

Closed TPODAvia closed 5 months ago

TPODAvia commented 6 months ago

This code produce a response around 1sec per printing. How to resolve this issue?

#include <Wire.h>
#include "INA3221.h"

// Constants for the number of sensors and their addresses
const uint8_t numSensors = 8;
const uint8_t addresses[numSensors] = {0x40, 0x41, 0x42, 0x43, 0x40, 0x41, 0x42, 0x43};
TwoWire* wires[numSensors] = {&Wire, &Wire, &Wire, &Wire, &Wire1, &Wire1, &Wire1, &Wire1};

// Array to hold sensor instances
INA3221 sensors[numSensors] = {
    INA3221(addresses[0], wires[0]),
    INA3221(addresses[1], wires[1]),
    INA3221(addresses[2], wires[2]),
    INA3221(addresses[3], wires[3]),
    INA3221(addresses[4], wires[4]),
    INA3221(addresses[5], wires[5]),
    INA3221(addresses[6], wires[6]),
    INA3221(addresses[7], wires[7])
};

bool DEBUG = true;

void setup() {
  Serial.begin(115200);
  // Initialize I2C buses
  Wire.begin();
  Wire.setTimeout(1);
  Wire1.begin();
  Wire1.setTimeout(1);

  pinMode(13, OUTPUT);

}

void loop() {
    // INA3221 sensor readings
    String feedback;
    for (int i = 0; i < numSensors; i++) {
        for (int ch = 0; ch < 3; ch++) {
            feedback += String(sensors[i].getCurrent_mA(ch));
            feedback += (i < numSensors - 1) ? "," : ";";
        }
    }

    // Efficient Serial Data Handling
    if (Serial.available() > 0) {
        digitalWrite(13, HIGH); // Example action on serial data available
        while (Serial.available()) {
            char c = Serial.read(); // Read and process each character
            // Process incoming data here
        }
        digitalWrite(13, LOW);
    }

    // Optional: Reduce frequency of serial outputs
    static unsigned long lastPrint = 0;
    unsigned long currentMillis = millis();
    if (currentMillis - lastPrint >= 1000) { // Adjust the interval as needed
        Serial.println(feedback);
        lastPrint = currentMillis;
    }
}
RobTillaart commented 6 months ago

Thanks for the question, Will investigate asap but that can take a few days.

RobTillaart commented 6 months ago

What is the response time you expect/want/need?

The amount of sensors is interesting, may I ask what your application does? Using the sensor in an array might be an informative example.

RobTillaart commented 5 months ago

As there is no answer to my question I assume the issue is solved.