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
}
}
}
}```