Coding the Gear Tooth Sensor

We need help with the code to the sensor. We have tried many things but cannot get it to function. If you can help it is greatly appreciated.

This is our code

if (rc_dig_in05 > 0) {
counter = counter + 1;
}
printf(“counter %d”, counter);

And it still won’t count.

p.s. Please show us some code.

First, a question - do you have digital_io_05 set as an input? if not, that may be part of your problem…

Next, i would highly recommend using Kevin Watson’s encoder code (see the sticky at the top of the forum) for doing the gear tooth sensors. It needs a small amount of modification, but not much, and it sets up the GTS as interrupts, which help ensure you won’t miss information. If your code above is in the main user part of your code, the line will only execute once every 26.2ms, which may or may not fall on a pulse from the GTS. an Interrupt, however, will force the code to be called to increment the counter every time the input changes from a 0 to a 1, so you shouldn’t miss any information from the GTS.

Try ROBOTC… it takes care of handling the encoders as interrupts without any additional coding and is easier to use than MPLab!

When you say “Doesn’t count”, do you mean the output displays only zero, or some other crazyness?