 //These two are the notes that Ginsing is currently playing.
 int ginsingNote0 = -1;
 int ginsingNote1 = -1;
-#endif 
+#endif
 
 //FirstNote and SecondNote are the notes we want to be playing.
 void playNote(int firstNote, int secondNote)
 {
 
+  if (debug)
+  {
+    Serial.print("Playing notes ");
+    Serial.print(firstNote);
+    Serial.print(" and ");
+    Serial.println(secondNote);
+  }
+
+  //Pick the notes to be played and which channel to play them on
+#if GINSING
+  harpNoteChoice.pickNotes(ginsingNote0, ginsingNote1, firstNote, secondNote);
+  if (ginsingNote0 == -1)
+  {
+    poly->release(GINSING0);
+  }
+  else
+  {
+    poly->setNote(GINSING0, ginsingNotes[ginsingNote0]);
+    poly->trigger(GINSING0);
+  }
+
+  if (ginsingNote1 == -1)
+  {
+    poly->release(GINSING1);
+  }
+  else
+  {
+    poly->setNote(GINSING1, ginsingNotes[ginsingNote1]);
+    poly->trigger(GINSING1);
+  }
+#endif
+}
+
+//Move the motor one step. Sleep, then take a light reading. The sleep
+//gives the sensor time to actually report the new reading.
+int stepTheMotorAndGetLightReading(int directionToStep)
+{
+  myMotor->step(stepSize, directionToStep, DOUBLE);
+  delay(delayBetweenSteps);
+  int currentLightReading = analogRead(lightSensorPin);
+  return currentLightReading;
+}
+
+//Is debug on? If so print some data.
+void checkNotes(int reflectedLightValues[], boolean pluckedNotes[])
+{
+  if (debug)
+  {
+    for (int i = 0; i < numberNotes; i++)
+    {
+      Serial.print(i);
+      Serial.print("=");
+      Serial.print(reflectedLightValues[i]);
+      Serial.print(" - ");
+    }
+    Serial.println("");
+  }
+
+  //So ... are any light readings different from any others?
+  //To find the anomaly, see how different each string is to every other string.
+  //One or two strings should stand out. Those are the plucked strings.
+  harpNoteDetector.checkNotes(reflectedLightValues, pluckedNotes);
+
+  //So what strings are plucked? Note that if more than two are plucked,
+  //that counts as an error ... if that's the case, just keep playing the current notes
+  //and hope the player gets his act straight.
+  int firstNote = -1;
+  int secondNote = -1;
+  if (harpNoteDetector.getNotes(firstNote, secondNote, pluckedNotes))
+  {
     if (debug)
     {
-        Serial.print("Playing notes ");
-        Serial.print(firstNote);
-        Serial.print(" and ");
+      if (firstNote >= 0)
+      {
+        Serial.print("==============PLUCKED 1: ");
+        Serial.println(firstNote);
+      }
+      if (secondNote >= 0)
+      {
+        Serial.print("==============PLUCKED 2: ");
         Serial.println(secondNote);
+      }
     }
-
-    //Pick the notes to be played and which channel to play them on
-#if GINSING
-    harpNoteChoice.pickNotes(ginsingNote0, ginsingNote1, firstNote, secondNote);
-    if (ginsingNote0 == -1)
+    playNote(firstNote, secondNote);
+  }
+  else if (debug)
+  {
+    Serial.println("getNotes returned false - more than three notes were counted as plucked.");
+  }
+  return;
+}
+
+//Is a button pressed? If so move the motor a bit. This lets the user adjust the laser fan so it's pointing upwards properly.
+void checkButtons()
+{
+  Serial.println("in checkButtons");
+#ifdef ADJUSTER_BUTTONS
+  if (digitalRead(buttonApin) == LOW)
+  {
+    myMotor->step(stepSize, BACKWARD, DOUBLE);
+  }
+
+  if (digitalRead(buttonBpin) == LOW)
+  {
+    myMotor->step(stepSize, FORWARD, DOUBLE);
+  }
+#else
+  while (Serial.available() > 0)
+  {
+    char c = Serial.peek();
+    if (c != 'f' && c != 'b')
     {
-        poly->release(GINSING0);
+      break;
     }
     else
     {
-        poly->setNote(GINSING0, ginsingNotes[ginsingNote0]);
-        poly->trigger(GINSING0);
+      myMotor->step(stepSize, c == 'b' ? BACKWARD : FORWARD, DOUBLE);
+      Serial.read();
     }
-
-    if (ginsingNote1 == -1)
-    {
-        poly->release(GINSING1);
-    }
-    else
-    {
-        poly->setNote(GINSING1, ginsingNotes[ginsingNote1]);
-        poly->trigger(GINSING1);
-    }
-#endif
-}
-
-//Move the motor one step. Sleep, then take a light reading. The sleep
-//gives the sensor time to actually report the new reading.
-int stepTheMotorAndGetLightReading(int directionToStep)
-{
-    myMotor->step(stepSize, directionToStep, DOUBLE);
-    delay(delayBetweenSteps);
-    int currentLightReading = analogRead(lightSensorPin);
-    return currentLightReading;
-}
-
-//Is debug on? If so print some data.
-void checkNotes(int reflectedLightValues[], boolean pluckedNotes[])
-{
-    if (debug)
-    {
-        for (int i = 0; i < numberNotes; i++)
-        {
-            Serial.print(i);
-            Serial.print("=");
-            Serial.print(reflectedLightValues[i]);
-            Serial.print(" - ");
-        }
-        Serial.println("");
-    }
-
-    //So ... are any light readings different from any others?
-    //To find the anomaly, see how different each string is to every other string.
-    //One or two strings should stand out. Those are the plucked strings.
-    harpNoteDetector.checkNotes(reflectedLightValues, pluckedNotes);
-
-    //So what strings are plucked? Note that if more than two are plucked,
-    //that counts as an error ... if that's the case, just keep playing the current notes
-    //and hope the player gets his act straight.
-    int firstNote = -1;
-    int secondNote = -1;
-    if (harpNoteDetector.getNotes(firstNote, secondNote, pluckedNotes))
-    {
-        if (debug)
-        {
-            if (firstNote >= 0)
-            {
-                Serial.print("==============PLUCKED 1: ");
-                Serial.println(firstNote);
-            }
-            if (secondNote >= 0)
-            {
-                Serial.print("==============PLUCKED 2: ");
-                Serial.println(secondNote);
-            }
-        }
-        playNote(firstNote, secondNote);
-    }
-    else if (debug)
-    {
-        Serial.println("getNotes returned false - more than three notes were counted as plucked.");
-    }
+  }
+#endif
+}
+
+void loop()
+{
+
+  //There must be at least a handful notes for the code below to work right.
+  if (numberNotes < 5)
+  {
     return;
-}
-
-//Is a button pressed? If so move the motor a bit. This lets the user adjust the laser fan so it's pointing upwards properly.
-void checkButtons()
-{
-#ifdef ADJUSTER_BUTTONS
-    if (digitalRead(buttonApin) == LOW)
-    {
-        myMotor->step(stepSize, BACKWARD, DOUBLE);
-    }
-
-    if (digitalRead(buttonBpin) == LOW)
-    {
-        myMotor->step(stepSize, FORWARD, DOUBLE);
-    }
-#endif
-}
-
-void loop()
-{
-
-    //There must be at least a handful notes for the code below to work right.
-    if (numberNotes < 5)
-    {
-        return;
-    }
-
-    //Run the laser forward, read all values, and see what is there. Note that this pretty much uses one
-    //less note than requested - but the START position counts as a spot. So moving it numberNotes makes that many
-    //strings plus the start string.
-
-    //It's already read the zero item. So read array items 1 through 7.
-    for (int i = 1; i < numberNotes; i++)
-    {
-        reflectedLightValues[i] = stepTheMotorAndGetLightReading(FORWARD);
-        checkNotes(reflectedLightValues, pluckedNotes);
-    }
-
-    checkButtons();
-    checkSonar();
-
-    //It just read item 7. So going backwards, read items 6 through zero.
-    for (int i = numberNotes - 2; i >= 0; i--)
-    {
-        reflectedLightValues[i] = stepTheMotorAndGetLightReading(BACKWARD);
-        checkNotes(reflectedLightValues, pluckedNotes);
-    }
-
-    checkButtons();
-    checkSonar();
-
-}
-
-/*
-TODO
-#) encapsulate ginsing/midi
-
-#) improve (?) initialization of reflected light values
-    a) spinning mirror during initialization
-    b) by taking averaging several values over time?
-    c) update it while looping?
-
-#) classes may not need to exist
-
-
-*/
-
-
-
-
-
-
+  }
+
+  //Run the laser forward, read all values, and see what is there. Note that this pretty much uses one
+  //less note than requested - but the START position counts as a spot. So moving it numberNotes makes that many
+  //strings plus the start string.
+
+  //It's already read the zero item. So read array items 1 through 7.
+  for (int i = 1; i < numberNotes; i++)
+  {
+    reflectedLightValues[i] = stepTheMotorAndGetLightReading(FORWARD);
+    checkNotes(reflectedLightValues, pluckedNotes);
+  }
+
+  checkButtons();
+  checkSonar();
+
+  //It just read item 7. So going backwards, read items 6 through zero.
+  for (int i = numberNotes - 2; i >= 0; i--)
+  {
+    reflectedLightValues[i] = stepTheMotorAndGetLightReading(BACKWARD);
+    checkNotes(reflectedLightValues, pluckedNotes);
+  }
+
+  checkButtons();
+  checkSonar();
+
+}

//that into something to change the GinSing note being played.
int findMultiplier(int height)
{
    /*
    Over 170 = nothing
    140 = high           80 - 5
    125 = normal  64 - 4

    92 = very low       32 - 2
    74 = lower          16 - 1
    62 bottom out
  */
  if (height > 135) return 5;
  if (height > 119) return 4;
  if (height > 100) return 3;
  if (height > 80) return 2;
  //My speaker gets weird at the low values. So I commented that one out.
  //	if (height > 65) return 1;
  return 0;
}

//Read the sonar unit and figure out if the
//notes should move up or down
void checkSonar()
{
  int height = analogRead(sonarPin);

  if (debug)
  {
    Serial.print("Height:");
    Serial.println(height);
  }

  if (height > 170)
  {
    return;
  }
  int multiplier = findMultiplier(height);
  int baseValue = 16 * multiplier;
#ifdef GINSING
  ginsingNotes[0] = (GSNote)baseValue;
  ginsingNotes[1] = (GSNote)(baseValue + 1);
  ginsingNotes[2] = (GSNote)(baseValue + 2);
  ginsingNotes[3] = (GSNote)(baseValue + 3);
  ginsingNotes[4] = (GSNote)(baseValue + 4);
  ginsingNotes[5] = (GSNote)(baseValue + 5);
  ginsingNotes[6] = (GSNote)(baseValue + 6);
#endif
}

