forked from jherland/arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotary_encoder_basics.ino
68 lines (56 loc) · 1.56 KB
/
rotary_encoder_basics.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
62
63
64
65
66
67
//these pins can not be changed 2/3 are special pins
int pin1 = 2;
int pin2 = 3;
volatile byte ring_buffer[256] = { 0 };
volatile byte rb_write; // current write position in ring buffer
byte rb_read; // current read position in ringbuffer
byte pin_state = 0;
int encoder_value = 0;
void setup()
{
Serial.begin(115200);
pinMode(pin1, INPUT);
pinMode(pin2, INPUT);
digitalWrite(pin1, HIGH); //turn pullup resistor on
digitalWrite(pin2, HIGH); //turn pullup resistor on
Serial.println("Ready");
//call updateEncoder() when any high/low changed seen
//on interrupt 0 (pin 2), or interrupt 1 (pin 3)
attachInterrupt(0, updateEncoder, CHANGE);
attachInterrupt(1, updateEncoder, CHANGE);
}
void updateEncoder()
{
ring_buffer[rb_write++] = PIND;
}
void loop()
{
if (rb_read == rb_write)
return; // Nothing has been added to the ring buffer
// Process the next value in the ring buffer
byte value = (ring_buffer[rb_read] >> 2) & 0b11;
// Did the value actually change since last reading?
if (value != (pin_state & 0b11)) {
// Append to history of pin states
pin_state = (pin_state << 2) | value;
// Are we in a "rest" state?
if (value == 0b11) {
// Figure out how we got here
switch (pin_state & 0b111111) {
case 0b000111:
// CCW
encoder_value--;
Serial.print("<- ");
Serial.println(encoder_value);
break;
case 0b001011:
// CW
encoder_value++;
Serial.print("-> ");
Serial.println(encoder_value);
break;
}
}
}
rb_read++;
}