Code Monkey home page Code Monkey logo

encoder's Introduction

encoder's People

Contributors

acropup avatar adamthesax avatar aftersomemath avatar alex-code avatar aster94 avatar dmadison avatar gagarcr avatar humanoid2050 avatar ivankravets avatar jromang avatar ladyada avatar lumostor avatar m-reimer avatar nopdotcom avatar paulstoffregen avatar pheipheiphei avatar planeturban avatar skullkill avatar smarpug avatar wasdee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

encoder's Issues

Support for STM32

Description

Tried to use the lib with STM32 bluepill , it seems that it is not yet supported
here is the build error: #error "Interrupts are unknown for this board, please add to this code"

For some reason this library is tagged as supporting STM32 in platformio documentation.

Do you have plans to support it soon?
Is it a matter of only adding a new define segment in the interrupt_pins.h header file?

ESP32 compiles but it is not accurate

There is no point on posting this on EXPRESSIF.COM

For me compiles perfect. The problem is that for each encoder rotation (1 point) I get multiples values and most of the times is not clean in terms of sequential move like:

33
34
33
34
33
34
35
36

This represents 1 move... How can accurately get a proper and unified value for every rotation point ?

4 steps in one

hello Paul,

i m trying your library both on arduino uno and attiny167
for some reason i get 4 steps for every movements of the rotary encoder
the encoder is one of the cheap everyone use from aliexpress and i am not using any pull up/down/caps

to have your lib working i am using myEnc.read()/4 do you know why is this happening?

cheers

Z signal detection

Thanks Paul for a very useful library.
Would it be possible to add another interrupt from the Z signal to preset a position value? This would be useful when dealing with positioning systems etc.
I have looked through your code but it is way over my level of knowledge.

Thanks again!
/Martin

Interrupt issue with read function with Due but not Mega

Description

Having an issue with basic encoder functionality using Arduino Due specifically (code works on Arduino Mega no problem). It can be worked around by not using interrupts. However, it would be good for my application to be able to use interrupts.

Steps To Reproduce Problem

I used the attached code to perform the read function from this library. This works no problem on the Arduino Mega. However, on the Arduino Due, the read function works only when I use #define ENCODER_DO_NOT_USE_INTERRUPTS. When that line is not included, the CLK and DT values update as expected for the Due, but myEnc.read() returns a constant 0.

Hardware & Software

Arduino Mega 2560 R3 and Arduino Due
Arduino IDE 2.0.0-rc2
Encoder.h 1.4.2, Arduino SAM Boards (32-bits ARM Cortex-M3) 1.6.12 for Due, and Arduino AVR Boards 1.8.4 for Mega
Windows 10

Arduino Sketch

#define ENCODER_DO_NOT_USE_INTERRUPTS // this line is required for Due to function properly, can be disabled for Mega
#include <Encoder.h>

// Encoder library available at https://www.pjrc.com/teensy/td_libs_Encoder.html
// This should automatically set these pins to interrupt mode
#define ENCODER_CLK_PIN 19
#define ENCODER_DT_PIN 18
Encoder myEnc(ENCODER_DT_PIN, ENCODER_CLK_PIN);

int previous_encoder_pos = 0;
int current_encoder_pos = 0;

void setup(void) {
  Serial.begin(9600);
  while (!Serial)
    delay(10); // will pause Zero, Leonardo, etc until serial console opens

  Serial.println("Encoder test!");
}

void loop() { 

  current_encoder_pos = myEnc.read();

  if (current_encoder_pos > previous_encoder_pos){
    Serial.print("DT: ");
    Serial.println(digitalRead(ENCODER_DT_PIN));
    Serial.print("CLK: ");
    Serial.println(digitalRead(ENCODER_CLK_PIN));
    Serial.print("current_encoder_pos = ");
    Serial.println(current_encoder_pos);
  }
  else if (current_encoder_pos < previous_encoder_pos){
    Serial.print("DT: ");
    Serial.println(digitalRead(ENCODER_DT_PIN));
    Serial.print("CLK: ");
    Serial.println(digitalRead(ENCODER_CLK_PIN));
    Serial.print("current_encoder_pos = ");
    Serial.println(current_encoder_pos);
  }

  previous_encoder_pos = current_encoder_pos;

}

Release 1.4

Hi Paul,

I tried use your library in version 1.3 on ESP8266, however I noticed that some defines are missing. The problem was already solved by @acropup. I tested the latest version from the master branch and it works fine with my ESP8266. May I ask you to release a new version so Arduino IDE can index it? It would be very helpful.

Cheers,
Artur

ENCODER_OPTIMIZE_INTERRUPTS not working for ATmega4809

Description

ENCODER_OPTIMIZE_INTERRUPTS do not work for ATmega4809 (Nano Every)

When I use the optimized source code, I no longer get any values for .read ();

it works without "ENCODER_OPTIMIZE_INTERRUPTS".

Steps to reproduce the problem

Two 2400 step encoders on an Arduino Nano Every.
Latest encoder library from Github from August 9th, 2020

board
Arduino Nano Every

Arduino IDE version
1.8.10

Operating system & version
Mac OS 10.14.6

Arduino Sketch

`
//#define ENCODER_OPTIMIZE_INTERRUPTS
#include <Encoder.h>
#include <Wire.h>

#define enc1P1 2
#define enc1P2 4
#define enc2P1 3
#define enc2P2 5
#define bit4 A0
#define bit3 A1
#define bit2 A2
#define bit1 A3
#define led 6

Encoder knobRot(enc1P1, enc1P2);
Encoder knobX(enc2P1, enc2P2);

typedef struct {
int16_t x;
int16_t rot;
}
B_t;
B_t data;

byte datasend = 0;
byte i2cAddress = 1;
boolean ledState = false;
byte ledblink;

void setup() {
pinMode(bit1, INPUT_PULLUP);
pinMode(bit2, INPUT_PULLUP);
pinMode(bit3, INPUT_PULLUP);
pinMode(bit4, INPUT_PULLUP);
pinMode(led, OUTPUT);
digitalWrite(led, HIGH);
bitWrite(i2cAddress, 0, !digitalRead(bit1));
bitWrite(i2cAddress, 1, !digitalRead(bit2));
bitWrite(i2cAddress, 2, !digitalRead(bit3));
bitWrite(i2cAddress, 3, !digitalRead(bit4));

Wire.begin(i2cAddress); // join i2c bus with address #8
Wire.setClock(400000);
Wire.onRequest(requestEvent); // register event
data.x = 0; // assign a value to the long number
data.rot = 0; // assign a value to the long number
delay(1000);
digitalWrite(led, LOW);
}

void loop() {
int32_t newX, newRot, tempRot;
newRot = knobRot.read();
newX = knobX.read();
if(newRot<0){
tempRot = newRot % 2400;
data.rot = 2400+tempRot;
}else{
data.rot = newRot % 2400;
}
data.x = newX;
Serial.print("absX= ");
Serial.print(data.x);
Serial.print(", absRot= ");
Serial.print(data.rot);
Serial.print(", absRot= ");
Serial.println(newRot);
//delay(1);
}

void requestEvent() {
datasend = Wire.write ((byte ) &data, sizeof data); / sends data ti Master */
//data.rot = 0;
//data.x = 0;
}`

Errors or Incorrect Output

If you see any errors or incorrect output, please show it here. Please use copy & paste to give an exact copy of the message. Details matter, so please show (not merely describe) the actual message or error exactly as it appears.

Perpetual reboot on ESPs (known solution that needs to be committed) => use RLars Fork !

This issue has been pointed a year ago... (version 1.4.1) on issue#40

ESPs crashes and reboots permanently :

 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 1392, room 16 
tail 0
chksum 0xd0
csum 0xd0
v3d128e5c
~ld

The solution found by odudex is to add ICACHE_RAM_ATTR ahead of each Interrupt Handler

Just missing these changes to be committed...

Is anyone able to do that?

-OR-

Is there a viable Fork that includes this ?

thx

Not working with Arduino Nano BLE 33 with Interrupts or without Interrupts

Description

Im getting error messages trying to use this library with the new Arduino Nano BLE 33.

If I use the example with interrupts I get the error:

Interrupts are unknown for this board, please add to this code.

If use the no interrupts example I get the error message posted below.

Steps To Reproduce Problem

See error message bellow

Hardware & Software

Board
Arduino Nano BLE 33

Arduino IDE version
1.8.10

Version info & package name (from Tools > Boards > Board Manager)
Arduino Nano BLE 33

Operating system & version
Mac OS 10.15

Arduino Sketch

// Change the code below by your sketch (please try to give the smallest code which demonstrates the problem)
#define ENCODER_DO_NOT_USE_INTERRUPTS
#include <Encoder.h>
Encoder myEnc(5, 6);

void setup() {
  Serial.begin(9600);
  Serial.println("Basic NoInterrupts Test:");
}

long position  = -999;

void loop() {
  long newPos = myEnc.read();
  if (newPos != position) {
    position = newPos;
    Serial.println(position);
  }
}

Errors or Incorrect Output

In file included from /Users/USERNAME/Documents/Arduino/libraries/Encoder/examples/NoInterrupts/NoInterrupts.pde:14:0:
/Users/USERNAME/Documents/Arduino/libraries/Encoder/Encoder.h:61:11: error: 'IO_REG_TYPE' does not name a type
  volatile IO_REG_TYPE * pin1_register;
           ^~~~~~~~~~~
/Users/USERNAME/Documents/Arduino/libraries/Encoder/Encoder.h:62:11: error: 'IO_REG_TYPE' does not name a type
  volatile IO_REG_TYPE * pin2_register;
           ^~~~~~~~~~~
/Users/USERNAME/Documents/Arduino/libraries/Encoder/Encoder.h:63:2: error: 'IO_REG_TYPE' does not name a type
  IO_REG_TYPE            pin1_bitmask;
  ^~~~~~~~~~~
/Users/USERNAME/Documents/Arduino/libraries/Encoder/Encoder.h:64:2: error: 'IO_REG_TYPE' does not name a type
  IO_REG_TYPE            pin2_bitmask;
  ^~~~~~~~~~~
/Users/USERNAME/Documents/Arduino/libraries/Encoder/Encoder.h: In constructor 'Encoder::Encoder(uint8_t, uint8_t)':
/Users/USERNAME/Documents/Arduino/libraries/Encoder/Encoder.h:82:11: error: 'struct Encoder_internal_state_t' has no member named 'pin1_register'
   encoder.pin1_register = PIN_TO_BASEREG(pin1);
           ^~~~~~~~~~~~~
/Users/USERNAME/Documents/Arduino/libraries/Encoder/Encoder.h:82:27: error: 'PIN_TO_BASEREG' was not declared in this scope
   encoder.pin1_register = PIN_TO_BASEREG(pin1);
                           ^~~~~~~~~~~~~~
/Users/USERNAME/Documents/Arduino/libraries/Encoder/Encoder.h:82:27: note: suggested alternative: 'INIT_OBSERVER'
   encoder.pin1_register = PIN_TO_BASEREG(pin1);
                           ^~~~~~~~~~~~~~
                           INIT_OBSERVER
/Users/USERNAME/Documents/Arduino/libraries/Encoder/Encoder.h:83:11: error: 'struct Encoder_internal_state_t' has no member named 'pin1_bitmask'
   encoder.pin1_bitmask = PIN_TO_BITMASK(pin1);
           ^~~~~~~~~~~~
/Users/USERNAME/Documents/Arduino/libraries/Encoder/Encoder.h:83:26: error: 'PIN_TO_BITMASK' was not declared in this scope
   encoder.pin1_bitmask = PIN_TO_BITMASK(pin1);
                          ^~~~~~~~~~~~~~
/Users/USERNAME/Documents/Arduino/libraries/Encoder/Encoder.h:83:26: note: suggested alternative: 'PIN_SPI_MOSI'
   encoder.pin1_bitmask = PIN_TO_BITMASK(pin1);
                          ^~~~~~~~~~~~~~
                          PIN_SPI_MOSI
/Users/USERNAME/Documents/Arduino/libraries/Encoder/Encoder.h:84:11: error: 'struct Encoder_internal_state_t' has no member named 'pin2_register'
   encoder.pin2_register = PIN_TO_BASEREG(pin2);
           ^~~~~~~~~~~~~
/Users/USERNAME/Documents/Arduino/libraries/Encoder/Encoder.h:85:11: error: 'struct Encoder_internal_state_t' has no member named 'pin2_bitmask'
   encoder.pin2_bitmask = PIN_TO_BITMASK(pin2);
           ^~~~~~~~~~~~
/Users/USERNAME/Documents/Arduino/libraries/Encoder/Encoder.h:92:31: error: 'struct Encoder_internal_state_t' has no member named 'pin1_register'
   if (DIRECT_PIN_READ(encoder.pin1_register, encoder.pin1_bitmask)) s |= 1;
                               ^~~~~~~~~~~~~
/Users/USERNAME/Documents/Arduino/libraries/Encoder/Encoder.h:92:54: error: 'struct Encoder_internal_state_t' has no member named 'pin1_bitmask'
   if (DIRECT_PIN_READ(encoder.pin1_register, encoder.pin1_bitmask)) s |= 1;
                                                      ^~~~~~~~~~~~
/Users/USERNAME/Documents/Arduino/libraries/Encoder/Encoder.h:92:7: error: 'DIRECT_PIN_READ' was not declared in this scope
   if (DIRECT_PIN_READ(encoder.pin1_register, encoder.pin1_bitmask)) s |= 1;
       ^~~~~~~~~~~~~~~
/Users/USERNAME/Documents/Arduino/libraries/Encoder/Encoder.h:93:31: error: 'struct Encoder_internal_state_t' has no member named 'pin2_register'
   if (DIRECT_PIN_READ(encoder.pin2_register, encoder.pin2_bitmask)) s |= 2;
                               ^~~~~~~~~~~~~
/Users/USERNAME/Documents/Arduino/libraries/Encoder/Encoder.h:93:54: error: 'struct Encoder_internal_state_t' has no member named 'pin2_bitmask'
   if (DIRECT_PIN_READ(encoder.pin2_register, encoder.pin2_bitmask)) s |= 2;
                                                      ^~~~~~~~~~~~
/Users/USERNAME/Documents/Arduino/libraries/Encoder/Encoder.h:93:7: error: 'DIRECT_PIN_READ' was not declared in this scope
   if (DIRECT_PIN_READ(encoder.pin2_register, encoder.pin2_bitmask)) s |= 2;
       ^~~~~~~~~~~~~~~
/Users/USERNAME/Documents/Arduino/libraries/Encoder/Encoder.h: In static member function 'static void Encoder::update(Encoder_internal_state_t*)':
/Users/USERNAME/Documents/Arduino/libraries/Encoder/Encoder.h:274:40: error: 'struct Encoder_internal_state_t' has no member named 'pin1_register'
   uint8_t p1val = DIRECT_PIN_READ(arg->pin1_register, arg->pin1_bitmask);
                                        ^~~~~~~~~~~~~
/Users/USERNAME/Documents/Arduino/libraries/Encoder/Encoder.h:274:60: error: 'struct Encoder_internal_state_t' has no member named 'pin1_bitmask'
   uint8_t p1val = DIRECT_PIN_READ(arg->pin1_register, arg->pin1_bitmask);
                                                            ^~~~~~~~~~~~
/Users/USERNAME/Documents/Arduino/libraries/Encoder/Encoder.h:274:19: error: 'DIRECT_PIN_READ' was not declared in this scope
   uint8_t p1val = DIRECT_PIN_READ(arg->pin1_register, arg->pin1_bitmask);
                   ^~~~~~~~~~~~~~~
/Users/USERNAME/Documents/Arduino/libraries/Encoder/Encoder.h:275:40: error: 'struct Encoder_internal_state_t' has no member named 'pin2_register'
   uint8_t p2val = DIRECT_PIN_READ(arg->pin2_register, arg->pin2_bitmask);
                                        ^~~~~~~~~~~~~
/Users/USERNAME/Documents/Arduino/libraries/Encoder/Encoder.h:275:60: error: 'struct Encoder_internal_state_t' has no member named 'pin2_bitmask'
   uint8_t p2val = DIRECT_PIN_READ(arg->pin2_register, arg->pin2_bitmask);
                                                            ^~~~~~~~~~~~
Mehrere Bibliotheken wurden fรผr "Encoder.h" gefunden
 Benutzt: /Users/USERNAME/Documents/Arduino/libraries/Encoder
exit status 1
Fehler beim Kompilieren fรผr das Board Arduino Nano 33 BLE.

Arduino 101 (Intel Curie Chip)

Can an Arduino 101 be used... It has support for external interupts on all of its pins... So can I use more than the default 2 that are on the ATmega328P (Arduino UNO)?

Amazing library! Is there an example Windows serial interface anywhere?

This library is absolutely fantastic, and I have used it to successfully print encoder readout to the serial plotter easily at 1kHz. However, implementing a serial interface in c++ in windows that can handle all that data has proved to be more difficult than I had expected. Even with multi-threaded asynchronous serial, I'm having trouble. Just wondering if there is some sample code that might help me get started. I know this is theoretically possible even on my crap laptop, because the Serial Plotter handles the data just fine without overflows, dropped data, etc.

Any help would be greatly, greatly appreciated!

LICENSE

Hi there, first of all thanks for working on this great library. Just wondering what's the project's license like? Is it okay to use this library for a project with BSD license? Thanks.

undercounting steps only in one direction, only when going slowly, with a linear encoder

Description

I have a linear encoder (x axis dro from a mill) which has 5000 steps per inch. I've confirmed it a quadrature encoder and i have the pinout correct. When using the encoder library with pins 2 and 3 on an elegoo uno r3, it reads correctly one direction, but in the other direction is vastly undercounts- but oddly, only if im moving it slowly. if i move the encoder head quickly in the problem direction, it counts the steps fairly accurately.

Steps To Reproduce Problem

probably cant since you dont have my specific encoder- unless you have a random chinese import sgc-6 500mm linear encoder

Hardware & Software

Board: Elegoo uno r3
Arduino IDE version: 2.0.3

Arduino Sketch

#include <Encoder.h>
Encoder my_enc(2, 3);

void setup() {
  Serial.begin(9600);
}
void loop() {
  Serial.println(my_enc.read());
}

Wrong sign after sleep mode on Atmel SAM

I'm using Encoder library on Adafruit Feather M0 (Atmel SAM) with PlatformIO on Visual Studio Code. My test program runs a DC motor with a quadrature encoder in the same direction. In normal power mode, the counting is accurate. When I put the microcontroller into sleep at each iteration of the loop, the count changes its sign, which is an error. Here is the code:

#include <Arduino.h>
#include <Adafruit_SleepyDog.h>
#include <Encoder.h>

constexpr int ENCODER_VCC = 11;
constexpr int MOTOR_ENABLE = 10;
constexpr int MOTOR_PHASE = 6;
constexpr int MOTOR_SLEEP = 5;

Encoder encoder(12, 13);

void setup()
{
  pinMode(LED_BUILTIN,  OUTPUT);
  pinMode(ENCODER_VCC, OUTPUT);
  pinMode(MOTOR_ENABLE, OUTPUT);
  pinMode(MOTOR_PHASE,  OUTPUT);
  pinMode(MOTOR_SLEEP,  OUTPUT);
  digitalWrite(ENCODER_VCC, LOW);
  digitalWrite(MOTOR_ENABLE, LOW);
  digitalWrite(MOTOR_PHASE,  LOW);
  digitalWrite(MOTOR_SLEEP,  LOW);

  Serial.begin(9600);
  Serial.println("Basic Encoder Test:");
}

void loop()
{
  digitalWrite(LED_BUILTIN,  HIGH);
  digitalWrite(ENCODER_VCC,  HIGH);
  digitalWrite(MOTOR_ENABLE, HIGH);
  digitalWrite(MOTOR_PHASE,  LOW);
  digitalWrite(MOTOR_SLEEP,  HIGH);

  delay(5000);

  digitalWrite(LED_BUILTIN,  LOW);
  digitalWrite(ENCODER_VCC,  LOW);
  digitalWrite(MOTOR_ENABLE, LOW);
  digitalWrite(MOTOR_PHASE,  LOW);
  digitalWrite(MOTOR_SLEEP,  LOW);

  Serial.println(encoder.read());
  // Watchdog.sleep(5000);
  delay(5000);
}

Incorrect output with Watchdog.sleep(5000);:

-8831
17677
-26537
35401
-44269

Correct output with delay(5000);:

-8855
-17723
-26595
-35461
-44335

ESP32 Not working.

I can't get the library working on an ESP32 lolin. I see that ESP32 is in the .h files. But for some reason it's not working. Here's an excerpt of the errors.

ESP32 is defined, because if i try to define it it says I'm redefining it. No idea how to solve it.

Compiling .pioenvs/lolin32/FrameworkArduino/HardwareSerial.o
In file included from /home/myuser/.platformio/lib/Encoder_ID129/Encoder.h:46:0,
from src/main.cpp:7:
/home/myuser/.platformio/lib/Encoder_ID129/utility/interrupt_pins.h:202:2: error: #error "Interrupts are unknown for th
is board, please add to this code"
#error "Interrupts are unknown for this board, please add to this code"
^
/home/myuser/.platformio/lib/Encoder_ID129/utility/interrupt_pins.h:205:2: error: #error "Encoder requires interrupt pi
ns, but this board does not have any :("
#error "Encoder requires interrupt pins, but this board does not have any :("
^
^
/home/myuser/.platformio/lib/Encoder_ID129/utility/interrupt_pins.h:206:2: error: #error "You could try defining ENCODE
R_DO_NOT_USE_INTERRUPTS as a kludge."
#error "You could try defining ENCODER_DO_NOT_USE_INTERRUPTS as a kludge."
^
In file included from src/main.cpp:7:0:
/home/myuser/.platformio/lib/Encoder_ID129/Encoder.h:61:11: error: 'IO_REG_TYPE' does not name a type
volatile IO_REG_TYPE * pin1_register;
^
/home/myuser/.platformio/lib/Encoder_ID129/Encoder.h:62:11: error: 'IO_REG_TYPE' does not name a type
volatile IO_REG_TYPE * pin2_register;
^

#define INPUT_PULLUP /Arduino Due

If I #define INPUT_PULLUP an error occure.

Arduino: 1.8.2 (Windows 10), Board: "Arduino Due (Programming Port)"

In file included from C:\Users\gabri\Google Drive\Arduino\Programme________\achse5test.ino:12:0:

C:\Users\gabri\Google Drive\Arduino\libraries\Encoder-master/Encoder.h: In constructor 'Encoder::Encoder(uint8_t, uint8_t)':

C:\Users\gabri\Google Drive\Arduino\libraries\Encoder-master/Encoder.h:74:29: error: expected primary-expression before ')' token

pinMode(pin1, INPUT_PULLUP);

                         ^

C:\Users\gabri\Google Drive\Arduino\libraries\Encoder-master/Encoder.h:75:29: error: expected primary-expression before ')' token

pinMode(pin2, INPUT_PULLUP);

                         ^

exit status 1
Fehler beim Kompilieren fรผr das Board Arduino Due (Programming Port).

Dieser Bericht wรคre detaillierter, wenn die Option
"Ausfรผhrliche Ausgabe wรคhrend der Kompilierung"
in Datei -> Voreinstellungen aktiviert wรคre.

Is this my fault or an error in that library ?

Thanks
Gabriel

Is a new release coming?

Description

Current release 1.4.1 does not include a number of recent updates like ESP32 support. This means that for users of PlatformIO and Arduino IDE's the latest library does not match the current master branch and does not work for them.

Steps To Reproduce Problem

Install Encoder from the Library Manager in PlatformIO or Arduino IDE and you will get the 1.4.1 release version that does not have the recent updates.

Steps to work around the problem in PlatformIO

add a

lib_deps=https://github.com/PaulStoffregen/Encoder.git

to your platformio.ini

Sanguino Interrupt define incomplete

Hi

I would suggest to add 1284(P) support by adding " || defined(AVR_ATmega1284P) || defined(AVR_ATmega1284)" to line 59 in interrupt_pins.h.

Currently working on a project in PlatformIO with an Encoder and a 1284P and meanwhile I'm doing this via build options:

-D CORE_NUM_INTERRUPT=3
-D CORE_INT0_PIN=10
-D CORE_INT1_PIN=11
-D CORE_INT2_PIN=2

I'd do a pull request but unfortunately I'm too stupid for that.
Thanks!

Compile errors when using Teensy4

Description

Encoder example won't compile for Teensy4

Steps To Reproduce Problem

select Teensy 4 and compile Basic example

Hardware & Software

Teensy 4
Arduino IDE version 1.8.9
Teensyduino version 1.47
Windows 10

Error

C:\Users\Michael\Documents\Arduino\libraries\Encoder/Encoder.h: In constructor 'Encoder::Encoder(uint8_t, uint8_t)':

C:\Users\Michael\Documents\Arduino\libraries\Encoder/Encoder.h:82:25: error: cannot convert 'volatile uint32_t* {aka volatile long unsigned int*}' to 'volatile uint8_t* {aka volatile unsigned char*}' in assignment

   encoder.pin1_register = PIN_TO_BASEREG(pin1);

Teensy 3.2 / LCDMenuLib2 / Rotary Encoder Interrupts

With the Arduino IDE 1.8.9 and Tinsyduino 1.46 I uploaded the example from the LCDMenuLib2 library on a Teensy 3.2.

=> https://github.com/Jomelo/LCDMenuLib2/tree/master/examples/03_displaytypes/gfx/LCDML_adafruit_gfx_ssd1306

With the help of an Oled Display SSD1306 (i2C) the menu from the example can be displayed, but the Rotary Encoder only works if I consider the option:

=> #define ENCODER_DO_NOT_USE_INTERRUPTS

(I have deactivated the interrupts from the example >>here<<)

Navigating with the Rotary Encoder without interrupts will (of course) "forget" some steps and the navigation will be almost unusable.

Does anyone know a workaround so that the interrupts can be used or does anyone have a suggestion why the navigation doesn't work anymore when the interrupts are activated?

(Same setup on basis of a Node MCU (ESP8266) and the adjustment from the Issue 40 works fine.)

Adding ESP8266 Compatibility

Hi Paul,

First: You are awesome and I'm super impressed by all you've done for the community. I love the Teensy 3.1 and it is my go-to for prototyping. I'm excited to have something to contribute back to you. ๐Ÿ˜

I wanted to use a rotary encoder on an ESP8266 (I'm using a NodeMCU 0.9), and I have it working now with pin interrupts. I'd like your opinion on where some of the code changes should go. In interrupt_pins.h, you write:

// Teensy (and maybe others) define these automatically
#if !defined(CORE_NUM_INTERRUPT)

I don't know how standard it is for a board to make these definitions, or for libraries to use them. Should I propose to the ESP8266 repo that we add those defines, or should we just add them to this library? They're all pretty straightforward, as far as I can tell. I've read that all GPIO except GPIO16 support pin interrupts, but I'll have to look more into that, as the datasheets I've found don't get into that sort of detail. It'll look something like this:

[...]
// ESP8266 (https://github.com/esp8266/Arduino/)
#elif defined(ESP8266)
  #define CORE_NUM_INTERRUPT EXTERNAL_NUM_INTERRUPTS
  #define CORE_INT0_PIN     0
  #define CORE_INT1_PIN     1
  #define CORE_INT2_PIN     2
[...]

In the process, I also found a small bug affecting the ESP8266 portInputRegister() definition, which I'll look at hopefully getting fixed before updating direct_pin_read.h.

RP Pico RP2040 ConfigurableFirmata Encoder Interrupts not working

Hello! been using ConfigurableFirmata V2.10.1 based from Firmatabuilder.com sketches successfully with Arduino.

Ive added support for RP2040 how is described here:
https://github.com/firmata/ConfigurableFirmata/blob/3a1747953150b6589b7d5bf1f3aa92c5aca3c365/BoardSupport.md

Everything works fine however ive notice that if i plug in an encoder it does not work accurately like it does in the Arduino nano port 2/3 that are interrupts pins.

In theory all RP2040 digital pins are interrupts but they are not behaving like interrupt pins but like normal pins therefore making the encoder not read accurately.

ive modified the encoder library here:
https://github.com/PaulStoffregen/Encoder/search?q=RP2040

to use
#elif defined(TARGET_RP2040) || defined(TARGET_RASPBERRY_PI_PICO)
instead of
#elif defined(ARDUINO_NANO_RP2040_CONNECT)

Its compiling fine however not working accurately, please help!! thanks

Can't use interrupt pins on Arduino/Genuino 101

Hi there.

I use this library on my Arduino/Genuino 101.
When only use "change" supported interrupt pins, such as 7&8, Encoder instance looks like doesn't count pulses.
It is properly counting when I use "change" not supported pins.
How I use "change" supported interrupt pins on this library?

Best regards.

Add support for nRF52 boards

Can I offer 0.03 bitcoin (~$40) towards making a new release of your Encoder library with support for nRF52 boards?

Specifically, I'm trying to run an Arduino sketch on a Adafruit Feather nRF52 Bluefruit - there's no helper chip (eg ATmega32u4 or ATSAMD21) with this product, instead code is run directly on the nRF52832.

Having successfully used your Encoder library with other boards, I've had a couple of compilation errors compiling for thenRF52:

  • libraries/Encoder/utility/interrupt_pins.h:202:2: error: #error "Interrupts are unknown for this board, please add to this code"
  • libraries/Encoder/Encoder.h:61:11: error: 'IO_REG_TYPE' does not name a type

...new values need adding to interrupt_pins.h & direct_pin_read.h, but I'm inexperienced in this area and even after a bit of digging around the board definition in adafruit/Adafruit_nRF52_Arduino I still wasn't sure what to add ๐Ÿ˜ž

If you could make a new release of your library with this support I'd appreciate it - if necessary, you can put the Bitcoin towards purchase of a Feather nRF52 for testing!

[ Feature Request ] : Seeeduino XIAO : support hardware interrupts

Description

The Seeeduino XIAO doesn't play nice with this library. Works great on an arduino nano, with hardware interrupts, but fails to pick up any inputs when ported to the xiao.

Steps To Reproduce Problem

  • Flash example sketch to xiao

  • Rotate encoder

  • Observe no change in position

  • Disabling interrupts with #define ENCODER_DO_NOT_USE_INTERRUPTS reports encoder inputs but with terribly shaky and unstable states :(

Hardware & Software

Board

Arduino IDE version : 1.8.16
Board: SEEEDuino XIAO
Operating system & version: Windows 10
Any other software or hardware? : Nope

Arduino Sketch

/* Encoder Library - Basic Example
 * http://www.pjrc.com/teensy/td_libs_Encoder.html
 *
 * This example code is in the public domain.
 */

#include <Encoder.h>

// Change these two numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability
Encoder myEnc(5, 6);
//   avoid using pins with LEDs attached

void setup() {
  Serial.begin(9600);
  Serial.println("Basic Encoder Test:");
}

long oldPosition  = -999;

void loop() {
  long newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
  }
}

Errors or Incorrect Output

No output from .read()

Using the encoder lib on an esp8266 via a MCP23017

Dear Paul,

unfortunately I don't have enough GPIO pins on my esp-07 so I need to use an MCP23017 with the https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library lib.
Do you see a chance to talk to the encoder via the mcp and do you have an idea how this can be done?

With the Adafruit_MCP23017 lib

include <Wire.h>

include <Adafruit_MCP23017.h>

I can declare the input pins on the MCP as
mcp.pinMode(x, INPUT);

but I can't do a nested
mcp.Encoder encmotor1(0, 1);
to tell that the interrupts are on the MCP.

Do you have a hint for me?

Thanks and regards, Ditmar

Basic example does NOT work reliably with KY040 mechanical switch rotary-encoder

I tested this library with the example "basic" with a KY040 rotary-encoder. This type of encoder has mechanical switches.
This is the result that I get when I turn the encoder
17:39:09.750 -> Basic Encoder Test:
17:39:09.750 -> 0
17:39:09.750 -> 1
17:39:09.750 -> 0
17:39:09.750 -> 1
17:39:09.750 -> 0
17:39:09.750 -> 1
17:39:09.793 -> 2
17:39:09.793 -> 3
17:39:09.793 -> 2
17:39:09.793 -> 3
17:39:09.793 -> 2
17:39:09.793 -> 3

I guess this is due to not handling properly any inbetween-states and bouncing of a mechanical-rotary-switch can have
I tried NewConder from GFV but get a compiler-error when compiling for Teensy 4.0
For other boards the NewEnocder from GVF works very good!
So Paul won't you improve your encoder-lib to the same level of high reliability?

input_pullup + interrupt fail on Arduino Due

Dear Paul,

It seems that something doesn't work when using encoders with open collector outputs.

Something disable the pinMode(pin, INPUT_PULLUP) command and put it back to HiZ state, thus compromising the reading of the encoder outputs.

Best,

Max

Encoder::update is private with optimized interrupts

Hello Paul,
First of all, thanks a lot for this great library.
I was using the library for some time on Arduino UNO and MEGA and it works well, but I have missed the ENCODER_OPTIMIZE_INTERRUPTS feature. I have then tried to enable it by adding the following define statement before including the library:

#define ENCODER_OPTIMIZE_INTERRUPTS

But I get an error while building it:

Encoder::update(Encoder_internal_state_t*)' is private

In fact, the update method is called in the interrupt routines so it has to be available publicly. Replacing the private statement l. 184 by public was sufficient to be able to compile and use the sketch.

It seems that this bug is present in the whole Github history and I am a bit surprised that nobody has reported it yet. So if I missed something, forgive me and close the issue. Or maybe most people, like me, do not read all the doc and do not use this feature.

Tested with Arduino 1.6.5, 1.6.7, library versions 1.3 and 1.4.

Sincerely,

Romain

Demo Code count 1,2,3 if your encoder reads four steps per tick

Please use this form only to report code defects or bugs.

## NO!
As long as GitHub is unable to provide

  • an easy to find and
  • step by step with a lot of screenshots tutorial how to use these complexe thing with forking and pull-requesting
    I simply use the issues-function to post useful demo-codes

So if you really need a bug-report: Here it is
"GitHubs explanations how GitHub works as a whole thing is spreaded all over a lot of links always just mentioning a part of the things assuming the reader has already lot's of knowledge about Github

I simply use the issues for this:

if your encoder reads four times for every mechanical tick of your encoder you can use this logic

#include <Encoder.h>  // von Paul Stoffregen

// If your encoder counts up by for for each step
// you can use this logic

const int CLK = 6;
const int DT = 5;

long altePosition = 0;
long neuePosition = 0;

long lastPos = 0;
long Zaehler = 0;
long alterZaehler = 0;

Encoder meinEncoder(DT, CLK);

void setup() {
  Serial.begin(115200);
} // SETUP


void loop() {
  neuePosition = meinEncoder.read();

  if (lastPos < neuePosition) {
    if (neuePosition % 4 == 0) {
      Zaehler++;
      lastPos = neuePosition;
    }
  }

  if (lastPos > neuePosition) {
    if (neuePosition % 4 == 0) {
      Zaehler--;
      lastPos = neuePosition;
    }
  }

  if (Zaehler != alterZaehler) {
    alterZaehler = Zaehler;
    Serial.println(Zaehler);
  } // IF

} // LOOP

best regards Stefan

attachInterrupt redefinition with optimized interrupts

Hello,
When using the ENCODER_OPTIMIZE_INTERRUPTS statement, the file utility/interrupt_config.h is used.
In this file, the following macro l.6 is used to redefine the attachInterrupt method:

#define attachInterrupt(num, func, mode) enableInterrupt(num)

The issue is that it interferes with other uses of this method name like with the TimerOne & TimerThree libraries. In these libraries, the attachInterrupt method only takes one argument but as the previous macro is not scoped, it is used as well in the Timer libraries. So the compiler throws that kind of errors:

Arduino/libraries/TimerThree/TimerThree.h:141:39: error: macro "attachInterrupt" requires 3 arguments, but only 1 given
     void attachInterrupt(void (*isr)()) __attribute__((always_inline)) {
                                       ^

A simple workaround would be to replace the attachInterrupt method in the Timer libraries by something like attachInterruptHandler or attachInterruptFunction but it will break the API and Arduino users are already used to that name.

But the issue is caused by the Encoder library so it would be better to fix that in this library.

Sincerely,

Romain

Not an issue - but...

I'm using your excellent library with an ESP32 - after doing so with Arduinos before. It seems, that you changed some files (ESP pins/interrupts) within the current version 1.4.1. These changes effect the users of ESP platform heavily, and many of them would not expect changes within a version they've already have. Please build a 1.4.2, so that ESP users get the latest changes - which are important.

I can confirm that the encoder runs like charm on an ESP32 with the latest changes.

Rasberry pi

Is there a way to make this work with rasberry pi?

Not working for a few pins on Arduino Due

#include<Encoder.h>

Encoder encoderFL1(25, 23);
Encoder encoderFL2(27, 29);

Encoder encoderFR1(31, 33);
Encoder encoderFR2(37, 35);

Encoder encoderHL1(41, 39);
Encoder encoderHL2(43, 45);

Encoder encoderHR1(47, 49);
Encoder encoderHR2(53, 51);

void setup()
{
// put your setup code here, to run once:
Serial.begin(115200);
}

void loop() {
// put your main code here, to run repeatedly:
Serial.print(encoderFL1.read());
Serial.print("\t");
Serial.print(encoderFL2.read());
Serial.print("\t");
Serial.print(encoderFR1.read());
Serial.print("\t");
Serial.print(encoderFR2.read());
Serial.print("\t");

Serial.print(encoderHL1.read());
Serial.print("\t");
Serial.print(encoderHL2.read());
Serial.print("\t");
Serial.print(encoderHR1.read());
Serial.print("\t");
Serial.print(encoderHR2.read());
Serial.println("\t");
delay(5);
}

/*
For encoderFL2 and encoderHR2 do not read
*/

Compilation warning on no interrupt example

Description

The no interrupt example throws out a bunch of errors on a new arduino 1.87 installation:
Encoder.h:69:7: Warning: type 'struct encoder' voilates one definition rule [-Wodr]
Encoder.h:69:7 - a different type is defined in another translation unit.
Encoder.h:132:10: the first difference ofr corresponding definitions is field 'interrupts_in_use'
etc...

Steps To Reproduce Problem

Installed new Arduino 1.8.7 on win10 machine.
Installed Encoder library
compiled No interrupt example

Counts by multiples of 2

It works pretty fine for me except that it returns pulses by pairs, so I can't stop the encoder on an impair value. That is for sure due to the cheap chinese encoders I use, otherwise I think somebody else would have report about that much before.

Anyway, this isn't a real issue, I simply divide the return number by 2 !

The Arduino librairie is still 1.41.

Thanks all for your work.

Cheers.

ESP8266 -Issue

Hi,

I am not able to make it work with ESP8266. But I know from an other guy that it's working with this library.

With other libs I have not problems

Contact Bounce

Is there a way to provide contact bounce checking one the encoder pins with this library??

thanks

Encoder with esp-07: Fatal exception 0 (Illegal Instruction Cause)

Dear Paul,

I use your routine and get random fatal exceptions. After spending unsuccessfull hours may I ask you for help?
I tried Arduino IDE 1.6.[7,8 and latest build] and all options in the menu for flash/cpu mode or frequency, different ESP-07, all with the same result: the function crashes randomly, although I use yield().
Any help would be greatly appreciated - I'm running out of ideas :-(

Thanks a lot and regards,
Ditmar

Error message (and my function used is cited below):

Fatal exception 0(IllegalInstructionCause):
epc1=0x402027c0, epc2=0x00000000, epc3=0x00000000, excvaddr=0x00000000, depc=0x00000000
Exception (0):
epc1=0x402027c0 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000
ctx: sys
sp: 3ffffc30 end: 3fffffb0 offset: 01a0

stack>>>
3ffffdd0: ........

void vorzurueck(int swtch, int mtr12pwm, long enc_steps) {
boolean anhalt = mcp.digitalRead(swtch);
if (!anhalt) {
encmotor1.write(0);
long tmp_enc = 0;
mcp.digitalWrite(mtr12pwm, 1);
while (!anhalt && tmp_enc < enc_steps) {
anhalt = mcp.digitalRead(swtch);
tmp_enc = encmotor1.read();
tmp_enc=abs(tmp_enc);
yield();
if (tmp_enc %1000==0) Serial.println(tmp_enc);
}
yield();
mcp.digitalWrite(mtr12pwm, 0);
}
}

Internal pull-up

How do I use the internal pull-up resistors for the encoder while using interrupt pins?

Crashing on ESP8266 with core 2.5.2

Description

Using the Basic Example from the Encoder Library, and ESP8266 core 2.5.2, the esp8266 crashes/resets. However, the NoInterrupt example runs ok.

Steps To Reproduce Problem

Just use the Basic example included with the Encoder lib, and an ESP8266 with core version 2.5.2. I think same issue with 2.5.1, but ok with 2.5.0.

Hardware & Software

Board : NodeMCU 1.0 (ESP-12E Module)
Board package : , version 2.5.2
Shields / modules used : none
Arduino IDE version : 1.8.9
Version info & package name (from Tools > Boards > Board Manager) : 1.4.1
Operating system & version : Win10

Arduino Sketch

/* Encoder Library - Basic Example
 * http://www.pjrc.com/teensy/td_libs_Encoder.html
 *
 * This example code is in the public domain.
 */

#include <Encoder.h>

// Change these two numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability
Encoder myEnc(5, 4);
//   avoid using pins with LEDs attached

void setup() {
  Serial.begin(115200);
  Serial.println("Basic Encoder Test:");
}

long oldPosition  = -999;

void loop() {
  long newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
  }
}

Errors or Incorrect Output

ets Jan 8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
v8b899c12
~ld

seems like you've overoptimized update() function

Hi. In no-interrupts case, when you invoke .read() inside the ISR of pin change interrupt, something bad happens. ISR is only fired once and then, no new calls happen. Funny thing is that adding some other function right below the read() call will make it work normally. I guess it's related to the ASM code in update().

#define ENCODER_DO_NOT_USE_INTERRUPTS
#include <Encoder.h>

Encoder myEnc(A2, A3);

void setup() {
  Serial.begin(115200);
  while (!Serial); Serial.println("Started.");
  PCMSK1 |= bit(PCINT10) | bit(PCINT11) | bit(PCINT12);
  PCIFR  |= bit (PCIF1);   // clear any outstanding interrupts
  PCICR |= bit(PCIE1);
}

volatile int pos  = 0;
int oldpos = 0;

void loop() {
  if (oldpos != pos) {
    oldpos = pos;
    Serial.println(pos);
  }
  delay(100);
}

// PCINT10, PCINT11
ISR(PCINT1_vect) {
  pos = myEnc.read();
  //  adding digitalRead(A0)
  // or even Serial.println(pos) updates the var correctly
}

No support for Arduino nano rp2040 connect

Please use this form only to report code defects or bugs.

For any question, even questions directly pertaining to this code, post your question on the forums related to the board you are using.

Arduino: forum.arduino.cc
Teensy: forum.pjrc.com
ESP8266: www.esp8266.com
ESP32: www.esp32.com
Adafruit Feather/Metro/Trinket: forums.adafruit.com
Particle Photon: community.particle.io

If you are experiencing trouble but not certain of the cause, or need help using this code, ask on the appropriate forum. This is not the place to ask for support or help, even directly related to this code. Only use this form you are certain you have discovered a defect in this code!

Please verify the problem occurs when using the very latest version, using the newest version of Arduino and any other related software.

----------------------------- Remove above -----------------------------

Description

Describe your problem.
No support for Arduino nano rp2040 connect

Steps To Reproduce Problem

Please give detailed instructions needed for anyone to attempt to reproduce the problem.

try to compile the basic example will fail, it will says the board does not have interrupt pins try to use no interrupt example also fails.

Hardware & Software

Board Arduino nano rp2040 connect
Shields / modules used no
Arduino IDE version 1.8.19
Teensyduino version (if using Teensy)
Version info & package name (from Tools > Boards > Board Manager) 3.0.0 for Mbed os nano
Operating system & version windows 11 22581.200
Any other software or hardware?

Arduino Sketch

// Change the code below by your sketch (please try to give the smallest code which demonstrates the problem)
#include <Arduino.h>

// libraries: give links/details so anyone can compile your code for the same result

void setup() {
}

void loop() {
}

Errors or Incorrect Output

If you see any errors or incorrect output, please show it here. Please use copy & paste to give an exact copy of the message. Details matter, so please show (not merely describe) the actual message or error exactly as it appears.

Where can i find the processor defines, to add support for new boards?

This library is hands down the best encoder library.

I'm trying to add support for the raspberry pi pico and for the robotis OpenCR, both of which i seem to use every day of late. Try as i might though, i can't find where the processor is defined, to add it in to your library. E.g for an arduino Mega it woud be -

#elif defined(AVR_ATmega1280) || defined(AVR_ATmega2560)

Cheers.

Ben

ISR vectors multiple defined

Hello,
I just tried to use your Encoder library in Platformio (platform=atmelavr).
First, everything compiled well, but then I switched on ENCODER_OPTIMIZE_INTERRUPTS.

After that I got multiple definitions of __vector_1 and __vector_2 when linking all together.

I'm using Encoder.h in multiple CPP files, so the ISR(INTx_vect) are multiply compiled and the linker throws an error.

My quick solution was to move the last part of Encoder.h (that part which is outside the class definition) to Encoder.cpp. So this part is only compiled once and the linker succeeds.

Maybe there is another, better solution?

Kind regards,
Martin

Raspberry Pico (RP2040) support?

Description

I tried running this with a Raspberry Pico using the "Arduino Mbed OS RP2040 (pico)" board setting, and got this error:

Arduino: 1.8.19 (Windows 10), Board: "Raspberry Pi Pico"
In file included from C:\Users\Pomax\Documents\Arduino\libraries\Encoder/Encoder.h:46:0,
                 from C:\Users\Pomax\Downloads\pico arduino\test\test.ino:1:
C:\Users\Pomax\Documents\Arduino\libraries\Encoder/utility/interrupt_pins.h:334: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"
  ^~~~~
C:\Users\Pomax\Documents\Arduino\libraries\Encoder/utility/interrupt_pins.h:337: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 :("
  ^~~~~
C:\Users\Pomax\Documents\Arduino\libraries\Encoder/utility/interrupt_pins.h:338: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."
  ^~~~~
exit status 1
Error compiling for board Raspberry Pi Pico.

Every single pin on the Pico can be set as interrupt pin (See https://raspberrypi.github.io/pico-sdk-doxygen/group__hardware__irq.html, "Interrupt Numbers" section), so it's basically the ideal board for interrupt work, and having this library support it would be super useful.

Steps To Reproduce Problem

Hook up a Raspberry Pico, pick the above-mentioned board, have a sketch that purely consists of #include <Encoder.h> and hit compile.

Hardware & Software

Board: Raspberry Pico
Shields / modules used: none
Arduino IDE version: 1.8.19
Teensyduino version (if using Teensy)
Version info & package name (from Tools > Boards > Board Manager): BN: Raspberry Pi Pico, VID: 2e8a, PID: 00C0, SN:
Operating system & version: win10 pro x64
Any other software or hardware?

Arduino Sketch

#include <Encoder.h>

void setup() {
 // doesn't even matter
}

void loop() {
 // idem ditto
}

Errors or Incorrect Output

See above

STM32H743ZI2 144 Nucleo compatibility issues

Hi, I am attempting to use the stm32h743zi2 and I am getting errors with the interrupts define and the do not use interrupts define in the code below I am trying to add the pins PF1 and PF0 to the code the defining value for this board is: ARDUINO_NUCLEO_H743ZI2
I am using the stm32duino libary for board manager

Code can be found here

Error trying to build SpeedTest.ino on Teensy 4.1 in Arduino 1.8.19: error: cannot convert 'volatile uint32_t* {aka volatile long unsigned int*}' to 'volatile unsigned char*' in initialization

Description

When I try to build the speedTest.ino example I get the following error.

error: cannot convert 'volatile uint32_t* {aka volatile long unsigned int*}' to 'volatile unsigned char*' in initialization
#define portOutputRegister(pin) ((digital_pin_to_info_PGM[(pin)].reg + 0))**

More detail below.

Steps To Reproduce Problem

Install Arduino 1.8.19 and Teensyduino Version 1.56, on Windows 10 pro and attempt to build the SpeedTest.ino example under Teensy4.1>Encoder

Hardware & Software

Board
Teensy 4.1

Arduino IDE version
1.8.19

Teensyduino version (if using Teensy)
Version 1.56 for windows 7 and up (downloaded from teensyduino website today 3/22/2022)

Version info & package name (from Tools > Boards > Board Manager)
Teensy 4.1

Operating system & version
Windows 10 pro

Any other software or hardware?

Arduino Sketch

Using the included encoder>speedtest.ino

/* Encoder Library - SpeedTest - for measuring maximum Encoder speed
 * http://www.pjrc.com/teensy/td_libs_Encoder.html
 *
 * This example code is in the public domain.
 */


// This SpeedTest example provides a simple way to verify how much
// CPU time Encoder is consuming.  Connect a DC voltmeter to the
// output pin and measure the voltage while the encoder is stopped
// or running at a very slow speed.  Even though the pin is rapidly
// pulsing, a DC voltmeter will show the average voltage.  Due to
// software timing, it will read a number much less than a steady
// logic high, but this number will give you a baseline reading
// for output with minimal interrupt overhead.  Then increase the
// encoder speed.  The voltage will decrease as the processor spends
// more time in Encoder's interrupt routines counting the pulses
// and less time pulsing the output pin.  When the voltage is
// close to zero and will not decrease any farther, you have reached
// the absolute speed limit.  Or, if using a mechanical system where
// you reach a speed limit imposed by your motors or other hardware,
// the amount this voltage has decreased, compared to the baseline,
// should give you a good approximation of the portion of available
// CPU time Encoder is consuming at your maximum speed.

// Encoder requires low latency interrupt response.  Available CPU
// time does NOT necessarily prove or guarantee correct performance.
// If another library, like NewSoftSerial, is disabling interrupts
// for lengthy periods of time, Encoder can be prevented from
// properly counting the intput signals while interrupt are disabled.


// This optional setting causes Encoder to use more optimized code,
// but the downside is a conflict if any other part of your sketch
// or any other library you're using requires attachInterrupt().
// It must be defined before Encoder.h is included.
//#define ENCODER_OPTIMIZE_INTERRUPTS

#include <Encoder.h>
#include "pins_arduino.h"

// Change these two numbers to the pins connected to your encoder
// or shift register circuit which emulates a quadrature encoder
//  case 1: both pins are interrupts
//  case 2: only first pin used as interrupt
Encoder myEnc(5, 6);

// Connect a DC voltmeter to this pin.
const int outputPin = 12;

/* This simple circuit, using a Dual Flip-Flop chip, can emulate
   quadrature encoder signals.  The clock can come from a fancy
   function generator or a cheap 555 timer chip.  The clock
   frequency can be measured with another board running FreqCount
   http://www.pjrc.com/teensy/td_libs_FreqCount.html

                        +5V
                         |        Quadrature Encoder Signal Emulator
 Clock                   |
 Input o----*--------------------------      ---------------------------o Output1
            |            |14           |    |
            |     _______|_______      |    |     _______________ 
            |    |    CD4013     |     |    |    |    CD4013     |
            |  5 |               | 1   |    |  9 |               | 13
        ---------|  D         Q  |-----|----*----|  D         Q  |------o Output2
       |    |    |               |     |         |               |
       |    |  3 |               |     |      11 |               |
       |     ----|> Clk          |      ---------|> Clk          |
       |         |               |               |               |
       |       6 |               |             8 |               |
       |     ----|  S            |           ----|  S            |
       |    |    |               |          |    |               |
       |    |  4 |            _  | 2        | 10 |            _  | 12
       |    *----|  R         Q  |---       *----|  R         Q  |----
       |    |    |               |          |    |               |    |
       |    |    |_______________|          |    |_______________|    |
       |    |            |                  |                         |
       |    |            | 7                |                         |
       |    |            |                  |                         |
        --------------------------------------------------------------
            |            |                  |
            |            |                  |
          -----        -----              -----
           ---          ---                ---
            -            -                  -
*/


void setup() {
  pinMode(outputPin, OUTPUT);
}

#if defined(__AVR__) || defined(TEENSYDUINO)
#define REGTYPE unsigned char
#else
#define REGTYPE unsigned long
#endif

void loop() {
  volatile int count = 0;
  volatile REGTYPE *reg = portOutputRegister(digitalPinToPort(outputPin));
  REGTYPE mask = digitalPinToBitMask(outputPin);

  while (1) {
    myEnc.read();	// Read the encoder while interrupts are enabled.
    noInterrupts();
    *reg |= mask;	// Pulse the pin high, while interrupts are disabled.
    count = count + 1;
    *reg &= ~mask;
    interrupts();
  }
}

Errors or Incorrect Output

Arduino: 1.8.19 Hourly Build 2019/02/04 10:33 (Windows 10), TD: 1.56, Board: "Teensy 4.1, Serial, 600 MHz, Faster, US English"

In file included from C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy4/core_pins.h:33:0,

                 from C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy4/wiring.h:39,

                 from C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy4/WProgram.h:45,

                 from C:\Users\name\AppData\Local\Temp\arduino_build_426018\pch\Arduino.h:6:

SpeedTest: In function 'void loop()':

C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy4/pins_arduino.h:149:75: error: cannot convert 'volatile uint32_t* {aka volatile long unsigned int*}' to 'volatile unsigned char*' in initialization

 #define portOutputRegister(pin)  ((digital_pin_to_info_PGM[(pin)].reg + 0))

                                                                           ^

C:\Users\name~1\AppData\Local\Temp\arduino_modified_sketch_275677\SpeedTest.ino:101:27: note: in expansion of macro 'portOutputRegister'

   volatile REGTYPE *reg = portOutputRegister(digitalPinToPort(outputPin));

                           ^

C:\Users\name~1\AppData\Local\Temp\arduino_modified_sketch_275677\SpeedTest.pde: At global scope:

SpeedTest:46: error: redefinition of 'Encoder myEnc'

 Encoder myEnc(5, 6);

              ^

C:\Users\name~1\AppData\Local\Temp\arduino_modified_sketch_275677\SpeedTest.ino:46:9: note: 'Encoder myEnc' previously declared here

 Encoder myEnc(5, 6);

         ^

SpeedTest:49: error: redefinition of 'const int outputPin'

 const int outputPin = 12;

           ^

C:\Users\name~1\AppData\Local\Temp\arduino_modified_sketch_275677\SpeedTest.ino:49:11: note: 'const int outputPin' previously defined here

 const int outputPin = 12;

           ^

SpeedTest: In function 'void setup()':

SpeedTest:89: error: redefinition of 'void setup()'

 void setup() {

      ^

C:\Users\name~1\AppData\Local\Temp\arduino_modified_sketch_275677\SpeedTest.ino:89:6: note: 'void setup()' previously defined here

 void setup() {

      ^

SpeedTest: In function 'void loop()':

SpeedTest:99: error: redefinition of 'void loop()'

 void loop() {

      ^

C:\Users\name~1\AppData\Local\Temp\arduino_modified_sketch_275677\SpeedTest.ino:99:6: note: 'void loop()' previously defined here

 void loop() {

      ^

In file included from C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy4/core_pins.h:33:0,

                 from C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy4/wiring.h:39,

                 from C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy4/WProgram.h:45,

                 from C:\Users\name~1\AppData\Local\Temp\arduino_build_426018\pch\Arduino.h:6:

C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy4/pins_arduino.h:149:75: error: cannot convert 'volatile uint32_t* {aka volatile long unsigned int*}' to 'volatile unsigned char*' in initialization

 #define portOutputRegister(pin)  ((digital_pin_to_info_PGM[(pin)].reg + 0))

                                                                           ^

C:\Users\name~1\AppData\Local\Temp\arduino_modified_sketch_275677\SpeedTest.pde:101:27: note: in expansion of macro 'portOutputRegister'

   volatile REGTYPE *reg = portOutputRegister(digitalPinToPort(outputPin));

                           ^

redefinition of 'Encoder myEnc'



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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.