refering to an object instance in a for loop

I am new to programming. I have discovered and solved a lot, but now I am stuck.
I am using 4 mlx90614 IR thermometers. I was able to change the internal adressses. I am also able to read values from each sensor.
Im my sketch I create object instances like this:

IRTherm therm0; // Create IRTherm objects to interact with
IRTherm therm1;
IRTherm therm2;
IRTherm therm3;

and then initiate them like this:

therm0.begin(0x5A);
therm1.begin(0x5B);
therm2.begin(0x5C);
therm3.begin(0x5D);

their reprogrammed addresses are ofcourse between brackets.

I need to be able to refer to individual instances in a for loop where the number after "therm" equals the loop counter.
I tried an array, and then use the index with the loop counter. But then my serial monitor hangs. And I honestly still am unsure how to define an array with object instances.

I spent two full days now, but as I said, I am stuck. Hope it makes sense.

Anybody with a solution? Thanks!!

An array is the way to go. For assistance, post your best effort using Code Tags.

Oops, I have deleted all of that. Will give a new try.

As gfvalvo suggests, use an array.

In this example (compiles, not tested), I create an array of pointers to IRTherm objects and populate the pointer array with the addresses of each of the declared objects.

In the code, references to the indexes of this array then allows the use of the ‘->’ operator to gain access to each object’s methods.

Give it a try. Note the serial monitor baud rate is set to 115200. Set your SM to match or change the code to match your SM…whichever works for you.

(As noted above: Compiles, not tested…)

#include <Wire.h> // Include Wire.h - Arduino I2C library
#include <SparkFunMLX90614.h> // Include IR thermometer library

IRTherm therm0; // Create an IRTherm object called temp
IRTherm therm1; // Create an IRTherm object called temp
IRTherm therm2; // Create an IRTherm object called temp
IRTherm therm3; // Create an IRTherm object called temp

uint8_t sensorAddresses[] = { 0x5a, 0x5b, 0x5c, 0x5d };
IRTherm *pSensors[] =
{
    &therm0,
    &therm1,
    &therm2,
    &therm3
};

void setup(void )
{
    Serial.begin(115200);
    for( uint8_t i=0; i<4; i++ )
    {
        pSensors[i]->begin(sensorAddresses[i]);
        pSensors[i]->setUnit(TEMP_F);        
        
    }//for
    
}//setup

void loop( void )
{
    static uint8_t
        sensorIndex=0;

    if( pSensors[sensorIndex]->read() ) // Read from the sensor
    { 
        // If the read is successful:
        float ambientT = pSensors[sensorIndex]->ambient(); // Get updated ambient temperature
        float objectT = pSensors[sensorIndex]->object(); // Get updated object temperature
        Serial.println("Ambient: " + String(ambientT) );
        Serial.println("Object: " + String(objectT) );
        Serial.println();
      
    }//if

    sensorIndex++;
    if( sensorIndex == 4 )
        sensorIndex = 0;
        
}//loop

Wow! thanks. I'll give that a try and let you know.

SOLVED!!

thank you so much. Now I can go ahead to the next challenges.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.