-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathLeftOrRight.ino
More file actions
124 lines (98 loc) · 3.23 KB
/
LeftOrRight.ino
File metadata and controls
124 lines (98 loc) · 3.23 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
/**
* ESP32RotaryEncoder: LeftOrRight.ino
*
* This is a simple example of how to track whether the knob was
* turned left or right instead of tracking a numeric value
*
* Created 1 November 2023
* By Matthew Clark
*/
#include <ESP32RotaryEncoder.h>
// Change these to the actual pin numbers that
// you've connected your rotary encoder to
const uint8_t DI_ENCODER_A = 27;
const uint8_t DI_ENCODER_B = 14;
const int8_t DI_ENCODER_SW = 12;
const int8_t DO_ENCODER_VCC = 13;
RotaryEncoder rotaryEncoder( DI_ENCODER_A, DI_ENCODER_B, DI_ENCODER_SW, DO_ENCODER_VCC );
// Used by the `loop()` to know when to
// fire an event when the knob is turned
volatile bool turnedRightFlag = false;
volatile bool turnedLeftFlag = false;
void turnedRight()
{
Serial.println( "Right ->" );
// Set this back to false so we can watch for the next move
turnedRightFlag = false;
}
void turnedLeft()
{
Serial.println( "<- Left" );
// Set this back to false so we can watch for the next move
turnedLeftFlag = false;
}
void knobCallback( long value )
{
// See the note in the `loop()` function for
// an explanation as to why we're setting
// boolean values here instead of running
// functions directly.
// Don't do anything if either flag is set;
// it means we haven't taken action yet
if( turnedRightFlag || turnedLeftFlag )
return;
// Set a flag that we can look for in `loop()`
// so that we know we have something to do
switch( value )
{
case 1:
turnedRightFlag = true;
break;
case -1:
turnedLeftFlag = true;
break;
}
// Override the tracked value back to 0 so that
// we can continue tracking right/left events
rotaryEncoder.setEncoderValue( 0 );
}
void buttonCallback( unsigned long duration )
{
Serial.printf( "boop! button was down for %lu ms\n", duration );
}
void setup()
{
Serial.begin( 115200 );
// Uncomment if your encoder does not have its own pull-up resistors
//rotaryEncoder.enableEncoderPinPullup();
//rotaryEncoder.enableButtonPinPullup();
// The encoder will only return -1, 0, or 1, and will not wrap around.
rotaryEncoder.setBoundaries( -1, 1, false );
// The function specified here will be called every time the knob is turned
// and the current value will be passed to it
rotaryEncoder.onTurned( &knobCallback );
// 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
rotaryEncoder.onPressed( &buttonCallback );
// This is where the inputs are configured and the interrupts get attached
rotaryEncoder.begin();
}
void loop()
{
// Check to see if a flag is set (is true), and if so, run a function.
//
// Why do it like this instead of within the `knobCallback()` function?
//
// Because the `knobCallback()` function is executed by a internal
// timer ISR (interrupt service routine), which needs to be fast and
// lean, so setting a boolean is the fastest way, and then let the main
// `loop()` do the heavy-lifting.
//
// If you were to let the `knobCallback()` function do all the work,
// there's a chance you'd have issues with WiFi or Bluetooth connections,
// or even cause the MCU to crash.
if( turnedRightFlag )
turnedRight();
else if( turnedLeftFlag )
turnedLeft();
}