Nano Every Encoder interrupt error

I cannot compile the Basic example included with the Encoder library for the Arduino Nano Every.

I tried changing the encoder pins to 2 and 3 (as these are the interrupt enabled pins on the Nano).

I also tried to emulate the ATMEGA328 pinout.

I get the following error.

Arduino: 1.8.10 (Mac OS X), Board: "Arduino Nano Every, None (ATMEGA4809)"

In file included from /Users/eraxeg/Documents/Arduino/libraries/Encoder/Encoder.h:46:0,
                 from /var/folders/ky/xcrtzkjs3sb124szt8qxns8r0000gn/T/arduino_modified_sketch_718609/Basic.pde:7:
/Users/eraxeg/Documents/Arduino/libraries/Encoder/utility/interrupt_pins.h:202:2: error: #error "Interrupts are unknown for this board, please add to this code"
 #error "Interrupts are unknown for this board, please add to this code"
  ^~~~~
/Users/eraxeg/Documents/Arduino/libraries/Encoder/utility/interrupt_pins.h:205:2: error: #error "Encoder requires interrupt pins, but this board does not have any :("
 #error "Encoder requires interrupt pins, but this board does not have any :("
  ^~~~~
/Users/eraxeg/Documents/Arduino/libraries/Encoder/utility/interrupt_pins.h:206:2: error: #error "You could try defining ENCODER_DO_NOT_USE_INTERRUPTS as a kludge."
 #error "You could try defining ENCODER_DO_NOT_USE_INTERRUPTS as a kludge."
  ^~~~~
Multiple libraries were found for "Encoder.h"
 Used: /Users/eraxeg/Documents/Arduino/libraries/Encoder
exit status 1
Error compiling for board Arduino Nano Every.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

How can I get encoders to work with interrupts on this board?

The Encoder library has not had support for the ATmega4809 microcontroller of the Nano Every added yet.

Since all the microcontroller-specific code in the library is for interrupts, the workaround should be to use the "ENCODER_DO_NOT_USE_INTERRUPTS" mode of the library, as demonstrated in the library's NoInterrupts example.

However, there seems to be a bug in the Encoder library when used in this mode. I believe I have a fix for the bug:

Open /Users/eraxeg/Documents/Arduino/libraries/Encoder/Encoder.cpp in a text editor.

Add the following line to the very top of the file:

#define FROM_ENCODER_CPP

Save the file.

Open /Users/eraxeg/Documents/Arduino/libraries/Encoder/Encoder.h in a text editor.

Change line 43 from:

#if defined(ENCODER_USE_INTERRUPTS) || !defined(ENCODER_DO_NOT_USE_INTERRUPTS)

to:

#if defined(ENCODER_USE_INTERRUPTS) || (!defined(ENCODER_DO_NOT_USE_INTERRUPTS) && !defined(FROM_ENCODER_CPP))

Save the file.

After that, you should be able to use the library with your Nano Every when in "ENCODER_DO_NOT_USE_INTERRUPTS" mode. Make sure to carefully read and understand the comments at the top of the NoInterrupts example.

Thank you pert, it works as expected.

I just proposed a pull to add the Nano Every support into the library : it is merged. With this latest version of the library, there should be no error now :slight_smile:

I can see the code, how do I use this for the Nano Every. I have tried copy and paste to the top of my sketch but I still get the same 'no interrupt errors'