How to collect data in a 2 dimensional array?

I am making a robot that has a lidar in his head.
I made a command that has the head panning over 450 micro steps and I am collecting distance and angle of the head in 2 variables.

  tfmP.getData( tfDist);
     Fr_Clear [i_Dist] = tfDist; //  frontal clearance (CM) array from the Lidar
     Fr_ClearDeg[i_Dist] = Mot_Ang_Read [6];  // Angle reading from the panning motor encoder.
   
       Serial.printf( "%u %03icm %.2f° \n", i_Dist, Fr_Clear[i_Dist], Fr_ClearDeg[i_Dist]);

I would like to call myself a better programmer and be able to have an 2 dimensional array that collect distance and angle.
How to write that?

And how to print it out?

Thanks

are you just collecting the distance between the head and some object for each motor position?

wouldn't you only need a single array with an element for each motor position (i.e. step)?

wouldn't the angle be proportional to the step?

Yes, the angle is proportional to the step. But is more accurate to get the encoder reading than the presumptive angle from the step.

Read looks like

431 054cm 39.34°
432 054cm 39.35°
433 054cm 39.38°
434 054cm 39.39°
435 054cm 39.40°
436 051cm 39.41°
437 051cm 39.42°
438 051cm 39.44°
439 051cm 39.45°
440 051cm 39.46°
441 051cm 39.47°
442 051cm 39.50°
443 051cm 39.51°
444 051cm 39.51°
445 051cm 39.52°
446 053cm 39.53°
447 053cm 39.55°
448 053cm 39.56°
449 053cm 39.56°

Either way, I'd want a 2 dimensional array to store distance and angle,

Rather than using a two dimensional array, consider using an array of struct.

struct Reading {
  float distance;
  float angle;
};

const int STEPS= 450;
Reading Fr_Clear[STEPS];

void foo() {
  tfmP.getData( tfDist);

  Fr_Clear[i_Dist].distance = tfDist; //  frontal clearance (CM) array from the Lidar
  Fr_Clear[i_Dist].angle = Mot_Ang_Read [6];  // Angle reading from the panning motor encoder.
}
[code]

You might also want to consider checking out naming conventions for variables in C++.

Yes, the angle is proportional to the step. But is more accurate to get the encoder reading than the presumptive angle from the step.

Why do you say that? The output you indicate with non uniform increments per step would argue against that.

Where is the panning motor encoder attached?

I would love to use a struct for the first time.

How do I print it out? Now I got

Serial.printf( "%u %03icm %.2f° \n", i_Dist, Fr_Clear[i_Dist], Fr_ClearDeg[i_Dist]); for the two arrays.

Thanks a lot
Mitch

I would use a 2D array, something like:

float Fr_data[2][450];

Fr_data[0][index] could be distance and Fr_data[1][index] could be angle.

Serial.printf( "%u %6.2fcm %5.2f° \n", i_Dist, Fr_data[0][i_Dist], Fr_data[1][i_Dist]);

laptophead:
I would love to use a struct for the first time.

How do I print it out? Now I got

Serial.printf( "%u %03icm %.2f° \n", i_Dist, Fr_Clear[i_Dist], Fr_ClearDeg[i_Dist]); for the two arrays.

depends on your struct

if it is
struct YourStruct
{ int clear;
int clearDeg;};

and your array is:

YourStruct yourStruct[42];

you access the member variables like:

yourStruct[i_Dist].clear
yourStruct[i_Dist].clearDeg

Which Arduino board are you using ?

float Fr_data[2][450];uses an awful lot of memory

I assumed it is not an AVR-based Arduino, since the OP indicated that Serial.printf() actually works for floats.

MEMORY is not an issue, I am on teensy 4.1

The array of struct is your best answer. Gives you flexibility in case the data types ever change and allows grouping together items of different data types in a logical manner.

Thanks a lot,
Still how do I printf various values from inside the Struct?

See the example above from Paul Murray

how do I printf various values from inside the Struct?

If the struct is named wibble and has a data item named wobble then just print wibble.wobble

If you have an array of structs of type wibble then print wibble[indexNumber].wobble