Created
July 4, 2012 19:15
-
-
Save companje/3049050 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Arduino.h" | |
class Encoder { | |
public: | |
int pinA, pinB, prevA, prevB, index, value; | |
//delta, prev, v; | |
Encoder() { | |
} | |
Encoder(int pinA, int pinB) : pinA(pinA), pinB(pinB) { | |
pinMode(pinA, INPUT); | |
pinMode(pinB, INPUT); | |
prevA = 0; | |
prevB = 0; | |
value = 0; | |
} | |
int update() { | |
static int8_t states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0}; | |
int a = digitalRead(pinA); | |
int b = digitalRead(pinB); | |
int index = prevA+(a<<1)+(prevB<<2)+(b<<3); | |
prevA = a; | |
prevB = b; | |
return value+=states[index]; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment