Nested FOR loop not working

Hi all,

I am trying to use Nested FOR loops, but for some reason it is not working for me. When I comment the first FOR loop, everything is all ok, but once I try to use the FOR loop I cant read anything from I2C.

Just a bit of background. What I am trying to do is to read the temperature of 4 PA modules, each PA module has 4 temperature sensors (TMP117). I am organising that data in a matrix (uiPATemperaturesX10_TMP117) where the rows represent the "indent" of the PA module and the columns the "ident" of the temp sensor.

So I decided to do a sweep of all temp sensor per module, and to do that I decided to use nested FOR loops which seems to be logical, but for some reason is not working for me. If I happen to comment the loop that sweeps the rows all is good. All the temperature of the same module can be read. The problem is when I incorporate the first FOR loop (for (uint8_t i = 0; i < NumOfPAmodules; i++)) Does anyone has any suggestion?

#include <Wire.h>

// TMP117 Variables // 
// TMP117 Addresses
const uint8_t TMP117_Addr1 = 0x48; //GND
const uint8_t TMP117_Addr2 = 0x49; //VCC
const uint8_t TMP117_Addr3 = 0x4A; //SDA
const uint8_t TMP117_Addr4 = 0x4B; //SCL
// TMP117_Register
 #define TMP117_Temp_Result  0x00
 #define TMP117_Configuration  0x01
 #define TMP117_THigh_Limit  0x02
 #define TMP117_TLow_Limit  0x03
 #define TMP117_EEPROM_UL  0x04
//TMP117 Resolution
const float TMP117_Resol = 0.0078125; 

const uint8_t NumTempSensorsPerPAModule = 4; 
const uint8_t NumOfPAmodules = 4; 

unsigned int uiPATemperaturesX10_TMP117[NumOfPAmodules-1][NumTempSensorsPerPAModule-1];//[PA module Number][Temp117 Number]
unsigned int uiPA_THighLimitX10_TMP117[NumOfPAmodules-1][NumTempSensorsPerPAModule-1]; //[PA module Number][Temp117 Number]
unsigned int uiPA_TLowLimitX10_TMP117[NumOfPAmodules-1][NumTempSensorsPerPAModule-1];  //[PA module Number][Temp117 Number]
unsigned int uiPA_Config_TMP117[NumOfPAmodules-1][NumTempSensorsPerPAModule-1];        //[PA module Number][Temp117 Number]

const byte PAAddressPIN_B0 = 4;  // Set pin number for B0 here
const byte PAAddressPIN_B1 = 7;  // Set pin number for B1 here
const byte PAAddressPIN_B2 = 2;  // Set pin number for B2 here

// SETUP //
void setup() {
pinMode(PAAddressPIN_B0, OUTPUT); 
pinMode(PAAddressPIN_B1, OUTPUT); 
pinMode(PAAddressPIN_B2, OUTPUT); 
pinMode(LED_BUILTIN, OUTPUT);
Wire.begin();
}

// MAIN // 
void loop() {
fbReadPA_DataTMP117(TMP117_Temp_Result, uiPATemperaturesX10_TMP117); 

Serial.println(uiPATemperaturesX10_TMP117[0][0]);
Serial.println(uiPATemperaturesX10_TMP117[0][1]);

digitalWrite(LED_BUILTIN, HIGH);   
delay(250);
digitalWrite(LED_BUILTIN, LOW);    
}

// FUNCTIONS // 
word fbReadDataTMP117(byte Register2read, uint8_t Address) {
   word RegisterValue = 0; 
   byte Low_Byte, High_Byte; 
 
   Wire.beginTransmission(Address);   
   Wire.write(Register2read);    
   Wire.endTransmission();

   Wire.requestFrom(Address, (size_t)2);    
   while (Wire.available()) { 
   Low_Byte = Wire.read(); 
   High_Byte = Wire.read();

   RegisterValue = ((((Low_Byte & 0x00FF)<<8) & 0xFF00) | (High_Byte & 0x00FF)); 
  }
 return RegisterValue;
}


void fbReadPA_DataTMP117(byte Register2read, unsigned int MatrixPointer[NumOfPAmodules-1][NumTempSensorsPerPAModule-1]) { 
    uint8_t Address = TMP117_Addr1; 
    bool B0, B1, B2; 
    
    for (uint8_t i = 0; i < NumOfPAmodules; i++){   // set the digital pins to address a specific PA module
      B0 = (i & 0x01); 
      B1 = (i & 0x02); 
      B2 = (i & 0x04); 
      
      if (B0) {digitalWrite(PAAddressPIN_B0, HIGH);} else {digitalWrite(PAAddressPIN_B0, LOW);}
      if (B1) {digitalWrite(PAAddressPIN_B1, HIGH);} else {digitalWrite(PAAddressPIN_B1, LOW);}
      if (B2) {digitalWrite(PAAddressPIN_B2, HIGH);} else {digitalWrite(PAAddressPIN_B2, LOW);}
      delayMicroseconds(100);

      for (uint8_t j = 0; j < NumTempSensorsPerPAModule; j++) {  // read the temperatures of the 4 PAs from a specific PA module
        if (Register2read == TMP117_Configuration) {
          MatrixPointer[i][j] = fbReadDataTMP117((Address + j), Register2read); 
        }
        else {
          MatrixPointer[i][j] = fbReadDataTMP117(Register2read, (Address + j))*(TMP117_Resol*10);  // save the temp times 10 e.g. 23.4 = 234
        }
        
      }
    }
}```

This has a strange order of parameters.

Hi @Whandall

You are right. I forgot to fix that bit, however at this point that line will never be called yet. In the example I posted it will never go that way. Thanks for spotting that though. Would you have any clue about the nested for loop not working?

Well the nested loops are working as nested loops so that is not a problem.

I assume that what you mean is that your code is not working as you would like which is something complicated different.

Why when assigning your arrays do you subtract one from the dimensions? That doesn’t make any sense to me.

1 Like

Hi @Grumpy_Mike ,

Thanks for your reply. The reason being is that I have 4 PA modules and 4 Temp sensor, I want my matrix in this case to be 4x4. Since the matrix starts from zero, I substract one, if that is what you meant.

The issue with the nested loop, is that once I put in place, I just get one serial response. Also the led does not blink anymore, it is like the code is in a really bad place, but cannot figure it out. On the contrary if I comment the first FOR loop, all is good but of course this is not what I want. .
Top one is when I comment the for loop, bottom one is when it is not commented.

const uint8_t NumTempSensorsPerPAModule = 4; 
const uint8_t NumOfPAmodules = 4; 

unsigned int uiPATemperaturesX10_TMP117[NumOfPAmodules-1][NumTempSensorsPerPAModule-1];//[PA module Number][Temp117 Number]
unsigned int uiPA_THighLimitX10_TMP117[NumOfPAmodules-1][NumTempSensorsPerPAModule-1]; //[PA module Number][Temp117 Number]

Look carefully at the declarations of the arrays such as this one

unsigned int uiPATemperaturesX10_TMP117[3][3];//[PA module Number][Temp117 Number]

So the array is 3 elements by 3 elements indexed from [0][0] to [2][2] (9 in total)

How many elements do you need it to have, considering that you have 4 modules each with 4 sensors ?

1 Like

Hi @UKHeliBob and @Whandall sorry, you are both right. I think that was the issue, I thought the array declaration started from zero. I've been jumping from Codesys and IDE, sorry my bad. Thanks guys for correcting that silly mistake :slightly_smiling_face:

Yes, just got back to this one. You mixed up the declaration which must contain all the numbers you want in an array, with the index numbers you have to use to access those elements of the array.

So four elements in an array you declared the array has four elements, but they are accessed by using the index values zero to three.

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