-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotary_encoder.h
53 lines (45 loc) · 1.27 KB
/
rotary_encoder.h
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
#define ROTARYSTEPS 1
#define ROTARYMIN 0
#define ROTARYMAX 10000
#define MYKTUKAS 4
int lastPos = -1, newPos;
unsigned int button, btn;
unsigned int key;
int buttonState;
int lastButtonState;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
//nuskaitomas myktuko reiksme
void read_key(){
//Nuskaitomas myktukas
int reading = digitalRead(MYKTUKAS);
if (reading != lastButtonState) {lastDebounceTime = millis();}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (!buttonState) {key = !key; }
}
}
lastButtonState = reading;
}
void rotary_encoder(){
//encoder.tick();
newPos = encoder.getPosition() * ROTARYSTEPS;
if (newPos < ROTARYMIN) {
encoder.setPosition(ROTARYMIN / ROTARYSTEPS);
newPos = ROTARYMIN;
} else if (newPos > ROTARYMAX) {
encoder.setPosition(ROTARYMAX / ROTARYSTEPS);
newPos = ROTARYMAX;
}
//Tikrinam ar nera nuspaustas myktukas
read_key();
if(key){button = 1; btn = 1; key = 0;}else
if(newPos > lastPos){button = 2; btn = 2;}else
if(newPos < lastPos){button = 3; btn = 3;}else{button = 0;}
if (lastPos != newPos) {
//Serial.print(newPos);
//Serial.println();
lastPos = newPos;
}
}