I have a problem with encoder value.Please help me!

val1 = TCNT5;
      // gear ratio and encoder PPR
      RPM = (val1 / (360*74.8)*60);
      //Serial.println(val1);
      Serial.println(RPM);
      t= 0;

My code above is to calculate RPM from encoder value.So the value of encoder is 0 - 65536 (I read from counter register) But when they reach the max value it reset.I think it because of when the value near the max and next value is reset to zero and count again.How to avoid this problem. Thank you.

Please post a complete program

As you can see when counter count to 65536 it reset to zero if my sampling loop locate between value that near the max and the value when counter reset the value will reset too.

]

My question is how to avoid counter reset the value because when the counter reset my RPM calculation will error

Set TCNT5 to zero at the start of the measurement interval.

If the counter overflows, there is an overflow signal that allows you to correct the count.

To avoid overflow, reduce the count interval, or increase the clock divider so that the counter counts more slowly.

The processor data sheet explains how the counters work.

jremington:
Set TCNT5 to zero at the start of the measurement interval.

If the counter overflows, there is an overflow signal that allows you to correct the count.

To avoid overflow, reduce the count interval, or increase the clock divider so that the counter counts more slowly.

The processor data sheet explains how the counters work.

As you can see in the picture How can i avoid the reset occur.

Answered in reply #4