Created
July 28, 2011 06:05
-
-
Save arduinoboard/1111051 to your computer and use it in GitHub Desktop.
The file that is currently on an Arduino Uno with a serial number of test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Created by WilliamK @ Wusik Dot Com (c) 2010 | |
http://arduino.wusik.com | |
*/ | |
#include "WConstants.h" | |
#include "Encoder.h" | |
// ------------------------------------------------------------------------------------------- // | |
Encoder::Encoder(int8_t pinLeft, int8_t pinRight, int8_t pinClick) | |
{ | |
encoderPinLeft = pinLeft; | |
encoderPinRight = pinRight; | |
encoderPinClick = pinClick; | |
pinMode(encoderPinLeft, INPUT); | |
digitalWrite(encoderPinLeft, HIGH); | |
pinMode(encoderPinRight, INPUT); | |
digitalWrite(encoderPinRight, HIGH); | |
pinMode(encoderPinClick, INPUT); | |
digitalWrite(encoderPinClick, HIGH); | |
isClicked = false; | |
prevClickValue = 1; | |
integerMode = false; | |
isClickedEvent = false; | |
currentPosition = 0; | |
prevPosition = 0; | |
tempPosition = 0; | |
tempPosition2 = 0; | |
maxValue = 127; | |
minValue = 0; | |
moveRate = 1.0f; | |
timeLastClick = millis(); | |
timeClickStarted = 0; | |
} | |
// ------------------------------------------------------------------------------------------- // | |
void Encoder::lowLevelTick(void) | |
{ | |
tempPosition2 = (digitalRead(encoderPinRight) * 2) + digitalRead(encoderPinLeft); | |
if (encodeSeq[tempPosition2] == tempPosition) currentPosition -= moveRate; | |
else if (encodeSeq[tempPosition] == tempPosition2) currentPosition += moveRate; | |
tempPosition = tempPosition2; | |
} | |
// ------------------------------------------------------------------------------------------- // | |
void Encoder::lowLevelClick(void) | |
{ | |
newClickValue = digitalRead(encoderPinClick); | |
if (newClickValue != prevClickValue) | |
{ | |
if ((millis() - timeLastClick) < clickDebounceTime) return; | |
if (newClickValue == 0 && !isClicked) | |
{ | |
isClickedEvent = true; | |
isClicked = true; | |
timeClickStarted = millis(); | |
} | |
else if (newClickValue == 1) isClicked = false; | |
timeLastClick = millis(); | |
} | |
prevClickValue = newClickValue; | |
} | |
// ------------------------------------------------------------------------------------------- // | |
boolean Encoder::onClickHold(void) | |
{ | |
if (isClicked && (millis() - timeClickStarted) >= clickHoldTime) return true; | |
return false; | |
} | |
// ------------------------------------------------------------------------------------------- // | |
boolean Encoder::hasClick(void) | |
{ | |
if (isClickedEvent) | |
{ | |
isClickedEvent = false; | |
return true; | |
} | |
return false; | |
} | |
// ------------------------------------------------------------------------------------------- // | |
void Encoder::setIntegerMode(boolean mode) | |
{ | |
integerMode = mode; | |
} | |
// ------------------------------------------------------------------------------------------- // | |
boolean Encoder::tick(void) | |
{ | |
lowLevelTick(); | |
lowLevelClick(); | |
return (hasChanged() || isClickedEvent); | |
} | |
// ------------------------------------------------------------------------------------------- // | |
boolean Encoder::hasChanged() | |
{ | |
currentPosition = constrain(currentPosition, minValue, maxValue); | |
if (integerMode) | |
{ | |
if ((int)getPosition() != (int)prevPosition) | |
{ | |
prevPosition = getPosition(); | |
return true; | |
} | |
} | |
else | |
{ | |
if (getPosition() != prevPosition) | |
{ | |
prevPosition = getPosition(); | |
return true; | |
} | |
} | |
return false; | |
} | |
// ------------------------------------------------------------------------------------------- // | |
float Encoder::getPosition(void) | |
{ | |
if (integerMode) return (int)currentPosition; | |
return currentPosition; | |
} | |
// ------------------------------------------------------------------------------------------- // | |
void Encoder::setMinMax(float _min, float _max) | |
{ | |
minValue = _min; | |
maxValue = _max; | |
currentPosition = constrain(currentPosition, minValue, maxValue); | |
} | |
// ------------------------------------------------------------------------------------------- // | |
void Encoder::setPosition(float _position) | |
{ | |
currentPosition = _position; | |
currentPosition = constrain(currentPosition, minValue, maxValue); | |
} | |
// ------------------------------------------------------------------------------------------- // | |
void Encoder::setRate(float _rate) | |
{ | |
moveRate = _rate; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Created by WilliamK @ Wusik Dot Com (c) 2010 | |
http://arduino.wusik.com | |
*/ | |
#ifndef ENCODER_h | |
#define ENCODER_h | |
#include <inttypes.h> | |
#include "HardwareSerial.h" | |
const int8_t encodeSeq[] = {1, 3, 0, 2}; | |
#define clickDebounceTime 50 | |
#define clickHoldTime 500 | |
// ------------------------------------------------------------------------------------------- // | |
class Encoder | |
{ | |
public: | |
Encoder(int8_t pinLeft, int8_t pinRight, int8_t pinClick); | |
boolean tick(void); | |
void lowLevelTick(void); | |
void lowLevelClick(void); | |
float getPosition(void); | |
void setMinMax(float _min, float _max); | |
void setPosition(float _position); | |
void setRate(float _rate); | |
boolean hasChanged(void); | |
boolean hasClick(void); | |
boolean onClickHold(void); | |
void setIntegerMode(boolean mode); | |
boolean isClicked; | |
private: | |
int8_t encoderPinLeft; | |
int8_t encoderPinRight; | |
int8_t encoderPinClick; | |
float prevPosition; | |
float maxValue; | |
float minValue; | |
float currentPosition; | |
float moveRate; | |
int8_t tempPosition; | |
int8_t tempPosition2; | |
boolean isForward; | |
boolean isClickedEvent; | |
int8_t prevClickValue; | |
int8_t newClickValue; | |
boolean integerMode; | |
unsigned long timeLastClick; | |
unsigned long timeClickStarted; | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment