-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathRangedCounter.ino
74 lines (54 loc) · 1.93 KB
/
RangedCounter.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
/////////////////////////////////////////////////////////////////
#include "ESPRotary.h"
/////////////////////////////////////////////////////////////////
#define ROTARY_PIN1 D1
#define ROTARY_PIN2 D2
#define CLICKS_PER_STEP 4 // this number depends on your rotary encoder
#define MIN_POS 0
#define MAX_POS 40
#define START_POS 20
#define INCREMENT 2 // this number is the counter increment on each step
#define SERIAL_SPEED 115200
/////////////////////////////////////////////////////////////////
ESPRotary r;
/////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(SERIAL_SPEED);
delay(50);
r.begin(ROTARY_PIN1, ROTARY_PIN2, CLICKS_PER_STEP, MIN_POS, MAX_POS, START_POS, INCREMENT);
r.setChangedHandler(rotate);
r.setLeftRotationHandler(showDirection);
r.setRightRotationHandler(showDirection);
r.setLowerOverflowHandler(lower);
r.setUpperOverflowHandler(upper);
// enable to be only notified one about an out-of-bounds hit
// r.retriggerEvent(false);
Serial.println("\n\nRanged Counter");
Serial.println("You can only set values between " + String(MIN_POS) + " and " + String(MAX_POS) +".");
Serial.print("Current position: ");
Serial.println(r.getPosition());
Serial.println();
// enable this to be also notified about rotation events when the bounds are hit
// r.triggerOnBounds(true); // off per default
}
void loop() {
r.loop();
}
/////////////////////////////////////////////////////////////////
// on change
void rotate(ESPRotary& r) {
Serial.println(r.getPosition());
}
// out of bounds event
void upper(ESPRotary& r) {
Serial.println("upper bound hit");
}
// out of bounds event
void lower(ESPRotary& r) {
Serial.println("lower bound hit");
}
// on left or right rotation
void showDirection(ESPRotary& r) {
Serial.println(r.directionToString(r.getDirection()));
}
/////////////////////////////////////////////////////////////////