frequency measurement via interrupts, 20 samples of micros() in array

Hi , I’m using arduino uno to measure an input frequency on pin2 (interrupt 0). The idea is to fill an array of 20 values of the micros() function. When there are 20 values written in an array, the interrupt is disabled untill data is analysed in the loop().
I want the data to be analysed for consistancy (in future) before a frequency is calculated ( eg. elapsed time for 20 samples’ divided by the ‘number of samples’)
With code below I have nice and stable readings, exept the first index of the array gives inacurate data. I could ignore the first index of the array, and work with the other measurements. But it’s driving me crazy!!

const int PwmIn=2;
volatile int edgeCounter=0;
volatile unsigned long timeStamps[25];

void setup(){
  Serial.begin(9600);
  pinMode(PwmIn,INPUT);
  attachInterrupt(0, PwmRegist, FALLING);
  timeStamps[0]=0;     //I've seen this initialisation on a tutorial, doesn't help me...
}

void loop(){
  if (edgeCounter>=20){                //measure at least 20 falling edges/interrupts before processing
     detachInterrupt(0);                 //stop measurements and process first
     for (int i=0;i<edgeCounter-1;i++){  //normaal i van 0 tem 19
       Serial.println(timeStamps[i+1]-timeStamps[i]);       /print intervals
     }  
     Serial.println();       /for clarity
     edgeCounter=0;                      //reset for new measurement cycle
     attachInterrupt(0, PwmRegist, FALLING);                        //start measuring again
     }
}
     
void PwmRegist(){        //interrupt service routine
  if (edgeCounter<20){                  //make 20 measurements
    timeStamps[edgeCounter]=micros();                          
    edgeCounter++;                      // increment edgecounter 
  } else {
     //detachInterrupt(0);                    //doesn't work because re-enabled on ISR exit
    //noInterrupts();                         //works but not nescessary
  }
}

this code gives me following measurements(intervals) on ~8kHz, notice how first measurements (timeStamps[0]) deviates:

12
108
108
112
112
108
112
112
112
108
112
112
108
112
112
108
112
112
108

104
108
112
112
108
112
112
108
112
112
108
112
112
108
112
112
108
112
112

40
112
112
112
108
112
108
112
112
108
112
112
108
112
112
112
108
112
112

Your advice is highly appreciated, thank you

When you detach the interrupt handler, the interrupts still happen. When you reattach the handler, it is called if there are pending interrupts of the appropriate type. Detaching and reattaching the handler is not the preferred method. cli() and sei(), to suspend and restart interrupt handling is preferred.

If you want to actually ignore interrupts at times, use a volatile 'ignore' flag. Then you won't get pending interrupts upon re-enabling them. Or you can clear the specific interrupt flag bit by hand before enabling.

Dear Paul and Mark, thank you for your response.

1) using cli and sei didn't work for me because serial communication was stopped, I guess it uses timing interrupts which are stopped by cli

2) using a flag (volatile boolean 'NewMeasurement') that is checked at the start of the ISR, and toggeled after data processing in the loop works: also the first time sample is consistent with the rest. However I'm a bit concerned that this approach takes up a lot of processing time, since the interrupt is not disabled and occurs ~every 100µs, only the code is skipped via an IF statement.

But thanks to your input I understand why the first measurement is not correct: it is indeed due to a pending interrupt. After enabling the interupt, system doens't wait for the next falling edge but does ISR immediately. This also explains why the faulty reading is always lower than the correct readings

Now I know the reason why the first measurement is not correct, I will simply discard the first reading. Being a novice in arduino maybe it's better to use the basic programming language with attach/detach or interrupts() and noInterrupts()?

Thanks a lot!