Arduino as an encoder pulse divider?

It should be within reach to accurately account for 1024 pulses per rev at 3200 RPM with the above code and even handle "fractions" if you need to slow down the pulse rate for your Windows app. Some clever low-level coding however is needed to pull this off for the additional logic needed to support the serial interface.

Note that interrupts are not used in the above code and you want to keep it that way as it is much more efficient when doing high-speed pulse counting.

Hi BenF - I'd read that interrupts were the way to go for efficient code?

I see that a poster Leo suggested interrupt modified code down the page a bit?

I'm hoping not to use serial, but use digitalWrite for the output, but hadn't though that far yet :slight_smile:

Here's how the Xylinx code reads what to do with the input, looks to be similar to the array used in Oleg's code

int8_t enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};

if  (  
            (quada2 = '0' and quada1 = '1' and quadb2 = '0' and quadb1 = '0')  or
            (quada2 = '0' and quada1 = '0' and quadb2 = '1' and quadb1 = '0')  or
            (quada2 = '1' and quada1 = '1' and quadb2 = '0' and quadb1 = '1')  or
            (quada2 = '1' and quada1 = '0' and quadb2 = '1' and quadb1 = '1')) then               
                  qcountup <= '1';
            else 
                  qcountup <= '0';
            end if;                  
            if  ( 
            (quada2 = '0' and quada1 = '0' and quadb2 = '0' and quadb1 = '1')  or
            (quada2 = '0' and quada1 = '1' and quadb2 = '1' and quadb1 = '1')  or
            (quada2 = '1' and quada1 = '0' and quadb2 = '0' and quadb1 = '0')  or
            (quada2 = '1' and quada1 = '1' and quadb2 = '1' and quadb1 = '0')) then
                  qcountdown <= '1';
            else
                  qcountdown <= '0';
            end if;