Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,17 @@ class Encoder
encoder.position = p;
}
#endif
inline int32_t getDirection() {
int32_t prevPos = encoder.position;
long currPos = read();
int32_t direction = 0;
if (currPos > prevPos) {
direction = 1;
} else if (currPos < prevPos) {
direction = -1;
}
return direction;
}
private:
Encoder_internal_state_t encoder;
#ifdef ENCODER_USE_INTERRUPTS
Expand Down
29 changes: 29 additions & 0 deletions examples/Direction/Direction.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* 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("Encoder Direction Test:");
}

void loop() {
int direction = myEnc.getDirection();

if (direction > 0) {
Serial.println("Forward");
} else if (direction < 0) {
Serial.println("Backward");
}
}