-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwithInterrupt.ino
51 lines (41 loc) · 1.01 KB
/
withInterrupt.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
/*
* Example for using a rotary encoders with interrupts
*/
#include <KY040.h>
// Rotary encoder
#define CLK_PIN 3 // aka. A
#define DT_PIN 2 // aka. B
KY040 g_rotaryEncoder(CLK_PIN,DT_PIN);
// Rotary encoder value (will be set in ISR)
volatile int v_value=0;
// ISR to handle the interrupts for CLK and DT
void ISR_rotaryEncoder() {
// Process pin states for CLK and DT
switch (g_rotaryEncoder.getRotation()) {
case KY040::CLOCKWISE:
v_value++;
break;
case KY040::COUNTERCLOCKWISE:
v_value--;
break;
}
}
void setup() {
Serial.begin(9600);
// Set interrupts for CLK and DT
attachInterrupt(digitalPinToInterrupt(CLK_PIN), ISR_rotaryEncoder, CHANGE);
attachInterrupt(digitalPinToInterrupt(DT_PIN), ISR_rotaryEncoder, CHANGE);
}
void loop() {
static int lastValue = 0;
int value;
// Get rotary encoder value set in ISR
cli();
value = v_value;
sei();
// Show, if value has changed
if (lastValue != value) {
Serial.println(value);
lastValue = value;
}
}