use a for boucle to call several library functions

Status
Not open for further replies.

Manu

Well-known member
Hi,

I'm sorry for my bad English.

I'm currently try to make a prototype that use 4x max31855 SPI chips to grab TK temperature and send them over CAN bus. I have it working but my initial code was "heavy". Today I try to simplify it and I face a problem. I use a teensy andAdafruit MAX31855 library.

Here the MAX and TK information declaration code :

Code:
#define nbTK 4

MAX31855 TK1 = MAX31855(MAX31855_1_CS);
MAX31855 TK2 = MAX31855(MAX31855_2_CS);
MAX31855 TK3 = MAX31855(MAX31855_3_CS);
MAX31855 TK4 = MAX31855(MAX31855_4_CS);

struct infoTK {
  double rawTemp;
  double internalTemp;
  double linTemp;
  uint8_t error;
};

struct infoTK infoTK[nbTK];

In the main loop I try to do :

Code:
void loop() {
  if (Serial.available()) // Mode configuration.
  {
    ModeConfiguration();
  }
  else {                  // Mode fonctionnement
    for (int i = 1; i <= nbTK; i++) {
      infoTK[i].rawTemp = TK[i].readCelsius();
      if (isnan(infoTK[i].rawTemp)) {
        infoTK[i].error = TK[i].readError();
      }
      else {
        infoTK[i].error = 0;
        infoTK[i].internalTemp = TK[i].readInternal();
        infoTK[i].linTemp = linearisationTemperature(infoTK[i].rawTemp, infoTK[i].internalTemp);
      }
      delay(5);
    }
    Watchdog.reset();
  }
}

But TK.readCelsius() (and other calls) didn't work because of TK. Is there a way to call MAX31855 library function without using TK1, TK2, TK3 or TK4 but something that will reflect i ?

Thank,
Manu
 
Arrays start at 0, not 1.

Change this:

Code:
    for (int i = 1; i <= nbTK; i++) {

to this:

Code:
    for (int i = 0; i < nbTK; i++) {
 
But TK.readCelsius() (and other calls) didn't work because of TK. Is there a way to call MAX31855 library function without using TK1, TK2, TK3 or TK4 but something that will reflect i ?


Generally pointers are used. Something like this:

Code:
struct infoTK {
  double rawTemp;
  double internalTemp;
  double linTemp;
  uint8_t error;
  MAX31855 *instance;
};

struct infoTK infoTK[nbTK];

Then in setup:

Code:
  infoTK[0].instance = &TK1;
  infoTK[1].instance = &TK2;
  infoTK[2].instance = &TK3;
  infoTK[3].instance = &TK4;

And in loop:

Code:
      infoTK[i].rawTemp = TK[i].instance->readCelsius();
 
Thank Paul for the reply.

I understand your way to do and haven't think about it before you point me here.

Code:
 infoTK[i].rawTemp = infoTK[i].instance->readCelsius();

It work ;-)
 
Status
Not open for further replies.
Back
Top