Encoder lib, change input pins after inversion of the direction

Hi,

I'm using this lib with an DC motor's encoder: GitHub - PaulStoffregen/Encoder: Quadrature Encoder Library for Arduino

When turning the motor left it counts in the positive range when rotating it right it counts downwards(negative range)

I thought changing the init of the Encoder

Encoder myEnc(2,3);

according to the direction to get positiv values only
e.g:

if(turnRight) { Encoder myEnc(2,3); }
else  if(turnLeft) { Encoder myEnc(3,2); }

but this doesnt work
Do you know another option to get only positiv and upwards values?

Why would you want to do this? If you only count up regardless of turning left or right, how can you know how far you are moving?

You would need to do this with compile time directives

#define TURN_RIGHT
#if defined TURN_RIGHT
Encoder myEnc(2,3);
#else
Encoder myEnc(3,2);
#endif

The standard quadrature encoder is specifically designed for position control, where you need to be
able to get back to the same position repeatably, so must use a signed counter.

If you just want speed control use only one channel of the encoder and count its pulses - ie attach
an interrupt for just one of the pins and increment the counter.

Using only one channel is too unprecise, the encoder also has an index pin for that pupose but using both channel works much better.

If you only count up regardless of turning left or right, how can you know how far you are moving?

I turn the motor till eg 100000 counts are reached after that I reset the encoder's position to 0.
So that's how I know how far the motor moved

If there is no other solution I would have done it as suggested in my first post.

#define TURN_RIGHT
#if defined TURN_RIGHT

That doesn't work as I can't change the direction within the program but only when compiling

/* Encoder Library - Basic Example
   http://www.pjrc.com/teensy/td_libs_Encoder.html

   This example code is in the public domain.
*/

// Variable 'counts' increments in either direction.

#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(A0, A1);
//   avoid using pins with LEDs attached

unsigned counts = -1;

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

long oldPosition  = -999;

void loop() {
  long newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    counts++;
    Serial.print(counts);
    Serial.print("\t");
    Serial.println(newPosition);
  }
  if (counts > 99) counts = 0;
}

Mind you, the code counts every transition, so four counts for each detent on my test manual encoder.

timtailors:
Using only one channel is too unprecise, the encoder also has an index pin for that pupose but using both channel works much better.

If you only count up regardless of turning left or right, how can you know how far you are moving?

I turn the motor till eg 100000 counts are reached after that I reset the encoder’s position to 0.
So that’s how I know how far the motor moved

If there is no other solution I would have done it as suggested in my first post.That doesn’t work as I can’t change the direction within the program but only when compiling

When you reset the position to 0, set a direction variable (1 or -1) and then just multiply this direction variable * the encoder position.

/* 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;

int turnDirection = 1;

void loop() {
  long newPosition = myEnc.read() * turnDirection;
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
  }
  if (newPosition = 1000 ) {
    turnDirection = -turnDirection; // switch direction
    myEnc.write(0); // reset position
  }
}