Heine:
Hi! I have a 400ppr, max 330 rpm, 2 phase, rotary encoder. (2200 points per secound)
I found that most of the example codes are using digitalWrite, which seems to be too slow. So i tried using interrupts without digitalread, but i have some problems with it.
volatile int A = 0;
volatile int B = 0;
volatile unsigned int count = 0;
void setup() {
Serial.begin (115200);
pinMode(2, INPUT);
pinMode(3, INPUT);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
attachInterrupt(0, Arising, RISING);
attachInterrupt(1, Brising, RISING);
}
void loop() {
if (count != count) {
Serial.println(count);
}
}
void Arising() {
attachInterrupt(0, Afalling, FALLING);
A = 1;
if (B = 1) {
count ++;
} else {
count --;
}
}
void Afalling() {
attachInterrupt(0, Arising, RISING);
A = 0;
if (B = 1) {
count --;
} else {
count ++;
}
}
void Brising() {
attachInterrupt(1, Bfalling, FALLING);
B = 1;
if (A = 1) {
count --;
} else {
count ++;
}
}
void Bfalling() {
attachInterrupt(1, Brising, RISING);
B = 0;
if (A = 1) {
count ++;
} else {
count --;
}
}
All i get when I'm turning the encoder any way is:
0
65535
0
1
0
0
65535
0
0
65535
65535
0
65535
1
65535
0
1
I've tried changing the phase shift, but the closest I get to function, is disabling one of the interrupts, and I get it to work, but then it only counts upwards either way.
Here is an image of the phase shift:

Can anyone help me with my code?
And what is the fastest way to read a 2 phase shift using arduino?
I've messed with encoders and have come up with this code that has most everything you might want.
I've documented it quite a bit to help you understand what is happening.
Features:
Step counter (Low resolution) counts once per sequence.
Speed in RPM both forward and reverse (-)
#define ClockPin 2 // Must be pin 2 or 3
#define DataPin 9 // can be any other pin
// My Encoder has 400 Clock pulses per revolution
// note that 150000.0 = (60 seonds * 1000000 microseconds)microseconds in a minute / 400 pulses in 1 revolution)
// change the math to get the proper multiplier for RPM for your encoder
#define Multiplier 150000.0 // don't forget a decimal place to make this number a floating point number
volatile long EncoderCounter = 0;
volatile float SpeedInRPM = 0;
void onPin2CHANGECallBackFunction(uint32_t Time, uint32_t PinsChanged, uint32_t Pins){
static uint32_t lTime; // Saved Last Time of Last Pulse
uint32_t cTime; // Current Time
cTime = micros(); // Store the time for RPM Calculations
int32_t dTime; // Delt in time
// Encoder Code
bool DataPinVal = digitalRead(DataPin);
// We know pin 2 just went high to trigger the interrupt
// depending on direction the data pin will either be high or low
EncoderCounter += (DataPinVal) ? 1 : -1; // Should we step up or down?
// End Encoder Code
// calculate the DeltaT between pulses
dTime = cTime - lTime;
lTime = cTime;
SpeedInRPM = Multiplier / ((DataPinVal) ? dTime: (-1 * dTime)); // Calculate the RPM Switch DeltaT to either positive or negative to represent Forward or reverse RPM
}
void setup() {
Serial.begin(115200); //115200
// put your setup code here, to run once:
pinMode(ClockPin, INPUT);
pinMode(DataPin, INPUT);
attachInterrupt(0,onPin2CHANGECallBackFunction,RISING);
}
void loop() {
static unsigned long SpamTimer;
if ( (unsigned long)(millis() - SpamTimer) >= (100)) {
SpamTimer = millis();
Serial.print(EncoderCounter);
Serial.print("\t");
Serial.print(SpeedInRPM, 3);
Serial.print(" RPM");
Serial.println();
SpeedInRPM = 0; // if no pulses occure in the next 100 miliseconds then we must assume that the motor has stopped
}
}
Not much to it 
Z