diff --git a/Readme.md b/Readme.md index d29dee5..9403911 100644 --- a/Readme.md +++ b/Readme.md @@ -1,3 +1,84 @@ -SimpleEncoder +# SimpleEncoder -- Documentation coming soon \ No newline at end of file +SimpleEncoder is a very simple, easy to use library for using rotary encoders with an Arduino. + +To use it, you simply invoke the library by passing in the pin assignments from your encoder. + +```c++ +#include + +const int BUTTON = 10; +const int PINA = 11; +const int PINB = 12; + +SimpleEncoder encoder(BUTTON, PINA, PINB); +``` + +The library handles configuring the pins for you. Then to use the encoder, you can engage the library +in a couple of different ways, all of which need to be in your loop method. + +```c++ +void loop() { + if (encoder.UP) { + /Code for upward motion + } + if (encoder.DOWN) { + /Code for downward motion + } +} +``` + +SimpleEncoder can even keep track of a value that changes when the rotary is turned + +```c++ +#include + +const int BUTTON = 10; +const int PINA = 11; +const int PINB = 12; +long myValue = 0; +long minValue = -100; +long maxValue = 100; + +SimpleEncoder encoder(BUTTON, PINA, PINB, myValue, minValue, maxValue); +``` + +Then simply query the library for any changes in the encoder and if they exist, get the new value + +```c++ +void loop() { + if(encoder.CHANGING) { + myValue = encoder.VALUE; + } +} +``` + +It's just that easy. + +There are two example sketches that show how to use the library. + +These are the public methods in the library: + +``` +up() - Rotary moving clockwise +down() - rotary moving counter clockwise +buttonPressed() - Button was pressed +changing() - rotary is moving in either direction +idle() - rotary is idle and not changing +getValue() - get the value of the variable being tracked +``` + +And these are the alternate MACROS that can be used instead of the method names: + +``` +UP - Rotary moving clockwise +DOWN - rotary moving counter clockwise +BUTTON - Button was pressed +CHANGING - rotary is moving in either direction +IDLE - rotary is idle and not changing +VALUE - get the value of the variable being tracked +``` + +Make sure that your encoder has 5v connected to it - usually sourced from the Arduino, and that it is also connected to ground or things won't work properly. + +Enjoy! \ No newline at end of file diff --git a/examples/UseCaseOne/UseCaseOne.ino b/examples/UseCaseOne/UseCaseOne.ino index 0040de0..6af511f 100644 --- a/examples/UseCaseOne/UseCaseOne.ino +++ b/examples/UseCaseOne/UseCaseOne.ino @@ -20,10 +20,13 @@ void setup() { } void loop() { - if (encoder.EUP) { + if (encoder.UP) { Serial.println("Encoder moving up"); } - if (encoder.EDOWN) { + if (encoder.DOWN) { Serial.println("Encoder moving down"); } + if (encoder.BUTTON) { + Serial.println("Button Pressed"); + } } diff --git a/examples/UseCaseTwo/UseCaseTwo.ino b/examples/UseCaseTwo/UseCaseTwo.ino index bff16d1..1b8af48 100644 --- a/examples/UseCaseTwo/UseCaseTwo.ino +++ b/examples/UseCaseTwo/UseCaseTwo.ino @@ -13,8 +13,8 @@ const int BTN = 10; const int encA = 11; const int encB = 12; long startValue = 0; -long lowerValue = -1000; -long upperValue = 1000; +long lowerValue = -100; +long upperValue = 100; SimpleEncoder encoder(BTN, encA, encB, startValue, lowerValue, upperValue); @@ -23,7 +23,7 @@ void setup() { } void loop() { - if (encoder.MOVING) { + if (encoder.CHANGING) { Serial.println("Current value is: " + String(encoder.VALUE)); } } diff --git a/keywords.txt b/keywords.txt index 5773ac2..e02e3d4 100644 --- a/keywords.txt +++ b/keywords.txt @@ -8,7 +8,7 @@ EUP KEYWORD1 EDOWN KEYWORD1 -MOVING KEYWORD1 +CHANGING KEYWORD1 IDLE KEYWORD1 VALUE KEYWORD1 diff --git a/src/SimpleEncoder.cpp b/src/SimpleEncoder.cpp index edc583d..a1509ec 100644 --- a/src/SimpleEncoder.cpp +++ b/src/SimpleEncoder.cpp @@ -29,13 +29,11 @@ SimpleEncoder::SimpleEncoder(int pinButton, int pinA, int pinB, long trackingVal } bool SimpleEncoder::up() { - getState(); - return state == UP; + return getState() == STATE_UP; } bool SimpleEncoder::down() { - getState(); - return state == DOWN; + return getState() == STATE_DOWN; } bool SimpleEncoder::buttonPressed() { @@ -43,35 +41,35 @@ bool SimpleEncoder::buttonPressed() { } long SimpleEncoder::getValue() { - getState(); - return trackValue ? value : 0; + if (trackValue) { + getState(); + if (value >= upper) value = upper; + if (value <= lower) value = lower; + return value; + } + return 0; } bool SimpleEncoder::changing() { - getState(); - return state != STATE_IDLE; + return getState() != STATE_IDLE; } bool SimpleEncoder::idle() { - getState(); - return state == STATE_IDLE; + return getState() == STATE_IDLE; } -void SimpleEncoder::getState() { - state = STATE_IDLE; +STATE SimpleEncoder::getState() { encState = digitalRead(encA); if (encState != lastState) { - if (digitalRead(encA) != encState) { - state = UP; - if (trackValue) value++; + lastState = encState; + if (digitalRead(encB) != encState) { + if (trackValue) value += 1; + return STATE_UP; } else { - state = DOWN; - if (trackValue) value--; + if (trackValue) value -= 1; + return STATE_DOWN; } } - if (trackValue) { - if (value >= upper) value = upper; - if (value <= lower) value = lower; - } + return STATE_IDLE; } \ No newline at end of file diff --git a/src/SimpleEncoder.h b/src/SimpleEncoder.h index e845eb6..bd3bb37 100644 --- a/src/SimpleEncoder.h +++ b/src/SimpleEncoder.h @@ -17,14 +17,15 @@ enum STATE{up, down, idle}; -#define UP STATE::up; -#define DOWN STATE::down; +#define STATE_UP STATE::up; +#define STATE_DOWN STATE::down; #define STATE_IDLE STATE::idle; -#define EUP up() -#define EDOWN down() -#define MOVING changing() +#define UP up() +#define DOWN down() +#define BUTTON buttonPressed() +#define CHANGING changing() #define IDLE idle() -#define VALUE value() +#define VALUE getValue() class SimpleEncoder { @@ -92,7 +93,6 @@ class SimpleEncoder { int encA; int encB; int BTN; - STATE state; int encState; int lastState; long value; @@ -100,8 +100,7 @@ class SimpleEncoder { long upper; bool trackValue = false; - void getState(); + STATE getState(); }; - #endif //SIMPLEENCODER_SIMPLEENCODER_H