forked from pimoroni/pimoroni-pico
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoder_read_change.cpp
45 lines (34 loc) · 1.11 KB
/
encoder_read_change.cpp
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
#include <cstdio>
#include "pico/stdlib.h"
#include "encoder.hpp"
/*
An example of how to read a mechanical rotary encoder, only when a change has occurred.
*/
using namespace encoder;
// Create an encoder on the 3 ADC pins, using PIO 0 and State Machine 0
const uint PIN_A = 26; // The A channel pin
const uint PIN_B = 28; // The B channel pin
const uint PIN_C = 27; // The common pin
Encoder enc(pio0, 0, {PIN_A, PIN_B}, PIN_C);
int main() {
stdio_init_all();
// Sleep 8 seconds to give enough time to connect up a terminal
sleep_ms(8000);
// Uncomment the below line to reverse the counting direction
// enc.direction(REVERSED_DIR);
// Initialise the encoder
enc.init();
// Print out the initial count, step, and turn (they should all be zero)
printf("Count = %ld, ", enc.count());
printf("Step = %d, ", enc.step());
printf("Turn = %d\n", enc.turn());
// Loop forever
while(true) {
if(enc.delta() != 0) {
// Print out the new count, step, and turn
printf("Count = %ld, ", enc.count());
printf("Step = %d, ", enc.step());
printf("Turn = %d\n", enc.turn());
}
}
}