Problem passing reference to initialised ADS1015 object [SOLVED]

Hi all,

To keep my code manageable I've split it in numerous files and libraries, and of course have to pass information around. I'm now running into a big problem passing a reference to the ADS1015 library from my main script into the library where it has to be used.

(sorry, can't post full code - way too big - and I still haven't uploaded it to github).

.ino file snippets:

#include <Adafruit_ADS1015.h>
#include <HydroMonitorMaster.h>

Adafruit_ADS1115 ads1115(0x48);
HydroMonitorMaster master;

void setup() {
  Wire.begin();
  ads1115.begin();

  master.enableThermistor(0, NTCSERIESRESISTOR, THERMISTORNOMINAL, BCOEFFICIENT, TEMPERATURENOMINAL, ADCMAX, &ads1115);
}

Then in HydroMonitorMaster.h:

#include <Adafruit_ADS1015.h>
class HydroMonitorMaster 
{
  public:
    void enableThermistor(unsigned char, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, Adafruit_ADS1115*);
};

and in HydroMonitorMaster.cpp:

void HydroMonitorMaster::enableThermistor(unsigned char sensorPin, unsigned int seriesResistor, unsigned int NTCNominal, unsigned int BCoefficient, unsigned int TNominal, unsigned int ADCMax, Adafruit_ADS1115 *ads) {

  useWaterTempSensor = true;
  waterTempSensor.begin(settings.WaterTempSensor, sensorPin, seriesResistor, NTCNominal, BCoefficient, TNominal, ADCMax, &ads);
  return;
}

When I try to compile this, I get the following compiler error:

Arduino: 1.8.1 (Linux), Board: "Generic ESP8266 Module, 80 MHz, 40MHz, DIO, 115200, 4M (1M SPIFFS), ck, Disabled, None"

/home/wouter/Arduino/libraries/HydroMonitor/src/HydroMonitorMaster.cpp: In member function 'void HydroMonitorMaster::enableThermistor(unsigned char, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, Adafruit_ADS1115*)':
/home/wouter/Arduino/libraries/HydroMonitor/src/HydroMonitorMaster.cpp:125:130: error: invalid conversion from 'Adafruit_ADS1115**' to 'uint8_t {aka unsigned char}' [-fpermissive]
   waterTempSensor.begin(settings.WaterTempSensor, sensorPin, seriesResistor, NTCNominal, BCoefficient, TNominal, ADCMax, &ads1115);
                                                                                                                                  ^
In file included from /home/wouter/Arduino/libraries/HydroMonitor/src/HydroMonitorMaster.h:14:0,
                 from /home/wouter/Arduino/libraries/HydroMonitor/src/HydroMonitorMaster.cpp:1:
/home/wouter/Arduino/libraries/Adafruit_ADS1X15-master/Adafruit_ADS1015.h:151:3: error:   initializing argument 1 of 'Adafruit_ADS1115::Adafruit_ADS1115(uint8_t)' [-fpermissive]
   Adafruit_ADS1115(uint8_t i2cAddress = ADS1015_ADDRESS);
   ^
exit status 1
Error compiling for board Generic ESP8266 Module.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

The ADS1115 library gets initialised in the .ino sketch, here the I2C address is passed as well. Then this already instantiated object has to be passed to the master library for further use (it's supposed to get passed on again to yet another library, where the actual sensor is read - one piece for each sensor, up to four different sensors on one ADS1115).

When passing the reference to the master.enableThermistor, it appears that the library tries to instantiate again. Of course this doesn't work (no way to give the I2C address), and of course it shouldn't happen at all because this object should have been instantiated already.

Now I must admit that I still don't fully understand references/pointers and may be doing something totally wrong, but this is at least what I want to happen. Initialise my external ADC (and, related, a port extender - haven't tackled that one yet but should go the same way), then pass the reference of the object to whichever part of my code needs it.

waterTempSensor is my own library handling the water temperature measurement. At the moment the only option is a thermistor, which is connected either directly to the ESP-12 or connected to an ADS1115 external ADC. Below the .h and the .cpp file as they stand. Need cleaning up work, I know.

/*
 * HydroMonitorWaterTempSensor.h
 *
 * (C) Wouter van Marle / City Hydroponics
 * www.cityhydroponics.hk
 */
 

// ensure this library description is only included once
#ifndef WATERTEMP_h
#define WATERTEMP_h

#include <Arduino.h> // Needed for the String type.
#include <Adafruit_ADS1015.h>

class HydroMonitorWaterTempSensor
{
  public:

    struct Settings {
      unsigned char Samples;            // The number of samples required for a reading.
    };

    HydroMonitorWaterTempSensor();
    void begin(Settings, unsigned char, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, Adafruit_ADS1115);
    void begin(Settings, unsigned char, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int);
    void setSettings(Settings);
    double readSensor(void);            // Read the temperature using the probe.
    void setADC(Adafruit_ADS1115);      // Sets the ADS1115 ADC.
    String settingsHtml(void);

  private:
    unsigned char sensorPin;                      // The pin the NTC is connected to.
    int seriesResistor;                 // The value of the series resistor.
    int NTCNominal;
    bool NTCPresent;
    int BCoefficient;
    float TNominal;
    int ADCMax;
    Adafruit_ADS1115 ads1115;
    bool useAds1115;                    // Whether we're using an ADS1115 or the internal ADC.
    Settings settings;

};

#endif
#include <HydroMonitorWaterTempSensor.h>

#include <Average.h>
#include <Adafruit_ADS1015.h>

Adafruit_ADS1115 ads1115;
      
/*
 * Calculate the water temperature from the reading of the thermistor.
 */
HydroMonitorWaterTempSensor::HydroMonitorWaterTempSensor () {
  NTCPresent = false;
  useAds1115 = false;
}

void HydroMonitorWaterTempSensor::begin(Settings s, unsigned char sp, int sr, int nn, int b, float tn, int adc, Adafruit_ADS1115 ads) {

  // This routing if we're connecting the NTC to an external ADS1115 ADC.
  useAds1115 = true;
  ads1115 = ads;
  begin(s, sp, sr, nn, b, tn, adc);
  return;
}

void HydroMonitorWaterTempSensor::begin(Settings s, unsigned char sp, int sr, int nn, int b, float tn, int adc) {

  sensorPin = sp;
  seriesResistor = sr;
  NTCNominal = nn;
  BCoefficient = b;
  TNominal = tn;
  ADCMax = adc;
  
  setSettings(s);
  return;
}

void HydroMonitorWaterTempSensor::setSettings(Settings s) {

  // Retrieve and set the Settings.
  settings = s;
  // Check whether any settings have been set, if not apply defaults.
  if (settings.Samples == 0 or settings.Samples > 100) {
    settings.Samples = 10;
  }
  return;
}

double HydroMonitorWaterTempSensor::readSensor() {

  // read the value from the NTC sensor:
  float reading;
  Average<float> measurements(settings.Samples);

  // Check whether the NTC sensor is present.
  if (useAds1115) reading = ads1115.readADC_SingleEnded(sensorPin);
  else reading = analogRead(sensorPin);
  if (reading < 0.03*ADCMax || reading > 0.97*ADCMax) {
    NTCPresent = false;
    return -1;
  }

  NTCPresent = true;
  for (int i = 0; i < settings.Samples; i++) {
    if (useAds1115) reading = ads1115.readADC_SingleEnded(sensorPin);
    else reading = analogRead(sensorPin);
  
    //Calculate temperature using the Beta Factor equation
    float measurement = 1.0 / (log (seriesResistor / ((ADCMax / reading - 1) * NTCNominal)) / BCoefficient + 1.0 / (TNominal + 273.15)) - 273.15;
    measurements.push(measurement);    
    delay(10);
    yield();
  }
  float t = measurements.mean();
  return t;
}

String HydroMonitorWaterTempSensor::settingsHtml() {
  String html = F("<tr>\
        <th colspan=\"2\">Water Temperature Sensor Settings.</th>\
      </tr><tr>\
        <td>Number of samples:</td>\
        <td><input type=\"number\" name=\"watertemp_samples\" value=\"");
  html += String(settings.Samples);
  html += F("\"></td>\
      </tr>");
  return html;
}

OK, solved it.

Changed

void begin(Settings, unsigned char, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, Adafruit_ADS1115);

to:

void begin(Settings, unsigned char, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, Adafruit_ADS1115*);

and it compiles just fine.
As I thought, just a C programming technicality...