Unable to write data via 2D array into a variable

Hi,

I tried to use a 2D array combined with a for-loop. Nothing special.
But it seems like I'm unable to write data into the array (pointer?). For example:

Array[0][0] = 100.0;
Serial.print(Array[0][0]); //-> returns 0 instead of 100.0

Here is the piece of code that doesn't work:

int counter_1;
int counter_2;
int plotter_max = 90;
int plotter_min = -90;

float acc_y,              acc_x,              acc_z,
      gy_y,               gy_x,               gy_z,
      acc_pitch_calc,     acc_roll_calc,      acc_yaw_calc,
      acc_pitch_fil_new,  acc_roll_fil_new,   acc_yaw_fil_new,
      acc_pitch_fil_old,  acc_roll_fil_old,   acc_yaw_fil_old,
      pitch_multiplied,   roll_multiplied,    yaw_multiplied,
      pitch_normalized,   roll_normalized,    yaw_normalized,
      pitch_mapped,       roll_mapped,        yaw_mapped,
      pitch_to_servo,     roll_to_servo,      yaw_to_servo,
      pitch,              roll,               yaw,
      last_pitch,         last_roll,          last_yaw;


const int max_lines = 6;
const int max_columns = 3;

const int multiplied_line = 0;
const int normalized_line = 1;
const int mapped_line = 2;
const int to_servo_line = 3;
const int actual_value_line = 4;
const int last_value_line = 5;

float alignment_array[max_lines][max_columns] = {
  {pitch_multiplied,   roll_multiplied,    yaw_multiplied},
  {pitch_normalized,   roll_normalized,    yaw_normalized},
  {pitch_mapped,       roll_mapped,        yaw_mapped},
  {pitch_to_servo,     roll_to_servo,      yaw_to_servo},
  {pitch,              roll,               yaw},
  {last_pitch,         last_roll,          last_yaw}
};
void setup(){
  Serial.begin(9600);
}
void loop(){
pitch = 123.0;
for (counter_1 = 0; counter_1 < max_columns; counter_1++) {
    alignment_array[multiplied_line][counter_1] = alignment_array[actual_value_line][counter_1] * 100.0;
    if (counter_1 == 0 || counter_1 == 2) {
      alignment_array[mapped_line][counter_1] = map(alignment_array[multiplied_line][counter_1], 0, 18000, 18000, 0);
    }
    else {
      if (alignment_array[actual_value_line][counter_1] > 0.0 && alignment_array[actual_value_line][counter_1] < 90.0) {
        alignment_array[mapped_line][counter_1] = map(alignment_array[multiplied_line][counter_1], 9000, 0, 0, 9000);
      }
      else if (alignment_array[actual_value_line][counter_1] < 0.0 && alignment_array[actual_value_line][counter_1] > -90.0) {
        alignment_array[mapped_line][counter_1] = map(alignment_array[multiplied_line][counter_1], 0, -9000, 9000, 18000);
      }
    }
    alignment_array[normalized_line][counter_1] = alignment_array[mapped_line][counter_1] / 100.0;
  }

  Serial.print('\t');
  Serial.print(pitch);
  Serial.print('\t');
  Serial.print(alignment_array[0][0]);
  Serial.print('\t');
  Serial.print(roll_multiplied);
  Serial.print('\t');
  Serial.print(yaw_multiplied);
  Serial.println('\t');

}

I'm using a Nano 33 BLE.

there a bunch of confusion about your code as well as no code that clearly demonstrates the problem you suggest

where is Array[][] defined?

i don't see where you set zeroeth element of alignment_array to anything, both actual_value_line and mapped_line are != 0. I wouldn't expect its printed value to be anything other than what it is initialized to (i.e. 0)

i see array [][] not zero if i set it to a changing value

   static float val = 2;
    alignment_array[0][0] = val += 1;

    Serial.print('\t');
    Serial.print(pitch);
    Serial.print('\t');
    Serial.print(alignment_array[0][0]);

looks like you statically initialize alignment_array [][] with variable, but the variables are not initialized (therefore 0).

because you initialized the array to variables (e.g. acc_y) suggests you may think the value of the array elements will be whatever those variables are changed to -- that the array elements are pointers to those variables. ??

btw, long symbol names (e.g. alignment_array, actual_value_line, ...) make the code hard to read. why not use "col" or ever just "c" instead of "counter_1"?

Sorry, the code:

Array[0][0] = 100.0;
Serial.print(Array[0][0]); //-> returns 0 instead of 100.

was just meant as an example to show my intentions.
The original .ino file of the project I'm working on has a size of ~300 lines. Instead of sending everything, i chose to copy the bare minimum.

To further explain my problem, I just wrote just a few lines. Which are more understandable, hopefully...

int z_1;
float Var_1;
float Var_2;
float Var_3;
float Var_4;

float Var_Array[2][3] = {
  {Var_1, Var_2},
  {Var_3, Var_4}
};

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  while (!Serial) {
  }
}

void loop() {
  //In the following part the variable_1 gets set to a value of 1.0
  //in the line after, the variable_2 is supposed to be set to the same value of var_1 by using the pointer [0][1] = [0][0]
  
  Var_1 = 1.0;
  Var_Array[0][1] = Var_Array[0][0];
  // The following code is just to check if it worked:
  Serial.print("Var_1: ");
  Serial.print(Var_1);
  Serial.print('\t');
  Serial.print("Var_1 Array: ");
  Serial.print(Var_Array[0][0]);
  Serial.print('\t');
  Serial.print("Var_2 Array: ");
  Serial.print(Var_Array[0][1]);
  Serial.println('\t');
}

If I understood it correctly, the var_2 should contain var_2 when the code is executed. This isn't the case tho. What is wrong?

float Var_Array[2][3] = {
  {Var_1, Var_2},
  {Var_3, Var_4}
};

That code initializes Var_Array with the current values of Var_1 ... VAR_4. Since those are global variables, and you did not supply them with an initial value, they are initialized to zero. Hence your array is initialized to zero.

BTW, why did you specify an array that holds 6 elements and only supply 4 initial values?

float Var_Array[2][3] =
{
  {Var_1, Var_2},
  {Var_3, Var_4}
};

Why no data for the third column ?

If I understood it correctly, the var_2 should contain var_2 when the code is executed.

Var_1, Var_2, Var_3 and Var_3 are all initialised to zero when they are declared as globals and you put these values into the array. When you later set the value of Var_1 to 1 it does not update the value held in the array, you need to do that explicitly

jaddow:
If I understood it correctly, the var_2 should contain var_2 when the code is executed. This isn't the case tho. What is wrong?

because you initialized the array to variables (e.g. acc_y) suggests you may think the value of the array elements will be whatever those variables are changed to -- that the array elements are pointers to those variables. ??

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