Skip to content

Commit 614b8d1

Browse files
authored
Merge pull request #5 from Spirik/encoder-example-upd
Example-04 Encoder update
2 parents 834eda2 + e50e8a0 commit 614b8d1

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ How to use
3737

3838
### Install
3939

40-
Library format is compatible with Arduino IDE 1.5.x+. There are two ways to install the library:
40+
Library format is compatible with Arduino IDE 1.5.x+. There are number of ways to install the library:
4141

4242
- Download ZIP-archive directly from [Releases](https://github.com/Spirik/KeyDetector/releases) section (or Master branch) and extract it into KeyDetector folder inside your Library folder.
4343
- Using Library Manager (since Arduino IDE 1.6.2): navigate to `Sketch > Include Library > Manage Libraries` inside your Arduino IDE and search for KeyDetector library, then click `Install`. (Alternatively you can add previously downloaded ZIP through `Sketch > Include Library > Add .ZIP Library` menu).
44+
- Using Library Manager in Arduino IDE 2: see [documentation](https://docs.arduino.cc/software/ide-v2/tutorials/ide-v2-installing-a-library) for details.
4445

4546
Whichever option you choose you may need to reload IDE afterwards.
4647

examples/Example-04_Encoder/Example-04_Encoder.ino

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#include <KeyDetector.h>
1414

15-
// Define signal identifiers for three outputs of encoder (channel A, channel B and a push-button)
15+
// Define signal identifiers for three outputs of encoder (Channel A, Channel B and a push-button)
1616
#define KEY_A 1
1717
#define KEY_B 2
1818
#define KEY_C 3
@@ -22,6 +22,8 @@ const byte channelA = 2;
2222
const byte channelB = 3;
2323
const byte buttonPin = 4;
2424

25+
byte chanB = HIGH; // Variable to store Channel B readings
26+
2527
// Array of Key objects that will link GEM key identifiers with dedicated pins
2628
// (it is only necessary to detect signal change on a single channel of the encoder, either A or B;
2729
// order of the channel and push-button Key objects in an array is not important)
@@ -35,7 +37,7 @@ Key keys[] = {{KEY_A, channelA}, {KEY_C, buttonPin}};
3537
// Make sure to adjust debounce delay to better fit your rotary encoder.
3638
// Also it is possible to enable pull-up mode when buttons wired with pull-up resistors (as in this case).
3739
// Analog threshold is not necessary for this example and is set to default value 16.
38-
KeyDetector myKeyDetector(keys, sizeof(keys)/sizeof(Key), /* debounceDelay= */ 5, /* analogThreshold= */ 16, /* pullup= */ true);
40+
KeyDetector myKeyDetector(keys, sizeof(keys)/sizeof(Key), /* debounceDelay= */ 3, /* analogThreshold= */ 16, /* pullup= */ true);
3941

4042
void setup() {
4143
// Serial communications setup
@@ -51,25 +53,28 @@ void setup() {
5153
}
5254

5355
void loop() {
56+
// Reading Channel B signal beforehand to account for possible delays due to polling nature of KeyDetector inner algorithm
57+
chanB = digitalRead(channelB);
58+
5459
myKeyDetector.detect();
5560

56-
// Press event (e.g. when button was pressed or channel A signal chaged to HIGH)
61+
// Press event (e.g. when button was pressed or Channel A signal chaged to HIGH)
5762
switch (myKeyDetector.trigger) {
5863
case KEY_A:
59-
//Signal from Channel A of encoder was detected
60-
if (digitalRead(channelB) == LOW) {
61-
// If channel B is LOW then the knob was rotated CW
64+
// Signal from Channel A of encoder was detected
65+
if (chanB == LOW) {
66+
// If Channel B is LOW then the knob was rotated CCW
6267
if (myKeyDetector.current == KEY_C) {
63-
Serial.println("Rotation CW with button pressed");
68+
Serial.println("Rotation CCW with button pressed");
6469
} else {
65-
Serial.println("Rotation CW");
70+
Serial.println("Rotation CCW");
6671
}
6772
} else {
68-
// If channel B is HIGH then the knob was rotated CCW
73+
// If Channel B is HIGH then the knob was rotated CW
6974
if (myKeyDetector.current == KEY_C) {
70-
Serial.println("Rotation CCW with button pressed");
75+
Serial.println("Rotation CW with button pressed");
7176
} else {
72-
Serial.println("Rotation CCW");
77+
Serial.println("Rotation CW");
7378
}
7479
}
7580
break;
@@ -79,23 +84,23 @@ void loop() {
7984
break;
8085
}
8186

82-
// Release event (e.g. when button was released or channel A signal changed to LOW)
87+
// Release event (e.g. when button was released or Channel A signal changed to LOW)
8388
switch (myKeyDetector.triggerRelease) {
8489
case KEY_A:
85-
//Signal from Channel A of encoder was detected
86-
if (digitalRead(channelB) == LOW) {
87-
// If channel B is LOW then the knob was rotated CCW
90+
// Signal from Channel A of encoder was detected
91+
if (chanB == LOW) {
92+
// If Channel B is LOW then the knob was rotated CW
8893
if (myKeyDetector.current == KEY_C) {
89-
Serial.println("Rotation CCW with button pressed (release)");
94+
Serial.println("Rotation CW with button pressed (release)");
9095
} else {
91-
Serial.println("Rotation CCW (release)");
96+
Serial.println("Rotation CW (release)");
9297
}
9398
} else {
94-
// If channel B is HIGH then the knob was rotated CW
99+
// If Channel B is HIGH then the knob was rotated CCW
95100
if (myKeyDetector.current == KEY_C) {
96-
Serial.println("Rotation CW with button pressed (release)");
101+
Serial.println("Rotation CCW with button pressed (release)");
97102
} else {
98-
Serial.println("Rotation CW (release)");
103+
Serial.println("Rotation CCW (release)");
99104
}
100105
}
101106
break;

0 commit comments

Comments
 (0)