Skip to content

Instantly share code, notes, and snippets.

@companje
Created July 4, 2012 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save companje/3049050 to your computer and use it in GitHub Desktop.
Save companje/3049050 to your computer and use it in GitHub Desktop.
#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