diff --git a/Readme.md b/Readme.md index c87e419..1f21bf3 100644 --- a/Readme.md +++ b/Readme.md @@ -120,6 +120,8 @@ Enjoy! ## Change Log +* 1.2.1 - Bug fixes and streamlined some code. + * 1.2.0 - Added steps and modified the code for efficiency. Added right and left methods to be used instead of clockwise and counterClockwise respectively. Changed all variables related to tracking a value to float datatypes to facilitate fractional number tracking. \ No newline at end of file diff --git a/library.properties b/library.properties index 462d51b..29b62f2 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=SimpleEncoder -version=1.2.0 +version=1.2.1 author=Michael Sims maintainer=Michael Sims sentence=SimpleEncoder is a library that makes it easy to use rotary encoders with Arduino's. diff --git a/src/SimpleEncoder.cpp b/src/SimpleEncoder.cpp index 2c1f355..891bf3f 100644 --- a/src/SimpleEncoder.cpp +++ b/src/SimpleEncoder.cpp @@ -5,116 +5,95 @@ #include SimpleEncoder::SimpleEncoder(int pinButton, int pinA, int pinB) { - encA = pinA; - encB = pinB; - buttonPin = pinButton; - pinMode(encA, INPUT); - pinMode(encB, INPUT); - pinMode(buttonPin, INPUT_PULLUP); + initPins(pinButton, pinA, pinB); } SimpleEncoder::SimpleEncoder(int pinButton, int pinA, int pinB, float trackingValue) { - encA = pinA; - encB = pinB; - buttonPin = pinButton; + initPins(pinButton, pinA, pinB); value = trackingValue; - pinMode(encA, INPUT); - pinMode(encB, INPUT); - pinMode(buttonPin, INPUT_PULLUP); + valueSteps = 1; + trackValue = true; + checkLimits = false; } SimpleEncoder::SimpleEncoder(int pinButton, int pinA, int pinB, float trackingValue, float lowerLimit, float upperLimit) { - encA = pinA; - encB = pinB; - buttonPin = pinButton; - pinMode(encA, INPUT); - pinMode(encB, INPUT); - pinMode(buttonPin, INPUT_PULLUP); + initPins(pinButton, pinA, pinB); value = trackingValue; lower = lowerLimit; upper = upperLimit; valueSteps = 1; - trackValue = valueSteps != 0; - checkLimits = (lower != upper) && (lower != 0.0 && upper != 0); + trackValue = true; + checkLimits = true; } SimpleEncoder::SimpleEncoder(int pinButton, int pinA, int pinB, float trackingValue, float lowerLimit, float upperLimit, float steps) { - encA = pinA; - encB = pinB; - buttonPin = pinButton; - pinMode(encA, INPUT); - pinMode(encB, INPUT); - pinMode(buttonPin, INPUT_PULLUP); + initPins(pinButton, pinA, pinB); value = trackingValue; lower = lowerLimit; upper = upperLimit; valueSteps = steps; - trackValue = valueSteps != 0; - checkLimits = (lower != upper) && (lower != 0.0 && upper != 0); + trackValue = true; + checkLimits = true; } bool SimpleEncoder::clockwise() { - return getState() == STATE_RIGHT; + return getState() == SE_STATE_RIGHT; } bool SimpleEncoder::right() { - return getState() == STATE_RIGHT; + return getState() == SE_STATE_RIGHT; } bool SimpleEncoder::counterClockwise() { - return getState() == STATE_LEFT; + return getState() == SE_STATE_LEFT; } bool SimpleEncoder::left() { - return getState() == STATE_LEFT; + return getState() == SE_STATE_LEFT; } -bool SimpleEncoder::buttonPressed() { - return digitalRead(buttonPin) == LOW; +bool SimpleEncoder::changing() { + return getState() != SE_STATE_IDLE; } -float SimpleEncoder::getValue() { - if (trackValue) { - getState(); - return value; - } - return 0; +bool SimpleEncoder::idle() { + return getState() == SE_STATE_IDLE; } -bool SimpleEncoder::changing() { - return getState() != STATE_IDLE; +bool SimpleEncoder::buttonPressed() { + return digitalRead(buttonPin) == LOW; } -bool SimpleEncoder::idle() { - return getState() == STATE_IDLE; +float SimpleEncoder::getValue() { + getState(); + return value; } void SimpleEncoder::setValue(float newValue) { - if((newValue >= lower) && (newValue <= upper)) { - value = newValue; - } + value = newValue; } void SimpleEncoder::adjustValue() { - if (value > upper) + if (value > upper) { value = upper; - else - if (value < lower) - value = lower; + } + else if (value < lower) { + value = lower; + } } void SimpleEncoder::setLimits(float lowerLimit, float upperLimit) { lower = lowerLimit; upper = upperLimit; - checkLimits = (lower != upper) && (lower != 0.0 && upper != 0); + checkLimits = true; } void SimpleEncoder::setLimits(float lowerLimit, float upperLimit, float newValue) { lower = lowerLimit; upper = upperLimit; value = newValue; - trackValue = valueSteps != 0; - checkLimits = (lower != upper) && (lower != 0.0 && upper != 0); + trackValue = true; + checkLimits = true; } void SimpleEncoder::setSteps(float steps) { @@ -123,7 +102,7 @@ void SimpleEncoder::setSteps(float steps) { } STATE SimpleEncoder::getState() { - static STATE thisState = STATE_IDLE; + static STATE thisState = SE_STATE_IDLE; static int lastState = 0; int encAState = digitalRead(encA); int encBState = digitalRead(encB); @@ -131,17 +110,30 @@ STATE SimpleEncoder::getState() { lastState = encAState; if (encBState != encAState) { if (trackValue) value += valueSteps; - thisState = STATE_RIGHT; + thisState = SE_STATE_RIGHT; } else { if (trackValue) value -= valueSteps; - thisState = STATE_LEFT;; + thisState = SE_STATE_LEFT;; } if (trackValue && checkLimits) adjustValue(); } else { - thisState = STATE_IDLE; + thisState = SE_STATE_IDLE; } return thisState; } +/** + * Private Methods + */ + +void initPins(int pinButton, int pinA, int pinB) { + encA = pinA; + encB = pinB; + buttonPin = pinButton; + pinMode(encA, INPUT); + pinMode(encB, INPUT); + pinMode(buttonPin, INPUT_PULLUP); +} + diff --git a/src/SimpleEncoder.h b/src/SimpleEncoder.h index 966174d..a23803c 100644 --- a/src/SimpleEncoder.h +++ b/src/SimpleEncoder.h @@ -15,11 +15,11 @@ #include #pragma once -enum STATE{ST_RIGHT, ST_LEFT, ST_IDLE}; +enum STATE{SE_ST_RIGHT, SE_ST_LEFT, SE_ST_IDLE}; -#define STATE_RIGHT STATE::ST_RIGHT; -#define STATE_LEFT STATE::ST_LEFT; -#define STATE_IDLE STATE::ST_IDLE; +#define SE_STATE_RIGHT STATE::SE_ST_RIGHT; +#define SE_STATE_LEFT STATE::SE_ST_LEFT; +#define SE_STATE_IDLE STATE::SE_ST_IDLE; #define CLOCKWISE clockwise() #define RIGHT right() #define COUNTERCLOCKWISE counterClockwise() @@ -98,24 +98,24 @@ class SimpleEncoder { */ bool left(); - /** + /** + * Returns true if the encoder is moving clockwise or counter clockwise + * @return + */ + bool changing(); + + /** + * Returns true if the encoder is not moving. + * @return + */ + bool idle(); + + /** * Returns true if the button is being pressed * @return bool */ bool buttonPressed(); - /** - * Returns true if the encoder is moving clockwise or counter clockwise - * @return - */ - bool changing(); - - /** - * Returns true if the encoder is not moving. - * @return - */ - bool idle(); - /** * Returns a long of the current vaklue being tracked. * @return long @@ -171,6 +171,7 @@ class SimpleEncoder { STATE getState(); void adjustValue(); + void initPins(int pinButton, int pinA, int pinB); }; #endif //SIMPLEENCODER_SIMPLEENCODER_H