-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterruptEncoders.ino
108 lines (91 loc) · 2.23 KB
/
interruptEncoders.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//Interrupt routines
void IRAM_ATTR m1_A_changed() {
M1_A_state = digitalRead(M1_ENC_A);
M1_B_state = digitalRead(M1_ENC_B);
//Rising
if (M1_A_state == HIGH) {
if (M1_B_state == HIGH) {
m1Raw = m1Raw - 1;
} else if (M1_B_state == LOW) {
m1Raw = m1Raw + 1;
}
}
//Falling
else if (M1_A_state == LOW) {
if (M1_B_state == HIGH) {
m1Raw = m1Raw + 1;
} else if (M1_B_state == LOW) {
m1Raw = m1Raw - 1;
}
}
}
void IRAM_ATTR m1_B_changed() {
M1_A_state = digitalRead(M1_ENC_A);
M1_B_state = digitalRead(M1_ENC_B);
//Rising
if (M1_B_state == HIGH) {
if (M1_A_state == HIGH) {
m1Raw = m1Raw + 1;
} else if (M1_A_state == LOW) {
m1Raw = m1Raw - 1;
}
}
//Falling
else if (M1_B_state == LOW) {
if (M1_A_state == HIGH) {
m1Raw = m1Raw - 1;
} else if (M1_A_state == LOW) {
m1Raw = m1Raw + 1;
}
}
}
void IRAM_ATTR m2_A_changed() {
M2_A_state = digitalRead(M2_ENC_A);
M2_B_state = digitalRead(M2_ENC_B);
//Rising
if (M2_A_state == HIGH) {
if (M2_B_state == HIGH) {
m2Raw = m2Raw + 1;
} else if (M2_B_state == LOW) {
m2Raw = m2Raw - 1;
}
}
//Falling
else if (M2_A_state == LOW) {
if (M2_B_state == HIGH) {
m2Raw = m2Raw - 1;
} else if (M2_B_state == LOW) {
m2Raw = m2Raw + 1;
}
}
}
void IRAM_ATTR m2_B_changed() {
M2_A_state = digitalRead(M2_ENC_A);
M2_B_state = digitalRead(M2_ENC_B);
//Rising
if (M2_B_state == HIGH) {
if (M2_A_state == HIGH) {
m2Raw = m2Raw - 1;
} else if (M2_A_state == LOW) {
m2Raw = m2Raw + 1;
}
}
//Falling
else if (M2_B_state == LOW) {
if (M2_A_state == HIGH) {
m2Raw = m2Raw + 1;
} else if (M2_A_state == LOW) {
m2Raw = m2Raw - 1;
}
}
}
void initEncoderInterrupt() {
pinMode(M1_ENC_A, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(M1_ENC_A), m1_A_changed, CHANGE);
pinMode(M1_ENC_B, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(M1_ENC_B), m1_B_changed, CHANGE);
pinMode(M2_ENC_A, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(M2_ENC_A), m2_A_changed, CHANGE);
pinMode(M2_ENC_B, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(M2_ENC_B), m2_B_changed, CHANGE);
}