Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 7 of 7

Thread: Defining Encoder() pins at runtime

  1. #1
    Junior Member
    Join Date
    Feb 2016
    Posts
    14

    Defining Encoder() pins at runtime

    Using quadrature Encoder library, v1.3, Teensy3.2.
    I thought I would be clever with a macro definition to get around the requirement of
    defining the encoder A/B pins at compile time. The problem is that a supplier
    swapped the encoder lines on a motor I'm using, so I added a solder pad that when
    shorted, would inform the firmware whether to reverse the pins.
    It looks like the macro extension is not supported:

    #define REVERSE_PIN 9 // if pin 9 is grounded, then reverse the encoder pin A/B
    #define ENCODER_A_PIN ({ \
    int i = 0;\
    pinMode(REVERSE_PIN, INPUT_PULLUP);\
    delay(10);\
    if (digitalRead(REVERSE_PIN)==LOW) { i=1;}\
    i;\
    })

    I can modify the library, and will do so. I am wondering if there is a better way to do this.

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,006
    Maybe read the pin once in setup() and set a variable to either 1 or -1. Then when you read the encoder, multiply by that number.

  3. #3
    Junior Member
    Join Date
    Feb 2016
    Posts
    14
    Doh! It's so easy. Okay, this does solve my particular problem. Thanks.
    For argument's sake, what would your solution be if the encoder wires were on completely different pins?

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,006
    Find a better supplier?

  5. #5
    Junior Member
    Join Date
    Feb 2016
    Posts
    14
    True that.

  6. #6
    Senior Member
    Join Date
    Feb 2017
    Posts
    214
    How about instantiating the Encoder dynamically? This is a modification of the "Basic Example" from the Encoder library. It compiles but is untested:
    Code:
    #include "Arduino.h"
    #include <Encoder.h>
    
    Encoder *encoderPtr;
    const uint8_t polarityPin = 9;
    
    void setup() {
    	Serial.begin(115200);
    	Serial.println("Basic Encoder Test:");
    	pinMode(polarityPin, INPUT_PULLUP);
    	if (digitalRead(polarityPin)) {
    		encoderPtr = new Encoder(2, 3);
    	} else {
    		encoderPtr = new Encoder(3, 2);
    	}
    }
    
    long oldPosition = -999;
    
    void loop() {
    	long newPosition = encoderPtr->read();
    	if (newPosition != oldPosition) {
    		oldPosition = newPosition;
    		Serial.println(newPosition);
    	}
    }

  7. #7
    Junior Member
    Join Date
    Feb 2016
    Posts
    14
    Thanks. I keep forgetting about this. Too many languages at one time.

    Quote Originally Posted by gfvalvo View Post
    How about instantiating the Encoder dynamically? This is a modification of the "Basic Example" from the Encoder library. It compiles but is untested:
    Code:
    #include "Arduino.h"
    #include <Encoder.h>
    
    Encoder *encoderPtr;
    const uint8_t polarityPin = 9;
    
    void setup() {
    	Serial.begin(115200);
    	Serial.println("Basic Encoder Test:");
    	pinMode(polarityPin, INPUT_PULLUP);
    	if (digitalRead(polarityPin)) {
    		encoderPtr = new Encoder(2, 3);
    	} else {
    		encoderPtr = new Encoder(3, 2);
    	}
    }
    
    long oldPosition = -999;
    
    void loop() {
    	long newPosition = encoderPtr->read();
    	if (newPosition != oldPosition) {
    		oldPosition = newPosition;
    		Serial.println(newPosition);
    	}
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •