Problems counting encoder pulses

We are having problems counting pulses with our encoders, even at very low speeds. We aren’t sure whether this is a problem in the code or with the hardware, but our counting problem is so bad that we can watch the controller missing pulses on hyperterminal. Is anyone else having this problem?

Do you know how fast you are sampling the inputs?
How about how fast the encoder shaft is turning?

Without seeing your code it is hard to diagnose.

If you are reading the inputs only once every 26ms, you are very likely to miss pulses.

best to put sensor-checking code (if you are using a polling method) in user_routines_fast.c

the best way (if possible) to poll such sensors would probably be interrupts.

Our code is in user_routines_fast. We tried adding another variable to the code to count the loops when the code did not detect a high signal, and usually we looped several thousand times (at least) between “hits.” As I said, we were going VERY slowly. Thanks for your ideas, though. We appreciate any help you can give us.

You might want to consider using an external counting circuit to count pulses and store them in some sort of register that can be read by the RC at any time. Before doing that, however, I would recommend modifying your code so that it uses interrupts to count pulses - this would eliminate any problems caused by the fairly low sample rate imposed by the rate at which the main loop executes.

We have a custom circuit that converts the frequency of pulses to an analog signal from 0 to 5v. With the counting circuitry independent from the control loop in software, it is easier to isolate problems and work on the “real” programming :wink:

Even if your code is running in user_routines_fast.c you can still miss pulses. Remember that everything comes to a screeching halt when it is time to run the Process_Data_From_Master_uP(). The most reliable way to count pulses is with an interrupt-driven routine. Perhaps you could look at Kevin Watson’s example code at
But if you really are turning the shaft very slowly (much greater than 26ms between pulses) it sound like you’ve got some other bug.
Why not post the code here?

Yes, there is an example written for the EDU-RC that should be easilly ported to the FRC-RC. Just drop encoder.c/.h into your project and modify user_routines_fast.c to call the interrupt handlers. The interrupt handlers are very efficient and should have no problem with many hundreds of counts per second (I’ve successfully done five thousand/sec).