-
Notifications
You must be signed in to change notification settings - Fork 17
/
Encoder.h
32 lines (27 loc) · 844 Bytes
/
Encoder.h
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
// Encoder
// by Andrew Kramer
// initializes an object to read input from a shaft encoder and
// track the number of encoder ticks using interrupts
// only works with Arduino Uno, Duemilanove, and others that
// have interrupt 0 defined as digital pin 2 and interrupt 1
// defined as digital pin 3
#ifndef Encoder_h
#define Encoder_h
#include "Arduino.h"
class Encoder
{
public:
Encoder(int encoderA, int encoderB,
long deltaT, int ticksPerRev);
int getSpeed(); // returns speed in degrees per second
int getDistance(); // returns distance rotated in degrees
void updateCount();
private:
int _encoderA, _encoderB; // encoder pins
double _degPerTick; // degrees of output shaft rotation per encoder tick
volatile long _count, _oldCount, _newCount;
long _deltaT; // in microseconds
int _lastSpeed;
long _totalCount;
};
#endif