Array assignment cause timing problem in for loop

I am trying to use an MEGA to generate a 6Hz pulse in the period of 160ms in a for loop and I would like to synchronise the analog data acquisition with the pulses.

Regarding the data, I would like to collect 100 sets of 50 samples.

Below is the code I used:

void loop()
{
  analogReference(INTERNAL1V1);
  delay(500);
  for (int i1 = 0; i1 <= 99; i1++) //Take 100 rows of samples
  {
    digitalWrite(pulse_gen, HIGH);
    delay (10); //ON pulse for 10ms
    digitalWrite(pulse_gen, LOW);
      for (int j1 = 0; j1 <= 49; j1++) //Take 50 cols of samples in 50ms
      {
        val = analogRead(analog_signal); 
        data_array[i1][j1] = val;
        delay (1);
      }
    delay (100); //Let the remaining 100ms of pulse pass
  }
}

The used baud rate is 115200.
The pulse_gen pin would output 6Hz pulses (160ms)continuously until the 100 sets data has been collected and start a new loop.
However, when I was trying to assign the analog signal into the array the pulse duration is now shortened to 53ms at about 18Hz.

Is the array assignment really affecting the timing issue of the loop and how can I sort it out?

Many thanks!

        data_array[i1][j1] = val;

What type is data_array? Why do you need to collect 5000 of them? Unless data_array is byte, 5000 of them won't fit in the Mega's memory.

the data_array is integer and I am trying to perform an averaging function thus collection of 100 sets data is required.

I would like to assign the 10-bit ADC signal "val" into the array to perform the averaging function, so if the MEGA is running out of memory how can I solve it?

May I use the flash memory to ease this problem with the same sampling rate as 1k Hz(collecting data per 1ms)?

the data_array is integer

So, you think it's OK to store 10,000 bytes in 2,000 bytes of SRAM?

May I use the flash memory to ease this problem

Flash memory is read-only. So, no.

the data_array is integer and I am trying to perform an averaging function thus collection of 100 sets data is required.

Could you not just add to the totals as the data arrives then, after 100 samples, divide the totals by 100 ? No need for a 2 dim array then.

oscar0226: I am trying to use an MEGA to generate a 6Hz pulse

Using fixed delays is not a good way to generate a regular output pulse, because the actual output interval depends on the time taken to execute the code as well as the delay.

I suggest using millis() to measure the interval between pulses and work out when the next pulse needs to occur. The blink without delay example sketch shows how to achieve this using a non-blocking approach, although you could use a blocking approach instead if you wanted.