 boolean computeGearPosition(int i)
 {
   // Hold on tight; this is kinda bespoke and messy.
+
+  // First, figure out what, on average, we saw.
+  int photoresistor_history[7];
   int positions_white = 0;
   for (int j=0; j<7; j++)
   {
-    positions_white += photoresistor_history[i][j];
+    photoresistor_history[j] = (
+      photoresistor_totals[i][j] / photoresistor_counts[i][j]
+      < photoresistor_thresholds[i]);
+
+    positions_white += photoresistor_history[j];
   }
 
   // If we saw 2 white, they should be in 1, 4 or 2, 5, and that means we have


        if (failed)
        {
          // Or should we say you failed calibration, not an error?
          sendCastleErrorMessage();
          state = 0;
          resetLights();
        }

// we perhaps win.
int last_selector_pos = 0;

// These store the state of the photoresistors in the current calibration
// phose.  photoresistor_totals[i][j] is the sum of the values we saw from
// photoresistor i in selector position j, and photoresistor_counts[i][j] is
// the number of such values.  (We use these to compute the average value.)
// 
// Only meaningful in state 1, and then only for 0 <= j <= the current selector
// gear position.  When we transition to state 2 we use this to populate
// gear_types and gear_orientations.
// NOTE: The photoresistor is positioned down.
int photoresistor_totals[3][7];
int photoresistor_counts[3][7];

// The state of the ith gear, if we know it.  This is not its current position,
// but rather the position corresponding to selector gear 0!  Only meaningful
// in state 2; set from photoresistor_{totals,counts} when we transition there.
// Type is 0, 1, 2, in order of the correct solution.
// Orientation is 0 in the correct solution, 1 if we are one step clockwise
// from it, and so on.
