timer library

I want to count pulses in a fixed time. I have managed to write a suitable sketch for counting pulses using interrupt. next step is to count pulses for 10 seconds. I thought that including a timer library and initializing timer for 10 seconds would do job but it didn't. Timer interrupt needs only one argument and the sketch I have written has three arguments for counting pulses. Please suggest some solution.

What do you want to happen after the 10 seconds? Stop counting?
In your main loop you could capture the time, attach the interrupt and after 10 seconds detach the interrupt and have a look at the counter (volatile) from the interrupt. Would that suit your needs?

My crystal ball says that the problem is on line 42 of the code that was not posted.

Well, since you can never have a portion of a pulse, counting them over a fixed time interval will always yeild ±1 pulse error. If the pulse rate is 1Hz, this would represent ±10% error; 10Hz —> ±1% error, etc. So this method works well for higher frequencies, depending on what your accuracy requirements are.

Another method is to just count a fixed number of pulses, then measure the elapsed time. This works well for lower frequencies, but measurement interval can become excessive when the rate is <1Hz, so a timeout might be needed. Also, at higher frequencies, the measurement interval becomes shorter and the timing resolution would become an issue.

My favorite way to do pulse and timing measurements is a hybrid of the above that’s limited only by the hardware.

  • For example, all that’s needed is to set a measurement interval, say 100ms.
  • If detecting rising edges, the first edge would start the measurement but NOT be counted. The startTime is recorded.
  • Each successive rising edge would would then be counted.
  • Once the 100ms measurement interval has elapsed, then the next rising edge would stop the test. The totalCount and stopTime is recorded.The readings will now be as accurate and precise as the timing hardware with no measurement errors added. Also, the input frequency range would only be limited by hardware, and not by the method used for measurement.