Arduino Due and two rotary quad encoders

For the discussion of Arduino related topics.

Moderator: phalanx

Post Reply
albnar
Posts: 1
Joined: Mon Nov 12, 2018 3:07 pm

Arduino Due and two rotary quad encoders

Post by albnar » Mon Nov 12, 2018 3:32 pm

HI,
I am new in this Forum and in development with Arduino too.
I was able to read one of both using code I found in https://www.pjrc.com/teensy/td_libs_Encoder.html and Arduino UNO.
Now I need to read two quad rotary encoders at the same time. Arduino UNO has only two external interrupts pins.
I decided to use Arduino DUE.
I modified the code to use two encoders.
It seems that DUE does not counts the pulses coming from encoders. It detects them only. Reading have always the same two values, it does not count them.
Could someone help me?

Here is the code (sorry I do not know a better way to paste it)

/* Encoder Library - TwoKnobs Example
* http://www.pjrc.com/teensy/td_libs_Encoder.html
*
* This example code is in the public domain.
*/
#include <Encoder.h>

// Change these pin 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 ribbon1(3,2);
Encoder ribbon2(4,5);

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

}

long positionRibbon1 = -999;
long positionRibbon2 = -999;

void loop() {
double newLeft1, newLeft2;
float dist1, dist2;
newLeft1 = ribbon1.read();
newLeft2 = ribbon2.read();
if (newLeft1 != positionRibbon1 or newLeft2 != positionRibbon2) {
Serial.print("E");
dist1 = newLeft1/2000.;
dist2 = newLeft2/2000.; //2000 counts = 1mm.
Serial.print(dist1,3);
Serial.print(",");
Serial.print(dist2,3);
Serial.println();
positionRibbon1 = newLeft1;
positionRibbon2 = newLeft2;

}
// if a character is sent from the serial monitor,
// reset both back to zero.

if (Serial.available()) {
Serial.read();
Serial.println("Reset Ribbon to zero");
ribbon1.write(0);
ribbon2.write(0);
}
}

paulvha
Posts: 297
Joined: Sat Nov 11, 2017 2:39 am

Re: Arduino Due and two rotary quad encoders

Post by paulvha » Tue Nov 13, 2018 6:09 am

I suspect it could be related to interrupts. Only the last call for encoder will be set with interrupts : Encoder ribbon2(4,5);
Look at the example Nointerrupts and #define ENCODER_DO_NOT_USE_INTERRUPTS before "include "encoder.h". It has a trade off so read the header of the example.

One aspect that I find interesting all the driver code in the the encoder.h file instead of encoder.cpp (where I would expect it)
Paul

User avatar
DanV
Posts: 393
Joined: Sat Apr 25, 2015 9:29 am

Re: Arduino Due and two rotary quad encoders

Post by DanV » Tue Nov 13, 2018 9:59 am

Aren't you constantly setting the encoder position to zero ?
ribbon1.write(0);
ribbon2.write(0);

Post Reply