-
Notifications
You must be signed in to change notification settings - Fork 1
/
RotaryEncoder.cpp
51 lines (48 loc) · 1.39 KB
/
RotaryEncoder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "RotaryEncoder.h"
RotaryEncoder::RotaryEncoder(int ENC_A, int ENC_B, int multiplier, int stepSize, int pauseLength) {
_ENC_A = ENC_A;
_ENC_B = ENC_B;
_multiplier = multiplier;
_stepSize = stepSize;
_pauseLength = pauseLength;
_oldENC_A = 0;
_oldENC_B = 0;
_lastENCread = 0;
_ENCcounter = 0;
_lastENCreadTime = micros();
//define pin modes
pinMode(_ENC_A, INPUT);
pinMode(_ENC_B, INPUT);
digitalWrite(_ENC_A,HIGH); //these pins do not have pull up resistors on an attiny...
digitalWrite(_ENC_B,HIGH); //you must pull them up on the board.
}
int RotaryEncoder::readEncoder() {
int a0Pin = digitalRead(_ENC_A);
int a1Pin = digitalRead(_ENC_B);
int changevalue = 1;
int returnVal = 0;
if(a0Pin != _oldENC_A || a1Pin != _oldENC_B) {
if(_oldENC_A == a1Pin && _oldENC_B != a0Pin) {
returnVal = -1;
} else if (_oldENC_B == a0Pin && _oldENC_A != a1Pin) {
returnVal = 1;
}
_oldENC_A = a0Pin;
_oldENC_B = a1Pin;
if(returnVal != 0) {
if(returnVal == _lastENCread) {
_ENCcounter++;
if((micros() - _lastENCreadTime) < _pauseLength) {
changevalue = max((_ENCcounter/_stepSize)*_multiplier,1);
}
_lastENCreadTime = micros();
} else {
_ENCcounter=0;
}
_lastENCread = returnVal;
}
return returnVal*changevalue;
} else {
return 0;
}
}