forked from pimoroni/pimoroni-pico
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoder_read_speed.cpp
39 lines (28 loc) · 918 Bytes
/
encoder_read_speed.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
#include <cstdio>
#include "pico/stdlib.h"
#include "encoder.hpp"
/*
An example of how to read the speed a mechanical rotary encoder is being turned at.
*/
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();
// Uncomment the below line to reverse the counting direction
// enc.direction(REVERSED_DIR);
// Initialise the encoder
enc.init();
// Loop forever
while(true) {
Encoder::Capture capture = enc.capture();
printf("Count = %ld, ", capture.count());
printf("Angle = %f, ", capture.degrees());
printf("Freq = %f, ", capture.frequency());
printf("Speed = %f\n", capture.degrees_per_second());
sleep_ms(100);
}
}