Using the Encoder library with pointers

Status
Not open for further replies.

Fuzzy

Active member
First off, thanks for this awesome encoder library. Writing encoder-code from scratch is a total pain. I tried once, and failed terribly.

I having some unexpected behavior when using the Encoder as a pointer.

I am using Teensy2++ with Arduino0022

Everything works fine if both pins are interrupt enabled:

Code:
#include <Encoder.h>
Encoder *encoderArray[1];
void setup() {
  Serial.begin(9600);
  encoderArray[0] = new Encoder(0,1); //use pins 0 and 1, both interrupt
  Serial.println("Basic Encoder Test:");
}
long oldPosition  = -999;
void loop() {
  long newPosition = encoderArray[0]->read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
  }
}

Output (as expected):
Basic Encoder Test:
0
1
0
1
2
3
4
5
6
7
8
9
10


However, the output goes goofy when one of the pins is not interrupt enabled:

Code:
#include <Encoder.h>
Encoder *encoderArray[1];
void setup() {
  Serial.begin(9600);
  encoderArray[0] = new Encoder(1,5); //use pins 1 and 5, only pin 1 is interrupt enabled
  Serial.println("Basic Encoder Test:");
}
long oldPosition  = -999;
void loop() {
  long newPosition = encoderArray[0]->read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
  }
}

Output (goofy):
Basic Encoder Test:
-672138537
2143535054
-672138537
2143535054
-672138537
2143535054
-672138537
2143535054
-672138537
 
More weirdness...

I switched from a pointer to an array. Now even stranger stuff is happening...

When only the first pin is interrupt enabled, everything works as expected

Code:
#include <Encoder.h>
//ONLY FIRST PIN IS INT ENABLED
Encoder e0(18,35);
Encoder e1(19,34);
Encoder myEncoders[] = {e0,e1};
long pos[] = {-999,-999};

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

void loop() {
  long newLeft, newRight;
  newLeft = myEncoders[0].read();
  newRight = myEncoders[1].read();
  if (newLeft != pos[0] || newRight != pos[1]) {
    Serial.print("Left = ");
    Serial.print(newLeft);
    Serial.print(", Right = ");
    Serial.print(newRight);
    Serial.println();
    pos[0] = newLeft;
    pos[1] = newRight;
  }
  // if a character is sent from the serial monitor,
  // reset both back to zero.
  if (Serial.available()) {
    Serial.read();
    Serial.println("Reset both knobs to zero");
    myEncoders[0].write(0);
    myEncoders[1].write(0);
  }
}

OUTPUT:
Left = -52, Right = 8
Left = -52, Right = 9
Left = -52, Right = 10
Left = -52, Right = 11
Left = -52, Right = 12
Left = -52, Right = 13
Left = -52, Right = 14
Left = -52, Right = 15
Left = -52, Right = 16
Left = -52, Right = 17
Left = -52, Right = 18
Left = -52, Right = 19
Left = -52, Right = 20
Left = -52, Right = 21
Left = -52, Right = 22
Left = -53, Right = 22
Left = -54, Right = 22
Left = -55, Right = 22
Left = -56, Right = 22
Left = -57, Right = 22
Left = -58, Right = 22
Left = -59, Right = 22
Left = -58, Right = 22
Left = -59, Right = 22
Left = -60, Right = 22
Left = -61, Right = 22
Left = -62, Right = 22
Left = -63, Right = 22
Left = -64, Right = 22
Left = -65, Right = 22
Left = -66, Right = 22
Left = -67, Right = 22
Left = -66, Right = 22



But when both pins are interrupt enabled, there is NO OUTPUT, ie,
'if (newLeft != positionLeft || newRight != positionRight)' is never true

Code:
#include <Encoder.h>
//ONLY FIRST PIN IS INT ENABLED
Encoder e0(0,1);
Encoder e1(36,37);
Encoder myEncoders[] = {e0,e1};
long pos[] = {-999,-999};

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

void loop() {
  long newLeft, newRight;
  newLeft = myEncoders[0].read();
  newRight = myEncoders[1].read();
  if (newLeft != pos[0] || newRight != pos[1]) {
    Serial.print("Left = ");
    Serial.print(newLeft);
    Serial.print(", Right = ");
    Serial.print(newRight);
    Serial.println();
    pos[0] = newLeft;
    pos[1] = newRight;
  }
  // if a character is sent from the serial monitor,
  // reset both back to zero.
  if (Serial.available()) {
    Serial.read();
    Serial.println("Reset both knobs to zero");
    myEncoders[0].write(0);
    myEncoders[1].write(0);
  }
}


If I use Pauls regular (dual encoder) code with the same encoders, everything works perfectly again.

Code:
/* 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 knobLeft(0,1);
Encoder knobRight(36,37);
//   avoid using pins with LEDs attached

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

long positionLeft  = -999;
long positionRight = -999;

void loop() {
  long newLeft, newRight;
  newLeft = knobLeft.read();
  newRight = knobRight.read();
  if (newLeft != positionLeft || newRight != positionRight) {
    Serial.print("Left = ");
    Serial.print(newLeft);
    Serial.print(", Right = ");
    Serial.print(newRight);
    Serial.println();
    positionLeft = newLeft;
    positionRight = newRight;
  }
  // if a character is sent from the serial monitor,
  // reset both back to zero.
  if (Serial.available()) {
    Serial.read();
    Serial.println("Reset both knobs to zero");
    knobLeft.write(0);
    knobRight.write(0);
  }
}

...What is going on here?
 
A very good question....

This is going to take me some time to investigate. I will look into this, but right now I'm occupied prepping for releasing Teensyduino 1.14 when Arduino 1.0.5 releases (any day now).
 
It's been a while. Really hoping this can get resolved soon. Need this for TeensyMonster, which is launching very soon.
 
Status
Not open for further replies.
Back
Top