-
Notifications
You must be signed in to change notification settings - Fork 1
/
rotary_encoder.cpp
43 lines (38 loc) · 1021 Bytes
/
rotary_encoder.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
#include "Arduino.h"
#include "rotary_encoder.h"
Encoder::Encoder(int _pin_a, int _pin_b)
{
pin_a = _pin_a;
pin_b = _pin_b;
pinMode(pin_a, INPUT);
pinMode(pin_b, INPUT);
}
void Encoder::update()
{
state_a = digitalRead(pin_a); // Reads the "current" state of the outputA
// If the previous and the current state of the outputA are different, that means a Pulse has occured
if (state_a != state_a_old)
{
speed = 1000000.0 / (micros() - last_update_time);
// If the A state is different to the B state, that means the encoder is rotating clockwise
if (digitalRead(pin_b) != state_a)
{
++position;
}
else
{
--position;
speed = -speed;
}
//Serial.print("Enc ");
//Serial.println(position);
//Serial.println(micros() - last_update_time);
last_update_time = micros();
//Serial.println("speed " + String(speed));
}
else
{
speed *= 0.95;
}
state_a_old = state_a; // Updates the previous state of the outputA with the current state
}