-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRotEncoderTEST.ino
61 lines (53 loc) · 2.07 KB
/
RotEncoderTEST.ino
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
52
53
54
55
56
57
58
59
60
61
/*****************************************************************************************/
/*** Last Revised using Arduino Version 1.8.9 ***/
/*** Title : Configuration of incremental rotary encoder for angular position tracking ***/
/*** Model : RE30E_500_213_1, three outputs, two in use ***/
/*** NOTE : Code should be usable for other incremental rotary encoder ***/
/*****************************************************************************************/
/*** res is the resolution of the encoder with unit, this model is rated at 500 pulse/revolution ***/
/*** one pulse == 0.72 deg, a period of one pulse will trigger 4 ISR in theory based on datasheet ***/
/*** Hence, this encoder has 0.18 degree per count. Change this value based on your model ***/
#define res 0.18
/*** For Arduino UNO, must use pin 2 and 3 as they are interrupt pins ***/
/*** Check the datasheet for other Arduino boards regarding interrupt pins ***/
/*** Otherwise, consider other options for defining interrupt pins ***/
int outA = 2;
int outB = 3;
int count = 0;
// Change the polarity of decrement and increment of count in the ISR to your preferred direction
// Note that the count polarity in the if-conditions for both ISR are opposite each other
// Additional conditions can be added to track if one revolution is completed - up to user
void setup() {
Serial.begin(9600);
pinMode(outA, INPUT);
pinMode(outB, INPUT);
attachInterrupt(digitalPinToInterrupt(outA), ISR_A, CHANGE);
attachInterrupt(digitalPinToInterrupt(outB), ISR_B, CHANGE);
}
void ISR_A()
{
if (digitalRead(outA) != digitalRead(outB))
{
count--;
}
else
{
count++;
}
}
void ISR_B()
{
if (digitalRead(outB) != digitalRead(outA))
{
count++;
}
else
{
count--;
}
}
void loop() {
Serial.print("encoder (deg) : ");
Serial.println(count*res); //using defined macro -> res, output should be in degrees instead of counts
delay(50);
}