how to read pulses from an external interrupt on an arduino uno of every 20ms

Hello everyone,

I am new to this forum and i have an problem with my project so plz help me to get out of it..I shall be very thankful to you.

actually i want to perform several task as follows:-

  1. i had connected an turbine sensor to digital pin 2 .

  2. now i want to count the no of pulses generated for every 20ms

  3. then i want to serially print those values after receiving 'a' on serial port.

below is my code i am using but having problem:-

#include "TimerOne.h"
int flowPin = 2;    
int x = 0;     
volatile int count = 0;   
char b ;
void setup()
 {
  pinMode(flowPin, INPUT);           //Sets the pin as an input
  pinMode(13,OUTPUT);
 
  Timer1.initialize(20000);
  attachInterrupt(0, Flow, RISING);
  Timer1.attachInterrupt(check);
  Serial.begin(9600);  
}

void loop()

{
        if (Serial.available() >0 )
    {
        b = Serial.read ();
 
    } 
 
}


void check()
{
  x= count;

    if (b == 'a')
     {
       Serial.println(x);
     }

  count = 0;
  
  }
 
void Flow()
{
   count++; //Every time this function is called, increment "count" by 1
}

with this code my result of serial.println(x) are not acuurate. So plz help in resolving this problem

with this code my result of serial.println(x) are not acuurate

How do you know ?

because the pulses generated are increasing and decreasing linearly.....but the result on serial monitor is non linear ..

while blowing continuesly air through turbine for several seconds....
i got the following result on serial monitor:-

0
0
0
0
0
0
0
4
10
23
7
1
0
0
0
0
0
3
0
0
0
0
0
0
0
0
0
0
0
0
0
2
3
1
5
4

I would just use micros() for the timing - something like this

void loop() {
   if (micros() - lastIntervalMicros >= 20000UL) {
      lastIntervalMicros += 20000UL;
      noInterrupts(); // pause interrupts to read and write multi-byte values
         pulsesThisPeriod = count;
         count = 0;
      interrupts();
      Serial.println(pulsesThisPeriod);
    }
}

...R