I have removed the Serial.println in Battery::getData() and changed the order so ads1115.begin() is being called before ads1115.readADC_SingleEnded(0). Here is my new class:
#include "Battery.h"
// Initilisation function for battery class.
// Params:
// float pdFactor - factor to convert divided voltage to actual voltage. Needs to be measured manually.
// int averagingWindow - How many samples to use when averaging
// int voltagePin - Pin number for reading voltage (Must be an analog pin)
// int currentPin - Pin number for reading current (Must be an analog pin)
Battery::Battery(float pdFactor, int averagingWindow, int voltagePin, int currentPin) : pdFactor(pdFactor), averagingWindow(averagingWindow), voltagePin(voltagePin), currentPin(currentPin) {
ads1115.begin();
update();
currentScale = 0.5f / current_v;
}
// Updates voltage and current variables.
void Battery::update() {
getData();
}
// Averages ADC data of a window of averagingWindow size, and then updates voltage and current class variables.
void Battery::getData() {
ads1115.readADC_SingleEnded(0);
// averaging loop
for (int i = 0; i <= averagingWindow; i++) {
voltage_adc += analogRead(voltagePin);
current_adc += analogRead(currentPin);
}
// convert ADC reading into voltage
// voltage_adc and current_adc are being converted to floats to hopefully get better accuracy from this calculation. Untested!!
voltage = ((float)voltage_adc / averagingWindow) * (5.0 / 1023.0) * pdFactor;
current_v = ((float)current_adc / averagingWindow) * (5.0 / 1023.0) * currentScale;
// reset ADC variables
voltage_adc = 0;
current_adc = 0;
// Voltage range checks
// --------------------
// if ACS770 voltage < 0.5 something has gone wrong, so clamp to 0.5V if voltage goes below
if (current_v < 0.5f) {
current_v = 0.5f;
}
// if battery voltage goes below 10V (assuming its a 4S battery) something has gone wrong.
if (voltage < 10.0f) {
//Serial.println("Battery voltage below 10V. Check connections.");
}
// if battery voltage goes below 0V something has gone wrong, so clamp to 0V if it goes below. ADC is broken if this goes below 0.
if (voltage < 0.0f) {
//Serial.println("Battery voltage below 0V. Something has gone wrong!");
voltage = 0.0f;
}
// convert current sense voltage into current using mV/A factor found in ACS770 datasheet.
// Take away 0.5 from current sense voltage as 0.5V is voltage at 0A.
current = (current_v - 0.5f) / 40e-3;
}
This however is still causing the same issue, with nothing being sent out from the Arduino. As for the public class variables I have made them private apart from voltage and current, which I trust myself only to read.