Skip to content

Commit f4a2187

Browse files
committed
added new encoder examples
1 parent bd4dd66 commit f4a2187

3 files changed

Lines changed: 190 additions & 0 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Encoder change with pushbutton
3+
4+
Many encoders change 4 steps per detent step. This sketch reads
5+
an encoder and prints the direction of change when it's been
6+
turned one detent step.
7+
8+
Additionally, many encoders have a built-in pushbutton.
9+
This sketch reads both the encoder's change and the pushbutton
10+
change.
11+
12+
created 17 June 2019
13+
by Tom Igoe
14+
*/
15+
16+
#include <Encoder.h>
17+
18+
const int buttonPin = 5; // pushbutton is on pin 5
19+
Encoder knob(0, 1); // initialize encoder on pins 0 and 1
20+
int lastKnobState = 0; // set last knob state
21+
int lastButtonState = 0; // set last button state
22+
23+
void setup() {
24+
// open serial communications
25+
Serial.begin(9600);
26+
pinMode (buttonPin, INPUT_PULLUP);
27+
}
28+
29+
void loop() {
30+
// read the encoder:
31+
int knobState = knob.read();
32+
//get the change in the encoder:
33+
int knobChange = knobState - lastKnobState;
34+
// if it's changed by 4 or more pulses:
35+
if (abs(knobChange) >= 4) {
36+
// You just want direction of change, not value, so
37+
// divide value by abs. value to get 1 or -1:
38+
knobChange = knobChange / abs(knobChange);
39+
// print the result:
40+
Serial.println(knobChange);
41+
// save the current state for comparison next time:
42+
lastKnobState = knobState;
43+
}
44+
45+
// read the pushbutton:
46+
int buttonState = digitalRead(buttonPin);
47+
// if the pushbutton has changed:
48+
if (buttonState != lastButtonState) {
49+
// wait a few milliseconds to debounce the button
50+
delay(3);
51+
// print the type of change, depending on the
52+
// state of the button:
53+
if (buttonState == LOW) {
54+
Serial.println("button was pressed");
55+
} else {
56+
Serial.println("button was released");
57+
}
58+
// save the current button state for comparison next time:
59+
lastButtonState = buttonState;
60+
}
61+
}

EncoderExample/EncoderExample.ino

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Encoder change
3+
4+
Many encoders change 4 steps per detent step. This sketch reads
5+
an encoder and prints the direction of change when it's been
6+
turned one detent step.
7+
8+
created 17 June 2019
9+
by Tom Igoe
10+
*/
11+
12+
#include <Encoder.h>
13+
14+
Encoder knob(0, 1); // initialize encoder on pins 0 and 1
15+
int lastKnobState = 0; // set last knob state
16+
17+
void setup() {
18+
// open serial communications
19+
Serial.begin(9600);
20+
}
21+
22+
void loop() {
23+
// read the encoder:
24+
int knobState = knob.read();
25+
//get the change in the encoder:
26+
int knobChange = knobState - lastKnobState;
27+
// if it's changed by 4 or more pulses:
28+
if (abs(knobChange) >= 4) {
29+
// You just want direction of change, not value, so
30+
// divide value by abs. value to get 1 or -1:
31+
knobChange = knobChange / abs(knobChange);
32+
// print the result:
33+
Serial.println(knobChange);
34+
// save the current state for next time:
35+
lastKnobState = knobState;
36+
}
37+
38+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
Encoder change with pushbutton
3+
4+
Reads an encoder and its pushbutton and uses the changes
5+
to control an LED. The knob turns the intensity up and down
6+
and the button turns it on and off, saving the intensity
7+
when turned off for when it turns back on.
8+
9+
created 17 June 2019
10+
by Tom Igoe
11+
*/
12+
13+
#include <Encoder.h>
14+
15+
const int buttonPin = 5; // pushbutton is on pin 5
16+
const int ledPin = 3; // LED is on pin 3
17+
Encoder knob(0, 1); // initialize encoder on pins 0 and 1
18+
int lastKnobState = 0; // set last knob state
19+
int lastButtonState = 0; // set last button state
20+
int intensity = 0; // intensity of the LED
21+
int lastIntensity = 0; // intensity before LED was last turned off
22+
23+
void setup() {
24+
// open serial communications
25+
Serial.begin(9600);
26+
// set I/O pin states:
27+
pinMode (buttonPin, INPUT_PULLUP);
28+
pinMode (ledPin, OUTPUT);
29+
}
30+
31+
void loop() {
32+
// add any encoder knob change to the intensity:
33+
intensity = intensity + readEncoderChange();
34+
// constrain it to 0-255:
35+
intensity = constrain(intensity, 0, 255);
36+
37+
// if the button is released:
38+
if (readButtonChange() == 1) {
39+
// if the light is on, turn it off:
40+
if (intensity > 0) {
41+
// save intensity before it's turned off:
42+
lastIntensity = intensity;
43+
intensity = 0;
44+
} else {
45+
// restore it to the last intensity before turning off:
46+
intensity = lastIntensity;
47+
}
48+
}
49+
// update the intensity of the LED:
50+
analogWrite(ledPin, intensity);
51+
}
52+
53+
int readEncoderChange() {
54+
// returns 0 for no change, -1 for one direction, 1 for the other:
55+
int result = 0;
56+
// read the encoder:
57+
int knobState = knob.read();
58+
//get the change in the encoder:
59+
int knobChange = knobState - lastKnobState;
60+
// if it's changed by 4 or more pulses:
61+
if (abs(knobChange) >= 4) {
62+
// You just want direction of change, not value, so
63+
// divide value by abs. value to get 1 or -1:
64+
result = knobChange / abs(knobChange);
65+
// save the current state for comparison next time:
66+
lastKnobState = knobState;
67+
}
68+
return result;
69+
}
70+
71+
int readButtonChange() {
72+
// returns 0 for no change, 1 for HIGH, -1 for LOW
73+
int result = 0;
74+
// read the pushbutton:
75+
int buttonState = digitalRead(buttonPin);
76+
// if the pushbutton has changed:
77+
if (buttonState != lastButtonState) {
78+
// wait a few milliseconds to debounce the button
79+
delay(3);
80+
// print the type of change, depending on the
81+
// state of the button:
82+
if (buttonState == LOW) {
83+
result = -1;
84+
} else {
85+
result = 1;
86+
}
87+
// save the current button state for comparison next time:
88+
lastButtonState = buttonState;
89+
}
90+
return result;
91+
}

0 commit comments

Comments
 (0)