   }
 }
 
-//
-// Based off of: http://www.circuitsathome.com/mcu/programming/reading-rotary-encoder-on-arduino
-//
-
-int8_t encStates[] = { 0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0 };
-uint8_t oldAB = 0;
-
-int8_t readEncoder()
-{
-  oldAB <<= 1;
-  oldAB |= digitalRead(ENC_A_PIN);
+void pollButton() {
+  // check the button
+  int tempButtonState = digitalRead(BUTTON_PIN);
   
-  oldAB <<= 1;
-  oldAB |= digitalRead(ENC_B_PIN);
-  
-  return ( encStates[( oldAB & 0x0f )]);
+  if (tempButtonState != buttonState) {
+    buttonState = tempButtonState;
+    
+    if (buttonState == LOW) {
+      Serial.println(F("Mute"));
+      bleMultimedia.write(MMKEY_MUTE);
+    }
+  }
 }
 
+void pollEncoder() {
+  // check the encoder
+  int encoderState = encoder.read();
+  
+  if (encoderState != 0) {
+      if (encoderState > 0) {
+        Serial.println(F("Volume up"));
+        bleMultimedia.write(MMKEY_VOL_UP);
+      } else {
+        Serial.println(F("Volume down"));
+        bleMultimedia.write(MMKEY_VOL_DOWN);
+      }
+    
+    encoder.write(0);
+  }
+}
+
+

#include <BLEHIDPeripheral.h>
#include <BLEMultimedia.h>

// define pins (varies per shield/board)
#define BLE_REQ   9
#define BLE_RDY   8
#define BLE_RST   4

#define BUTTON_PIN 5

#define ENC_A_PIN 6
#define ENC_B_PIN 7

BLEHIDPeripheral bleHIDPeripheral = BLEHIDPeripheral(BLE_REQ, BLE_RDY, BLE_RST);
BLEMultimedia bleMultimedia;

int buttonState;

void setup() {
  Serial.begin(9600);
#if defined (__AVR_ATmega32U4__)
  while(!Serial);
#endif

  pinMode(BUTTON_PIN, INPUT_PULLUP);
  buttonState = digitalRead(BUTTON_PIN);
  
  pinMode(ENC_A_PIN, INPUT_PULLUP);
  pinMode(ENC_B_PIN, INPUT_PULLUP);

  if (buttonState == LOW) {
    Serial.println(F("BLE HID Peripheral - clearing bond data"));

    Serial.println(central.address());

    while (bleHIDPeripheral.connected()) {
      pollButton();
      
      pollEncoder();
    }

    // central disconnected
