Having trouble with TMP102 function calls on ESP thing plus WROOM

I have 2 TMP102 bds connected I2C to my ESP32 Thing Plus. I finally have all the bugs worked out to get the temps and record them on a SD card.

Now am practicing programming functions to use in future projects. I am totally unable to find a way to send the “sensor0” and “sensor1” names to the “TMP102 sensor0” call thru a function call. I finally was able to find a way to send the sensor address to the function thru an array of string addresses but not the sensor name.

This is obviously not a major problem because I can never have more than 4 TMP102 sensors on the I2C wire. But I would like to know what is special about the sensor0, etc. names that they can not be replaced with a string of the same name.

After additional trouble shooting, I finally solved my problem.

The problem is in a function such as

TMP102 sensor0;

if(!sensor0.begin(AddOf102num))

the variable sensor0 could be a string out of an array. One just cannot replace sensor0 with another string “sensor0”.

You must create a new variable and then replace it with what you used before.

So what works is

String VarSensor = MySensor;

TMP102 VarSensor;

if(!VarSensor.begin(AddOf102num))

MySensor = “sensor0”

Hope this saves someone else the hours I spent on it.

No it is still a problem–I had missed a residual call and the sensor0 was not replaced.

So if anyone know of a way to replace The sensor? call with a variable I would appreciate the info.

sensor0 is an object; it has functions associated with it, so you can’t just pass a sensor number or name as an argument to the function. I don’t have one to try but something like this may work:

TMP102 sensor[4];

for (int sensorNum = 0; sensorNum < 4; sensorNum++)
{
  if(!sensor[sensorNum].begin(AddOf102num[sensorNum ]))
  ...
}

/mike

Thanks Mike. That was the point I was missing. I quickly ran your sketch and I got past my hangup. I still have work to do to identify which sensor is which and reference them in Serial.print statements, but that should be easy.

I quickly went into the Arduino reference page to study up on objects, but could not find anything. Do you know where I would go to get more info?

Thanks again, Rudy