 }
 
 // based on the pot value, we know which of the 7 positions the selector gear is in
-// returns an int 0-6 inclusive or -1 in error cases
-// if the pot value is out of range, throws an error and informs the castle operators that the puzzle is broken
-// the position of the selector gear uniquely determines the position of all of the other gears
+// returns an int 0-6 inclusive, -1 if we are in between positions, and -2 if we
+// have an unreasonable value
 int getSelectorGearPosition() 
 {
   int val = analogRead(pot);
+  Serial.print(val);
+  Serial.print(" ");
+
+  if (val > pot_locs[0])
+  {
+    return -2;
+  }
   
   for (int i=0; i<7; i++)
   {
-    if (val > pot_locs[i] && val < pot_locs[i+1])
+    if (val > pot_locs[2*i])
+    {
+      return -1;
+    }
+    else if (val <= pot_locs[2*i] && val >= pot_locs[2*i+1])
     {
       return i;
     }
   }
-  
-  // else: val is below pot_locs[0] or greater than pot_locs[7]
-  // puzzle is borken
-  return -1;
-}
+
+  return -2;
+}

  for(int i=0; i<7; i++)
  {
    digitalWrite(output_lights[i], LOW);
  }
  for(int i=0; i<3; i++) 
  {
    digitalWrite(gear_lodged[i], LOW);
  }

  // TODO(bhomberg): close all solenoids (??)

// PIN NUMBERS
int output_lights[7] = {38, 39, 40, 41, 42, 43, 44};

int red_button = 30;

int pot = A0;

int photoresistors[3] = {A1, A2, A3};

// if the potentiometer is between pot_locs[2*i] and pod_locs[2*i+1], then we 
// say we are in position i.  otherwise, we are between positions.
int pot_locs[14] = {1024, 885, 825, 690, 640, 450, 390, 280, 190, 70, 50, 24, 12, 0};


// STATE OF THE WORLD
