Skip to content

Commit

Permalink
First Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
EasyG0ing1 committed May 29, 2022
1 parent 2039947 commit 4f6351b
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 38 deletions.
85 changes: 83 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,84 @@
SimpleEncoder
# SimpleEncoder

- Documentation coming soon
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 <SimpleEncoder.h>

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 <SimpleEncoder.h>

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!
7 changes: 5 additions & 2 deletions examples/UseCaseOne/UseCaseOne.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
6 changes: 3 additions & 3 deletions examples/UseCaseTwo/UseCaseTwo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -23,7 +23,7 @@ void setup() {
}

void loop() {
if (encoder.MOVING) {
if (encoder.CHANGING) {
Serial.println("Current value is: " + String(encoder.VALUE));
}
}
2 changes: 1 addition & 1 deletion keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

EUP KEYWORD1
EDOWN KEYWORD1
MOVING KEYWORD1
CHANGING KEYWORD1
IDLE KEYWORD1
VALUE KEYWORD1

Expand Down
40 changes: 19 additions & 21 deletions src/SimpleEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,49 +29,47 @@ 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() {
return digitalRead(BTN) == LOW;
}

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;
}
17 changes: 8 additions & 9 deletions src/SimpleEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -92,16 +93,14 @@ class SimpleEncoder {
int encA;
int encB;
int BTN;
STATE state;
int encState;
int lastState;
long value;
long lower;
long upper;
bool trackValue = false;

void getState();
STATE getState();
};


#endif //SIMPLEENCODER_SIMPLEENCODER_H

0 comments on commit 4f6351b

Please sign in to comment.