Skip to content

Commit

Permalink
Bug fixes and streamlined some code.
Browse files Browse the repository at this point in the history
  • Loading branch information
EasyG0ing1 committed Oct 25, 2024
1 parent c06ca1d commit 2e60d63
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 76 deletions.
2 changes: 2 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=SimpleEncoder
version=1.2.0
version=1.2.1
author=Michael Sims <sims.mike@gmail.com>
maintainer=Michael Sims <sims.mike@gmail.com>
sentence=SimpleEncoder is a library that makes it easy to use rotary encoders with Arduino's.
Expand Down
108 changes: 50 additions & 58 deletions src/SimpleEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,116 +5,95 @@
#include <SimpleEncoder.h>

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) {
Expand All @@ -123,25 +102,38 @@ 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);
if (encAState != lastState) {
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);
}

35 changes: 18 additions & 17 deletions src/SimpleEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
#include <Arduino.h>
#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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -171,6 +171,7 @@ class SimpleEncoder {

STATE getState();
void adjustValue();
void initPins(int pinButton, int pinA, int pinB);
};

#endif //SIMPLEENCODER_SIMPLEENCODER_H

0 comments on commit 2e60d63

Please sign in to comment.