-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathSimpleCounterWithButton.ino
66 lines (48 loc) · 1.45 KB
/
SimpleCounterWithButton.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
/////////////////////////////////////////////////////////////////
#include "Button2.h" // https://github.com/LennartHennigs/Button2
#include "ESPRotary.h"
/////////////////////////////////////////////////////////////////
#define ROTARY_PIN1 D1
#define ROTARY_PIN2 D2
#define BUTTON_PIN D4
#define CLICKS_PER_STEP 4 // this number depends on your rotary encoder
#define SERIAL_SPEED 115200
/////////////////////////////////////////////////////////////////
ESPRotary r;
Button2 b;
/////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(SERIAL_SPEED);
delay(50);
Serial.println("\n\nSimple Counter");
r.begin(ROTARY_PIN1, ROTARY_PIN2, CLICKS_PER_STEP);
r.setChangedHandler(rotate);
r.setLeftRotationHandler(showDirection);
r.setRightRotationHandler(showDirection);
b.begin(BUTTON_PIN);
b.setTapHandler(click);
b.setLongClickHandler(resetPosition);
}
void loop() {
r.loop();
b.loop();
}
/////////////////////////////////////////////////////////////////
// on change
void rotate(ESPRotary& r) {
Serial.println(r.getPosition());
}
// on left or right rotation
void showDirection(ESPRotary& r) {
Serial.println(r.directionToString(r.getDirection()));
}
// single click
void click(Button2& btn) {
Serial.println("Click!");
}
// long click
void resetPosition(Button2& btn) {
r.resetPosition();
Serial.println("Reset!");
}
/////////////////////////////////////////////////////////////////