 // ENCODER HANDLERS -----------------------------
 
 
-
-void doEncoderA(){
-
-  // look for a low-to-high on channel A
-  if (digitalRead(encoder0PinA) == HIGH) { 
-    // check channel B to see which way encoder is turning
-    if (digitalRead(encoder0PinB) == LOW) {  
-      encoder0Pos = encoder0Pos + 1;         // CW
-    } 
-    else {
-      encoder0Pos = encoder0Pos - 1;         // CCW
-    }
-  }
-  else   // must be a high-to-low edge on channel A                                       
-  { 
-    // check channel B to see which way encoder is turning  
-    if (digitalRead(encoder0PinB) == HIGH) {   
-      encoder0Pos = encoder0Pos + 1;          // CW
-    } 
-    else {
-      encoder0Pos = encoder0Pos - 1;          // CCW
-    }
-  }
-  
-  // mark Z
-  int zeropin = digitalRead(encoder0PinZ);
-  if (encoder0ZeroState == 0 && zeropin == 1) {
-    encoder0Zero = encoder0Pos;
-  }
-  encoder0ZeroState = zeropin;
-}
-
-void doEncoderB(){
-
-  // look for a low-to-high on channel B
-  if (digitalRead(encoder0PinB) == HIGH) {   
-   // check channel A to see which way encoder is turning
-    if (digitalRead(encoder0PinA) == HIGH) {  
-      encoder0Pos = encoder0Pos + 1;         // CW
-    } 
-    else {
-      encoder0Pos = encoder0Pos - 1;         // CCW
-    }
-  }
-  // Look for a high-to-low on channel B
-  else { 
-    // check channel B to see which way encoder is turning  
-    if (digitalRead(encoder0PinA) == LOW) {   
-      encoder0Pos = encoder0Pos + 1;          // CW
-    } 
-    else {
-      encoder0Pos = encoder0Pos - 1;          // CCW
-    }
-  }
-}
-
+// triggered on rising encoder A
+void doEncoderA() {
+  /* via Trammell:
+      For the quadrature, you can also just trigger on the rising edge of one
+    line. When it goes high, you then sample the other line as soon as possible.
+    If it is already high, then you are going clockwise.  If it is still low,
+    then you're going anti-clockwise.
+  */
+  if (ENCODER_PORT & encoder0PinBMask) {
+    encoder0Pos ++;
+  }
+  else {
+    encoder0Pos --;
+  }
+}
 
 
 

  pinMode(encoder0PinB, INPUT); 
  pinMode(encoder0PinZ, INPUT);
  // encoder pin on interrupt 0 (pin 2)
  attachInterrupt(0, doEncoderA, CHANGE);
  // encoder pin on interrupt 1 (pin 3)
  attachInterrupt(1, doEncoderB, CHANGE);  
}

void setupMotorDriver() {


EthernetUDP UDP;
OscUDP etherOSC;  

int MSEC_PER_STATUS = 50; // millseconds between sending status messages

