count pulses on pin 5

hallo , after many tests made with interrupt i was not able to count pulses on an arduino pin with a pulse frequency over 200 hz without loosing them.
i looked for a program using internal arduino counter connected to pin 5 to the timer used as a counter. it seem to work and i did some changings but i was not able more to see it working.
may you help me about ?
this is listing
/*
Count pulses on pin 5 via Timer-1
Start with rising edge on pin 2
Stop with falling edge on pin 2
ToDo:

  • deal with overflows
    */

unsigned long counts;

void setup() {
Serial.begin(19200);

// pinMode(2, INPUT); digitalWrite(2, HIGH); // trigger on pin-2 = PD2 = interrupt-0
pinMode(5, INPUT); digitalWrite(5, HIGH); // pulses on pin-5 = PD5 = T1

TCCR0A=0; // setup timer-0
TCCR0B=0;
TIMSK0=0;

TCCR2A=0; // setup timer-2
TCCR2B=0;
TIMSK2=0;

TCCR1A=0; // setup timer-1
TCCR1C=0;
TIMSK1=0;
GTCCR=0;

TCNT1=0; azzero contatore

}

void loop() {
Serial.println ("Pstart...............................");

while (counts<1000) {
TCCR1B=0; // stop count
counts = TCNT1; // read count
Serial.print ("counts ....");// print count
Serial.println (counts);
TCCR1B=7; // start count
delay(100);
}
Serial.println ("end........................")
Serial.print ("counts .");
Serial.println (counts);
TCNT1=0;
}

Hi

I'm surprised that you would be losing count at as low as 200Hz, and after some google-fu found this article The overhead of Arduino Interrupts | Bill Grundmann's Blog which seems to indicate to me that 200kHz should be achievable, but that will rely on the interrupt function being simple and fast. Above you've not included your interrupt code which makes it hard to see where things might have gone wrong.

If by 200Hz you meant a higher frequency (kHz or even mHz?) then an external counter IC might be the best bet? How high do you expect your sampled frequency to go?

Remember when posting your sketch to include it inside CODE tags to make it easier to read (that's the # button above).

Cheers, Geoff