  ***************************************************************/
 
 #define MODE_JIRO 1
-#define MODE_DEBUG 1
-#define MODE_DEBUG_SHOW_TIME 0
+#define MODE_DEBUG 0
 
 #define CHANNELS 4
 
-#define SAMPLE_CACHE_LENGTH 15
+#define SAMPLE_CACHE_LENGTH 12
 #define POWER_CACHE_LENGTH 3
 
-#define LIGHT_THRES 500
-#define HEAVY_THRES 1000
+#define LIGHT_THRES 5000
+#define HEAVY_THRES 20000
 
 #include <limits.h>
 #include <Keyboard.h>

    power [i] -= tempInt * tempInt;
    tempInt = sampleCache [i].get ();
    power [i] += tempInt * tempInt;
    
    if (power [i] < LIGHT_THRES) {
      power [i] = 0;
    }
    
    powerCache [i].put (power [i]);
    lastChannelSample [i] = channelSample [i];
    
    if (powerCache [i].get (1) == 0) {
      triggered [i] = false;
    }

    bool isDescending = true;
    for (short int j = 0; j < POWER_CACHE_LENGTH - 1; j++){
      if (!triggered [i] && (powerCache [i].get (j - 1) > powerCache [i].get (j))) {
        isDescending = false;
        break;
      }
    }
    if (isDescending) {
      if (power [i] >= HEAVY_THRES) {
        triggered [i] = true;
#if !MODE_DEBUG
        Keyboard.print (heavyKeys [i]);
#endif
      } else if (power [i] >= LIGHT_THRES) {
        triggered [i] = true;
#if !MODE_DEBUG
        Keyboard.print (lightKeys [i]);
#endif
      }

#if MODE_DEBUG
    calcLastTime += micros () - lastTime;
#endif
    }
    
#if MODE_DEBUG
    Serial.print (power [i]);
    Serial.print ("\t");
#endif

  }

#if MODE_DEBUG
// Damn Arduino plotter, the Y axis scale changes too frequently! Use these values to "lock" the plotter's scale.
  Serial.print (50000);
  Serial.print ("\t");
  Serial.print (0);
  Serial.print ("\t");

  Serial.println ("");
#endif

// Force the sample frequency to be less than 1000Hz
  unsigned int frameTime = micros () - lastTime;
  lastTime = micros ();
  if (frameTime < 1000) {
    delayMicroseconds (1000 - frameTime);
  }
}
