[Solved] Loop pgm_read large array

I have 6000ish points of interests, which I separated into three equally large arrays and wrote in my memory. The reason for the seperation was basicly the maximum array size.

So I wrote a function to read from these arrays precisely, which I intend to loop over:

double readFromMemory(int arrayIndex, int firstDimension, int secondDimension) {
  if (arrayIndex == 1) {
    return pgm_read_float_far(pgm_get_far_address(radarPositions1[firstDimension][secondDimension]));
  }
  if (arrayIndex == 2) {
    return pgm_read_float_far(pgm_get_far_address(radarPositions2[firstDimension][secondDimension]));
  }
  if (arrayIndex == 3) {
    return pgm_read_float_far(pgm_get_far_address(radarPositions3[firstDimension][secondDimension]));
}

That way, my intention is, that I dont load 140+ kb Data into my 8kb memory. Instead only load one, evaluate and discard, repeat.

Here is my loop:

int loopLimit = sizeof(radarPositions1)/sizeof(radarPositions1[0]);

for (int i = 1; i < 4; i++) {
  for(int j = 0; j < loopLimit; j++) {
    readFromMemory(i, j, 0));
  }
}

And this dont compile. I get following error:

<artificial>:(.text+0xfec): undefined reference to `r30' (this one comes 3 times)

collect2.exe: error: ld returned 1 exit status

Since readFromMemory(1, 0, 0)); compiles and runs properly, also Serial.println ing in loop works properly, I think I am filling the memory with some stuff which is not removed afterwards.

Any ideas?
Board: Arduino Mega with 8Kb Memory 256 Kb Flash
Link to GitHub Repo of Project

Update: Code works, when the second loop (the one with j) runs only once.
Update 2: Calling readFromMemory twice crashes as well. No loop involved...

  readFromMemory(1, 0, 0);
  readFromMemory(1, 1, 0);

bump...

It is hard to find help about memory issues ^^

I think the argument to pgm_get_far_address has to be know at compile time, so you can't give it an indexed value directly. Last time I stored something that large I had to get the base address of the array then calculate the position of the variable instead of using array indexes.

double readFromMemory(int arrayIndex, int firstDimension, int secondDimension) {
  const uint_farptr_t radarPositions1base = pgm_get_far_address(radarPositions1);
  const uint_farptr_t radarPositions2base = pgm_get_far_address(radarPositions2);
  const uint_farptr_t radarPositions3base = pgm_get_far_address(radarPositions3);
  const uint16_t entriesPerRow1 = sizeof(radarPositions1[0]) / sizeof(radarPositions1[0][0]);
  const uint16_t entriesPerRow2 = sizeof(radarPositions2[0]) / sizeof(radarPositions2[0][0]);
  const uint16_t entriesPerRow3 = sizeof(radarPositions3[0]) / sizeof(radarPositions3[0][0]);
  if (arrayIndex == 1) {
    //return pgm_read_float_far(pgm_get_far_address(radarPositions1[firstDimension][secondDimension]));
    return pgm_read_float_far(radarPositions1base + sizeof(float) * (firstDimension * entriesPerRow1 + secondDimension));
  }
  if (arrayIndex == 2) {
    //return pgm_read_float_far(pgm_get_far_address(radarPositions2[firstDimension][secondDimension]));
    return pgm_read_float_far(radarPositions2base + sizeof(float) * (firstDimension * entriesPerRow2 + secondDimension));
  }
  if (arrayIndex == 3) {
    //return pgm_read_float_far(pgm_get_far_address(radarPositions3[firstDimension][secondDimension]));
    return pgm_read_float_far(radarPositions3base + sizeof(float) * (firstDimension * entriesPerRow3 + secondDimension));
  }
}

Is the 3rd entry in your table always a whole number? I didn't look at all the entries, but I don't see any values over 180 or negative, if you can store that as a byte all your data would fit in two arrays.

Works, appreciate that mate!

How do you create a mixed array? I doubt that I can represent the first and second dimensions of my array as byte.

dr-o:
Works, appreciate that mate!

How do you create a mixed array? I doubt that I can represent the first and second dimensions of my array as byte.

An array of structures.

This is a gamechanger dougp! Thanks. I don't need any optimization neither on memory nor runtimewise yet but this will come in handy, when I refactor the whole thing when it is finished!

dr-o:
This is a gamechanger dougp! Thanks. I don't need any optimization neither on memory nor runtimewise yet but this will come in handy, when I refactor the whole thing when it is finished!