Help with storing values into array

Hi folks,

Looking for some help with storing values into an array. The code I've attached is a small chunk from a program used to run a particulate monitor. I can provide more of the code if necessary. The program directs the arduino to take ten samples at one second intervals every minute while simultaneously printing these samples to the serial monitor. The monitor uses SPI communication. I should also note that the original code was written by the company that manufactures this particulate monitor. I've been slowly modifying it to get it to function to my project needs.

Anyway, my eventual goal is to get an average of the 10 values stored in the array at the end of each sample cycle. The value of interest is "pm10Conv". I believe I have the correct syntax for storing values into an array using a for loop (found in the PrintData function), but the location of said for loop leads to the same value being stored into each index, instead of 10 different values if that makes sense.

Is there a way for me to grab the 10 different "pm10Conv" values that have been printed at the end of each cycle and store them into an array? I figure the for loop will need to be inside of the main loop for this to happen, but I'm not really sure how to go about this since each line of printed values are somewhat "disconnected" from each other. I've attached a screenshot of what the printed values look like on the serial monitor.

Based on the numbers printed in the attached picture I would want my array to be as follows:
pm10Array = {1, 3, 1, 1, 2, 102, 1, 0, 0, 1}

Hope my question makes sense. Thanks.

void loop()
{
  StartOPC(); // Function to start monitor

  // Get 11 histogram data sets (don't record the first one as it will contain invalid data)
  for (byte i=0; i<11; i++)
  {
    delay(1000);
    ReadOPChist(); // Function to read monitor histogram data

    if (i != 0)
    {
      PrintData(opSerial); // Print data to serial monitor
      opSerial.println(); // Formatting
    }
  }

  StopOPC(); // Function to stop monitor
}

void ReadOPChist (void)
{
  GetReadyResponse(0x30);
  for (SPI_in_index=0; SPI_in_index<86; SPI_in_index++)
  {
    delayMicroseconds(10);
    SPI_in[SPI_in_index] = SPI.transfer(0x01); // Value of outgoing byte doesn't matter
  }
  
  SetSSpin(HIGH);
  SPI.endTransaction();
  delay(10);
}

void PrintData (Stream &port)
{
  unsigned char i;
  unsigned char x;
  float *pFloat;
  float *pm10Float;
  int pm10Conv;

  int pm10Array[10]; // Array to hold 10 pm10 values

  // PM values(ug/m^3) (4-byte float) x3
  for (i=60; i<72; i+=4)
  {
    pFloat = (float *)&SPI_in[i];
    port.print(*pFloat, 3); // print to 3 decimal places
    AddDelimiter(port); // Function for adding delimiter between values
  }

  // Convert PM10 mass concentration from float to integer
  // Print converted PM10 mass concentration
  pm10Float = (float *)&SPI_in[68]; // PM10 value stored in SPI_in index 68
  pm10Conv = *pm10Float + 0.5; // float to integer conversion
  port.print(pm10Conv);

  for (x=0; x<10; x++)
  {
    pm10Array[x] = pm10Conv;
    AddDelimiter(port);
    port.print(pm10Array[x]);
  }

  port.flush();
}

sample.PNG

your printData() function only contains 1 data value per call so you can't fill your entire array until it has been called at least 10 times.

To do that, you need to make your array a global variable (declared outside the function) and have an index (global) keeping track of where to put the next value.

void loop()
{
  StartOPC(); // Function to start monitor

  // Get 11 histogram data sets (don't record the first one as it will contain invalid data)
  for (byte i = 0; i < 11; i++)
  {
    delay(1000);
    ReadOPChist(); // Function to read monitor histogram data

    if (i != 0)
    {
      PrintData(opSerial); // Print data to serial monitor
      opSerial.println(); // Formatting
    }
  }

  StopOPC(); // Function to stop monitor
}

void ReadOPChist (void)
{
  GetReadyResponse(0x30);
  for (SPI_in_index = 0; SPI_in_index < 86; SPI_in_index++)
  {
    delayMicroseconds(10);
    SPI_in[SPI_in_index] = SPI.transfer(0x01); // Value of outgoing byte doesn't matter
  }

  SetSSpin(HIGH);
  SPI.endTransaction();
  delay(10);
}

const int arrayCount = 10;
int pm10Array[arrayCount]; // Array to hold 10 pm10 values
int arrayIdx = 0;

void PrintData (Stream &port)
{
  unsigned char i;
  unsigned char x;
  float *pFloat;
  float *pm10Float;
  int pm10Conv;


  // PM values(ug/m^3) (4-byte float) x3
  for (i = 60; i < 72; i += 4)
  {
    pFloat = (float *)&SPI_in[i];
    port.print(*pFloat, 3); // print to 3 decimal places
    AddDelimiter(port); // Function for adding delimiter between values
  }

  // Convert PM10 mass concentration from float to integer
  // Print converted PM10 mass concentration
  pm10Float = (float *)&SPI_in[68]; // PM10 value stored in SPI_in index 68
  pm10Conv = *pm10Float + 0.5; // float to integer conversion
  port.print(pm10Conv);

  pm10Array[arrayIdx] = pm10Conv;
  arrayIdx++;
  if ( arrayIdx >= arrayCount ) {
    arrayIdx = 0;
  }
  port.flush();
}

After the array is full, you can extend your code to average them, print them, whatever.