For loop that behave strangely over a typecast

I have a for loop that decide not to stop when I have a certain typecast process that is assigned to a variable.

below is the code:

  RegisterPatch _patchingSetting;
  uint8_t regAddress1 = 0;
  uint8_t regAddress2 = 0;
  uint8_t regAddress3 = 0;
  uint8_t regAddress4 = 0;
  uint8_t value1 = 0;
  uint8_t value2 = 0;
  uint8_t value3 = 0;
  uint8_t value4 = 0;

  int rowsize = sizeof(patchingSetting) / sizeof(RegisterPatch);
  int columnsize = sizeof(_patchingSetting) / 4;

for (int row = 0; row < rowsize; row++)
  {
    PROGMEM_readAnything(&patchingSetting[row], _patchingSetting);

    if (row != rowsize - 1)
    {
      int col = 0;
      for (byte j = 0; j < columnsize; j++)
      {

#ifdef PATCH_DEBUG_LAST_ROW
        Serial.print("Row: ");
        Serial.print(row);
        Serial.print(" Column: ");
        Serial.println(j);
#endif

        regAddress = _patchingSetting[0] + (col * 4);
        regAddress1 = (uint8_t)((regAddress >> 24) & 0x000000FF);
        regAddress2 = (uint8_t)((regAddress >> 16) & 0x000000FF);
        regAddress3 = (uint8_t)((regAddress >> 8) & 0x000000FF);
        regAddress4 = (uint8_t)(regAddress & 0x000000FF);
        column = col + 1;
        (uint8_t)_patchingSetting[column]; // <<< This is OK
        value1 = (uint8_t)_patchingSetting[0]; // <<< This is also OK
        value1 = (uint8_t)_patchingSetting[column]; // <<< This is NOK
        // value1 = ((_patchingSetting[column] & 0xFF000000) >> 24);
        // value2 = ((_patchingSetting[column] & 0x00FF0000) >> 16);
        // value3 = ((_patchingSetting[column] & 0x0000FF00) >> 8);
        // value4 = (_patchingSetting[column] & 0x000000FF);
        Wire.beginTransmission(VCM_Special_Driver_Addr);
        Wire.write(regAddress1);
        Wire.write(regAddress2);
        Wire.write(regAddress3);
        Wire.write(regAddress4);
        Wire.write(value1);
        Wire.write(value2);
        Wire.write(value3);
        Wire.write(value4);
        Wire.endTransmission();
        col += 1;
      }
    }
    else
    {
      for (int j = 0; j < 29; j++)
      {
        regAddress = _patchingSetting[0] + (j * 4);
        Wire.beginTransmission(VCM_Special_Driver_Addr);
        Wire.write((uint8_t)((regAddress >> 24) & 0x000000FF));
        Wire.write((uint8_t)((regAddress >> 16) & 0x000000FF));
        Wire.write((uint8_t)((regAddress >> 8) & 0x000000FF));
        Wire.write((uint8_t)(regAddress & 0x000000FF));
        Wire.write((uint8_t)((_patchingSetting[j + 1] >> 24) & 0x000000FF));
        Wire.write((uint8_t)((_patchingSetting[j + 1] >> 16) & 0x000000FF));
        Wire.write((uint8_t)((_patchingSetting[j + 1] >> 8) & 0x000000FF));
        Wire.write((uint8_t)(_patchingSetting[j + 1] & 0x000000FF));
        Wire.endTransmission();
      }
    }
  }

There are 2 for loops in the 'if then else' structure that essentially do the same thing.
For the first for loop, when I performed the typecast from unsigned long to uint8_t, it is ok as long as I do not assign it to another variable (Marked with 'This is OK').
But whenever I assign that typecast result into another variable, the loop just doesn't stop, it goes on until the variable ends and return back to the first value of that loop indicator variable (in this case it is byte j).

Funny thing is that it didn't happen on the second loop, that also performs the same typecast and assign it into a variable or put it into another process (in this case an I2C 'Wire.write' function.

I have ensured that there is no other part in the code that can change the for loop condition. And narrowed down to the 'typecasting of an array component that is assigned to another variable' but only in the first loop.
Please help, what else could I check?

Thank you

consider the loop

      for (byte j = 0; j < columnsize; j++) {
       .....
      }

a byte can hold a value 0 to 255
what happens if the value of columsize is greater than 255?

I initially used int for the loop indicator, the loop kept increasing until the maxValue of int (I guess, I didn't get the chance to wait). Thus I changed it to byte so that I can observe better.

what is the value of columnsize at the start of the loop?

Oh, btw I am using an ESP32 Dev kit hardware with ESP32 Arduino framework. I am using Visual studio Code with platformIO as development platform. I remembered that this very code worked fine before, I had no issue. Does it have anything to do with the update (if any) of the framework?

The columnsize variable has a value of 33. I even tried to change the loop condition with a constant like so. The behaviour stayed the same.

for (byte j = 0; j < 33; j++)
      {
.
.
.
.
      }

I've just found the solution. In platformIO it seems that, it is always using the latest platform version, at least in my setting. After I changed the espressif32 version to 3.5.0 (released 4 months ago), the code now is fully functional.

Not really sure how can I report this or replicate the issue. As it is not possible for me to re share the whole project code.

Thank you

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