Electronic – arduino – Using rotary encoders with no interrupt pins

arduinomobile robotrobotics

I little background first..
I have 4 separate wheels in my robot and an encoder on each wheel. I am using an arduino mega 2560 which only has 6 interrupt pins, every code that I've seen either uses 2 interrupt lines per encoder or 1 but then its not that precise.

My idea:
My idea here is that the encoders tick at around 300Hz at maximum speed for my robot so I'm using a timer overflow interrupt to read all of the inputs that encoders are connected to at roughly 1kHz which also gives me plenty of time (1 ms) to check if the inputs changed and do my calculations and then return from the interrupt.

My question:
Is my thinking correct? Will I be able to read properly the encoder position and changes? Is there anything better I could do? Are there any caveats that I should take into account when dealing with this?

Update:
From the discussion in the comments and answers I currently have the two following alternatives:

  1. Stick to the polling idea (increase frequency when necessary)
  2. Use a "shared" interrupt available on every pin (didn't know it was possible)

Best Answer

If you really poll much faster than the encoder moves then this will work. Whether it is a good idea depends on how fast the encoder can actually move and on how important it is that you don't miss clicks. I certainly hate it when I spin a dial to scroll through a long UI list and it moves slowly backwards, although there is no real harm done. If the encoder is feedback from a motor, missing clicks could cause bigger problems, like mechanical damage.

You could use dedicated logic to make a counter, or you could use a different microcontroller that has a built-in encoder peripheral, but for most purposes polling is probably fine if you do it fast enough.