-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathTwoRotaryEncoders.ino
More file actions
141 lines (117 loc) · 3.93 KB
/
TwoRotaryEncoders.ino
File metadata and controls
141 lines (117 loc) · 3.93 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* ESP32RotaryEncoder: TwoRotaryEncoders.ino
*
* This is a basic example of how to instantiate two distinct Rotary Encoders.
*
* Rotary Encoder #1:
* - Turning the knob will increment/decrement a value between 1 and 10,
* and print it to the serial console.
*
* - Pressing the button will enable/disable Rotary Encoder #2.
*
* Rotary Encoder #2:
* - Turning the knob will increment/decrement a value between -100 and 100,
* and print it to the serial console.
*
* - Pressing the button will enable/disable Rotary Encoder #1.
*
* While a rotary encoder is disabled, turning the knob or pressing the button
* will have no effect.
*
* Created 3 October 2023
* Updated 1 November 2023
* By Matthew Clark
*/
#include <ESP32RotaryEncoder.h>
const uint8_t RE1_DI_ENCODER_A = 27;
const uint8_t RE1_DI_ENCODER_B = 14;
const int8_t RE1_DI_ENCODER_SW = 12;
const int8_t RE1_DO_ENCODER_VCC = 13;
const uint8_t RE2_DI_ENCODER_A = 35;
const uint8_t RE2_DI_ENCODER_B = 32;
const int8_t RE2_DI_ENCODER_SW = 33;
const int8_t RE2_DO_ENCODER_VCC = 25;
RotaryEncoder rotaryEncoder1( RE1_DI_ENCODER_A, RE1_DI_ENCODER_B, RE1_DI_ENCODER_SW, RE1_DO_ENCODER_VCC );
RotaryEncoder rotaryEncoder2( RE2_DI_ENCODER_A, RE2_DI_ENCODER_B, RE2_DI_ENCODER_SW, RE2_DO_ENCODER_VCC );
void printKnob1Value( long value )
{
Serial.printf( "RE1 value: %ld\n", value );
}
void printKnob2Value( long value )
{
Serial.printf( "RE2 value: %ld\n", value );
}
void button1ToggleRE2( unsigned long duration )
{
if( rotaryEncoder2.isEnabled() )
{
rotaryEncoder2.disable();
Serial.println( "RE2 is disabled." );
}
else
{
rotaryEncoder2.enable();
Serial.println( "RE2 is enaabled." );
}
}
void button2ToggleRE1( unsigned long duration )
{
if( rotaryEncoder1.isEnabled() )
{
rotaryEncoder1.disable();
Serial.println( "RE1 is disabled." );
}
else
{
rotaryEncoder1.enable();
Serial.println( "RE1 is enaabled." );
}
}
void setup_RE1()
{
// Uncomment if your encoder does not have its own pull-up resistors
//rotaryEncoder.enableEncoderPinPullup();
//rotaryEncoder.enableButtonPinPullup();
// Range of values to be returned by the encoder: minimum is 1, maximum is 10
// The third argument specifies whether turning past the minimum/maximum will
// wrap around to the other side.
// In this example, turn past 10, wrap to 1; turn past 1, wrap to 10
rotaryEncoder1.setBoundaries( 1, 10, true );
// The function specified here will be called every time the knob is turned
// and the current value will be passed to it
rotaryEncoder1.onTurned( &printKnob1Value );
// The function specified here will be called every time the button is pushed and
// the duration (in milliseconds) that the button was down will be passed to it
rotaryEncoder1.onPressed( &button1ToggleRE2 );
// This is where the inputs are configured and the interrupts get attached
rotaryEncoder1.begin();
}
void setup_RE2()
{
// Uncomment if your encoder does not have its own pull-up resistors
//rotaryEncoder.enableEncoderPinPullup();
//rotaryEncoder.enableButtonPinPullup();
// Range of values to be returned by the encoder: minimum is -100, maximum is 100
// The third argument specifies whether turning past the minimum/maximum will wrap
// around to the other side.
// In this example, turn past 100, stay on 100; turn past -100, stay on -100
rotaryEncoder2.setBoundaries( -100, 100, false );
// The function specified here will be called every time the knob is turned
// and the current value will be passed to it
rotaryEncoder2.onTurned( &printKnob2Value );
// The function specified here will be called every time the button is pushed and
// the duration (in milliseconds) that the button was down will be passed to it
rotaryEncoder2.onPressed( &button2ToggleRE1 );
// This is where the inputs are configured and the interrupts get attached
rotaryEncoder2.begin();
}
void setup()
{
Serial.begin( 115200 );
setup_RE1();
setup_RE2();
}
void loop()
{
// Your stuff here
}