-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEncoder.ino
More file actions
82 lines (73 loc) · 2.26 KB
/
Encoder.ino
File metadata and controls
82 lines (73 loc) · 2.26 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
/**
* A basic example of using the EventEncoder
* Note: This example is for an encoder without a button so
* button events and 'pressed changed' etc will not be fired. .
* See the EventEncoderButton example for that.
*/
//First include your chosen encoder library
#include <Encoder.h> //PJRC's Encoder library
//Then include the adapter for your chosen encoder library
#include <EncoderAdapter/PjrcEncoderAdapter.h> //Adapter for PJRC's Encoder
//Then include EventEncoder
#include <EventEncoder.h>
const uint8_t encoderPin1 = 2; //must be in interrupt pin
const uint8_t encoderPin2 = 3; //should be in interrupt pin
//Create an encoder adapter
PjrcEncoderAdapter encoderAdapter(encoderPin1, encoderPin2); //Adapter for PJRC's Encoder.
//Create the EventEncoder, passing a reference to the adapter
EventEncoder myEncoder(&encoderAdapter); //Create an EventEncoder
/**
* Utility function to print the encoder events to Serial.
* See other examples for other event types
*/
void printEvent(InputEventType iet) {
switch (iet) {
case InputEventType::ENABLED :
Serial.print("ENABLED");
break;
case InputEventType::DISABLED :
Serial.print("DISABLED");
break;
case InputEventType::IDLE :
Serial.print("IDLE");
break;
case InputEventType::CHANGED :
Serial.print("CHANGED");
break;
default:
Serial.print("Unknown event: ");
Serial.print((uint8_t)iet);
break;
}
}
/**
* A function to handle the events
* Can be called anything but requires InputEventType and
* EventEncoder& defined as parameters.
*/
void onEncoderEvent(InputEventType et, EventEncoder& ee) {
Serial.print("onEncoderEvent: ");
printEvent(et);
if ( et == InputEventType::CHANGED ) {
Serial.print(", increment: ");
Serial.print(ee.increment());
Serial.print(", position: ");
Serial.print(ee.position());
}
Serial.println();
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
myEncoder.begin();
delay(500);
Serial.println("EventEncoder Basic Example");
//Link the event(s) to your function
myEncoder.setCallback(onEncoderEvent);
}
void loop() {
// You must call update() for every defined EventEncoder.
// This will update the state of the encoder and
// fire the appropriate events.
myEncoder.update();
}