 void dof_loop() {
   /* Get a new sensor event */
   sensors_event_t accel, mag, gyro, temp;
-
   lsm.getEvent(&accel, &mag, &gyro, &temp);
 
   float mx, my = 0.0;
-  float accel_x = clamp(accel.acceleration.x, -20, 20);
-  float accel_y = clamp(accel.acceleration.y, -20, 20);
+  float accelX = clamp(accel.acceleration.x, accelMin, accelMax);
+  float accelY = clamp(accel.acceleration.y, accelMin, accelMax);
 
   if (accel.acceleration.x > xRange[1] || accel.acceleration.x < xRange[0]) {
-    my = map(accel_x - xNormal, -20, 20, 200, -200);
-    mx = map(accel_y - yNormal, -20, 20, 200, -200);
+    my = map(accelX, accelMin, accelMax, outMax, outMin);
+    mx = map(accelY, accelMin, accelMax, outMax, outMin);
   }
   else if (accel.acceleration.y > yRange[1] || accel.acceleration.y < yRange[0]) {
-    my = map(accel_x - xNormal, -20, 20, 200, -200);
-    mx = map(accel_y - yNormal, -20, 20, 200, -200);
+    my = map(accelX, accelMin, accelMax, outMax, outMin);
+    mx = map(accelY, accelMin, accelMax, outMax, outMin);
   }
+
   Mouse.move(mx, my, 0);
-  delay(10);
+  delay(5);
 }
 
 void mpr_loop() {
- // Get the currently touched pads
+  // Get the currently touched pads
   currtouched = cap.touched();
-  
+  checkTouch();
+  // reset our state
+  lasttouched = currtouched;
+}
+
+void checkTouch() {
   for (uint8_t i=0; i < 12; i++) {
     // it if *is* touched and *wasnt* touched before, alert!
-    if ((currtouched & _BV(i)) && !(lasttouched & _BV(i)) ) {
-      Serial.print(i); Serial.println(" touched");
-    }
+    if ( (currtouched & _BV(i)) && !(lasttouched & _BV(i)) ) touchHandler(i);
+
     // if it *was* touched and now *isnt*, alert!
-    if (!(currtouched & _BV(i)) && (lasttouched & _BV(i)) ) {
-      Serial.print(i); Serial.println(" released");
-    }
-  }
-
-  // reset our state
-  lasttouched = currtouched;
-
-  // comment out this line for detailed data from the sensor!
-  return;
-  
-  // debugging info, what
-  Serial.print("\t\t\t\t\t\t\t\t\t\t\t\t\t 0x"); Serial.println(cap.touched(), HEX);
-  Serial.print("Filt: ");
-  for (uint8_t i=0; i < 12; i++) {
-    Serial.print(cap.filteredData(i)); Serial.print("\t");
-  }
-  Serial.println();
-  Serial.print("Base: ");
-  for (uint8_t i=0; i < 12; i++) {
-    Serial.print(cap.baselineData(i)); Serial.print("\t");
-  }
-  Serial.println();
-  
-  // put a delay so it isn't overwhelming
-  delay(100); 
+    if ( !(currtouched & _BV(i)) && (lasttouched & _BV(i)) ) releaseHandler(i);
+  } 
+}
+
+void touchHandler(int id) {
+  Serial.print(id); Serial.println(" released");
+}
+
+void releaseHandler(int id) {
+  Serial.print(id); Serial.println(" touched");
 }
 
 void calibrate(float x, float y) {

void dof_setup() {
  if(!lsm.begin()) { while(1); }

  /* Setup the sensor gain and integration time */
  configureSensor();
  
  // Set a max and min value to not do anything
  // calibrate(accel.acceleration.x, accel.acceleration.y);
  
  sensors_event_t accel, mag, gyro, temp;
  lsm.getEvent(&accel, &mag, &gyro, &temp);  
}

#include <Adafruit_LSM9DS0.h>
#include "Adafruit_MPR121.h"

#define LSM9DS0_XM_CS 10
#define LSM9DS0_GYRO_CS 9
#define LSM9DS0_SCLK 13
#define LSM9DS0_MISO 12
#define LSM9DS0_MOSI 11

// LSM9DS0, use I2C, ID #1005
Adafruit_LSM9DS0 lsm = Adafruit_LSM9DS0(1005);

// MPR121, use I2C, ID #1000 
Adafruit_MPR121 cap = Adafruit_MPR121();

float xNormal = -3.0,
      yNormal = 1,
      // Clamp inputs to this range (m/s^2)
      accelMax =  20,
      accelMin = -20,
      // Map inputs to this range (pixels)
      outMin   =  200,
      outMax   = -200;

float xRange[] = {0,0};
float yRange[] = {0,0};

uint16_t lasttouched = 0;
uint16_t currtouched = 0;

// Configures the gain and integration time for the TSL2561
void configureSensor(void) {
  // 1.) Set the accelerometer range
  //lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_2G);
