Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added setPositionLimits to handle limited rotators #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
10 changes: 1 addition & 9 deletions examples/LimitedRotator/LimitedRotator.ino
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ void setup()
while (! Serial);
Serial.println("LimitedRotator example for the RotaryEncoder library.");
encoder.setPosition(10 / ROTARYSTEPS); // start with the value of 10.
encoder.setPositionLimits( ROTARYMIN / ROTARYSTEPS, ROTARYMAX / ROTARYSTEPS );
} // setup()


Expand All @@ -66,15 +67,6 @@ void loop()
// get the current physical position and calc the logical position
int newPos = encoder.getPosition() * ROTARYSTEPS;

if (newPos < ROTARYMIN) {
encoder.setPosition(ROTARYMIN / ROTARYSTEPS);
newPos = ROTARYMIN;

} else if (newPos > ROTARYMAX) {
encoder.setPosition(ROTARYMAX / ROTARYSTEPS);
newPos = ROTARYMAX;
} // if

if (lastPos != newPos) {
Serial.print(newPos);
Serial.println();
Expand Down
32 changes: 31 additions & 1 deletion src/RotaryEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "RotaryEncoder.h"
#include "Arduino.h"
#include <limits.h>

#define LATCH0 0 // input state at position 0
#define LATCH3 3 // input state at position 3
Expand Down Expand Up @@ -58,6 +59,10 @@ RotaryEncoder::RotaryEncoder(int pin1, int pin2, LatchMode mode)
_position = 0;
_positionExt = 0;
_positionExtPrev = 0;

// set absolute min max limits of our storage type
_positionMin = LONG_MIN;
_positionMax = LONG_MAX;
} // RotaryEncoder()


Expand Down Expand Up @@ -146,7 +151,15 @@ void RotaryEncoder::tick(void)
}
break;
} // switch

// apply limits
if (_positionExt < _positionMin) {
setPosition(_positionMin);
} else if (_positionExt > _positionMax) {
setPosition(_positionMax);
} // if
} // if

} // tick()


Expand All @@ -164,5 +177,22 @@ unsigned long RotaryEncoder::getRPM()
return 60000.0 / ((float)(t * 20));
}

void RotaryEncoder::setPositionLimits(long min, long max)
{
if (max < min) {
long temp = min;
min = max;
max = temp;
}

_positionMin = min;
_positionMax = max;

if (_positionExt < _positionMin) {
setPosition(_positionMin);
} else if (_positionExt > _positionMax) {
setPosition(_positionMax);
} // if
} // setPositionLimits()

// End
// End
10 changes: 8 additions & 2 deletions src/RotaryEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,12 @@ class RotaryEncoder
// Returns the RPM
unsigned long getRPM();

// Set min and max value of position
void setPositionLimits(long min, long max);

private:
int _pin1, _pin2; // Arduino pins used for the encoder.

LatchMode _mode; // Latch mode from initialization

volatile int8_t _oldState;
Expand All @@ -69,8 +72,11 @@ class RotaryEncoder

unsigned long _positionExtTime; // The time the last position change was detected.
unsigned long _positionExtTimePrev; // The time the previous position change was detected.

long _positionMin;
long _positionMax;
};

#endif

// End
// End