Arduino Mega2560 dosen't proccessing routine command in loop when it send a data via serial port the delay about about 100ms per sending poreccess

I'm currently writing code to read analog values every 7812 microseconds (128 Hz) sampling rate
Then I store the information in an array with 128 values and this takes 1 second to fill the array and then send it through the serial port
The problem is that when the array is full and sent, the processor takes 100 milliseconds to start taking new readings
I mean, when the transmission instructions are executed, it consumes 100 milliseconds, and at this time, the analog reading process does not take place, which means that 100 milliseconds are interrupted every one second in sampling.

float valHam[128];
long  valTime[128];
float input=0;
unsigned long pmicros=micros();
int s1i = 0;
void setup() {

void loop() {
  if (micros() - pmicros >= 7812) {//sampling 
    //Time Array
    valTime[s1i]= millis(); 
    s1i = ++s1i % 128;
    if (s1i == 0) {sendData();}
    pmicros =micros();

void sendData() {
  for(int r=0; r<128; r++) {
    if (r<127) {Serial.print(",");}

  for (int r=0; r<128; r++) {
   if (r<127) {Serial.print(",");}                         

Result Example:
its mean the differnce time into tow reading 100ms (2132-8-2024)=100
can you help me to reduce that 100ms delay in sending?

Use a faster serial speed.

Please remember to use code tags when posting ALL of your code.

Hello, do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation of your ask).


Print becomes blocking if you fill up the outgoing buffer… why do you wait to have 128 measures to start printing? Why don’t you print as you go?

Thanks but i am using 115200 baud rate for serial port and i think it is fast

Not possible to tell that from what you posted, and I don't.

i send it to mobile app via blutooth module and when sending data every 8 milliseconds, the application, I saw sometimes a loss in some values, so I resorted to sending data every 1 second in the form of a packet

It doesn't harm to try a faster speed, does it?

Serial does not send packets… it’s one bit after the other and the Serial hardware does some work for you asynchronously… so you should keep it busy…

Clearly not a complete sketch, so the problem may well be in the mystery part that's missing.

115200bps should be enough to not have this problem with the 100ms delay, I agree.

Thanks above code when compiling the result is shown in picture

Thanks can you explain more how to keep it busy and the benift from it?

analogRead does not return a float, so no need to store the result in one.

pmicros =micros();

The sampling code does not take zero time, so your sample times will slip.
Please stop editing the original post - it's hard to see what is going on.

Ok Thanks
can you rewrite your solution for sampling code as command lines ?

I don't know which shell you're using, so I'm not sure what you mean by command line.

Look, you're printing integer values as floats, so you're wasting three character periods per value printed, because the ".00" is redundant.

how can i fix it ?

1. You have an int type array containing 128 samples which means that you have 128x2-byte (256-byte) information.

2. At Bd = 115200 and frame length of 10, you need about (1/115200)* (2568 + 2562) = 22 ms to transmit the whoe array of Step-1. Why are you saying that it is 100 ms?

3. The 22 ms transmission dealy time of Step-2 could be reduced to about 11 ms ususing Bd = 230400.

OP does not send in binary so you should count the ascii representation, the separators (coma and pipe) and the CR/LF

Just send every time you read and optimize the number of bytes you send…

I am saying 100ms delay but often 143ms delayed
can you review the picture that show the resut from serial mointor and chek the delayed time?

Thanks i will try to do it.