Need help with for loop

Hi,

I'm trying to log IMU data onto an SD card using the LowLatencyLogger example in the SdFat library. I wanted to shorten the number of lines of code by using a for loop. However, when I try to do this the first line of data that is recorded onto the Excel sheet is repeated over and over again.

Originally, this is how I had my code. When I ran the code, the data is shown properly on the Excel spreadsheet (shown in the first attached image):

struct data_t {
  float ax1, ay1, az1;
};

void acquireData(data_t* data) {
//Acquire IMU 1 data
imu::Vector<3> accel1 = bno1.getVector(Adafruit_BNO055::VECTOR_ACCELEROMETER);
data->ax1 = accel1.x();
data->ay1 = accel1.y();
data->az1 = accel1.z();
}

void printData(Print* pr, data_t* data) {
//Print IMU 1 data
  pr->write(',');
  pr->print(data->ax1);
  pr->write(',');
  pr->print(data->ay1);
  pr->write(',');
  pr->print(data->az1);
}

void printHeader(Print* pr) {
//Print IMU 1 headers   
  pr->write(',');
  pr->print(F("ax1"));
  pr->write(',');
  pr->print(F("ay1"));
  pr->write(',');
  pr->print(F("az1"));

  pr->println();
}

However, when I tried to change the code in the printData function to a for loop, the first line that is outputted keeps repeating over and over again (shown in the second attached image).

const uint8_t IMU1_DIM = 3; //number of values in IMU 1 array
struct data_t {
  float imu1_data[IMU1_DIM] = {ax1, ay1, az1}; //array of IMU 1 values
  float ax1, ay1, az1;
};

void acquireData(data_t* data) {
//Acquire IMU 1 data
imu::Vector<3> accel1 = bno1.getVector(Adafruit_BNO055::VECTOR_ACCELEROMETER);
data->ax1 = accel1.x();
data->ay1 = accel1.y();
data->az1 = accel1.z();
}

void printData(Print* pr, data_t* data) {
//Print IMU 1 data
  for (int i = 0; i < IMU1_DIM; i++) {
    pr->write(',');
    pr->print(data->imu1_data[i]);
}
}

void printHeader(Print* pr) {
//Print IMU 1 headers   
  pr->write(',');
  pr->print(F("ax1"));
  pr->write(',');
  pr->print(F("ay1"));
  pr->write(',');
  pr->print(F("az1"));

  pr->println();
}

I'm relatively new to programming and not really sure what I'm doing wrong so any help would be greatly appreciated! Thanks!

Thanks for using code tags, but if you had read the forum guide, you would know that you should always post your code in full. As you admit, you are new to programming, so how can you know if you are posting the relevent parts?

The image you posted is doing exactly what I would expect (given I can't see all the code). In what way did you expect using a for loop would shorten your code? I don't think you posted the part you wanted to shorten, which would have given us more of a clue.

You altered your data_t type by adding an array, but when you read the data, you continue to assign it to ax1, ay1, az1.

If you want 3 sets of data, you need data to be an array, not build an array within the data_t structure.

OR

If you want the 3 data points (ax1, ay1, az1) in an array versus separate variables, then get rid of ax1, ay1, az1 and change acquireData()

const uint8_t IMU1_DIM = 3; //number of values in IMU 1 array
struct data_t {
  float imu1_data[IMU1_DIM]; //array of IMU 1 values

};

void acquireData(data_t* data) {
//Acquire IMU 1 data
imu::Vector<3> accel1 = bno1.getVector(Adafruit_BNO055::VECTOR_ACCELEROMETER);
data->imu1[0] = accel1.x();
data->imu1[1] = accel1.y();
data->imu1[2] = accel1.z();
}