Reading a rotary encoder on Arduino Zero

I just upgraded a project form Uno to Zero. The rotary encoder libraries that work with UNO are not available for zero. I was using the AdaEncoder. When I try to load the sample code (MyEncoder.ino) to the zero I get this error:

Warning: platform.txt from core 'Arduino SAMD (32-bits ARM Cortex-M0+) Boards' contains deprecated"{compiler.path}{}" {} {} "{build.path}/{archive_file}" "{object_file}", automatically converted to"{compiler.path}{}" {} {} "{archive_file_path}" "{object_file}". Consider upgrading this core.
In file included from /home/justin/code/VeloKey/arduino/libraries/AdaEncoder/Examples/MyEncoder/MyEncoder.ino:26:0:
/home/justin/code/VeloKey/arduino/libraries/ooPinChangeInt/ooPinChangeInt.h:85:20: fatal error: new.h: No such file or directory
    #include <new.h>
compilation terminated.
exit status 1
Error compiling.

When I try to “roll my own” encoder code (attached) using interrupts, it looks like pulses are missed. Are there any quadrature encoder libraries out there that I can use out of the box with Zero?


encoder_int_test.ino (1.88 KB)

I have little idea but I see one thing: try moving "int last_pos = 0;" outside of loop(). Oh sorry, I looked closer and see you have a while() in your loop().

try to have an interrupt only at one pin with raising edge, and in the routine read the other pin : if the other pin is high it is one direction, if the other pin is low it's the other. You will then have less interrupts.

Thanks you guys. Your solution makes sense to me @AloyseTech, but I can’t get it to work. I’ve tried adding debounce, detecting rising alone and falling edges alone. Still goes backward a good portion of the time and skips ahead or behind by many positions. Here is the latest output from turning in the CW direction.

old new direction

0 1 +
1 -1 -
-1 1 +
1 2 +
2 -4 -
-4 -3 +
-3 -7 -
-7 31 +
31 32 +
32 25 -
22 20 -
20 21 +
21 15 -
13 15 +

I’ve attached the latest.

encoder_int_test.ino (1.06 KB)

Probably too late for you, but for others it might help: I think it's a "hardware" problem. May be your encoder is a 5V model, connected here to 3.3V, and the high pulses generated are not strong enough to be considered as high values by the zero, sometimes they are, sometimes they are not, this would explain why it misses some values and does skip some values. I would try to connect an oscilloscope to see the voltage levels generated when turning the knob, read the zero datasheet to see what is the minimum voltage to be considered as High (something like 0.6*VCC or 0.7*VCC) and boost the voltage level with a transistor, may be with a diode to prevent too much higher voltages: