 
 void loop()
 {
-
-  for (int i = 1; i < gNoteCount; i++)
-  {
-    if (gMotorStepDelay > 0) {
-      stepMotor(FORWARD);
-      waitForNote(i);
-    }
-  }
-
+  static int iNote = 0;
+  static int motorDirection = FORWARD;
+
+  waitForNote(iNote, gMotorStepDelay);
   checkSonar();
-
-  for (int i = gNoteCount - 2; i >= 0; i--)
-  {
-    if (gMotorStepDelay > 0) {
-      stepMotor(BACKWARD);
-      waitForNote(i);
-    }
-  }
-
   checkButtons();
-  checkSonar();
-}
+
+  if (gMotorStepDelay > 0) 
+  {
+    stepMotor(motorDirection);
+    if (motorDirection == FORWARD)
+    {
+      iNote++;
+      if (iNote == (gNoteCount-1))
+      {
+        motorDirection = BACKWARD;
+      }
+    }
+    else
+    { 
+      iNote--;
+      if (iNote == 0)
+      {
+        motorDirection = FORWARD;
+      }
+    }
+  }
+}
+
+void shutdown ()
+{
+  if (gGinsing == true)
+  {
+     gGS.end();
+  }
+}
+


}


void SendMIDI(char cmd, char data1, char data2) 
{
  if (gMute == false)
  {
    Serial.print(cmd);
    Serial.print(data1);
    Serial.print(data2);
  }
}


void waitForNote(int note) 
{
  static bool noteStates[NOTE_COUNT_MAX] = {0};
  bool detected = false;
  bool currentlyOn = noteStates[note];
  unsigned long duration = millis() + gMotorStepDelay;
  while (millis() < duration)
  {
    int light = analogRead(LIGHT_SENSOR_PIN);

  Serial.println(cmd);  
}

void playMidiNote(bool on, int note) 
{
  if (gMidi == false)
  {
    return;
  }
  char cmd = on ? MIDICMD_NOTEON : MIDICMD_NOTEOFF;
  char data1 = 0x4a + note;
  char data2 = 0x49;
  Serial.print(cmd);
  Serial.print(data1);
  Serial.print(data2);
}

void playGinsingNote(bool on, int note)
{
  if (gGinsing == false)
  {
    return;
  }

  if (on == true)
  {
    gGSPoly->setNote(1, gGSNote[note]);
    gGSPoly->trigger(1);
  }
  else
  {
    gGSPoly->release(1);
  }
}
  
  
void playNote(bool on, int note)
{
  if (on == true && gMute == true) 
  {
    return;
  }

  playMidiNote(on, note);

  playGinsingNote(on, note);
}


void playNotesCmd()
{
  Serial.println(F("Running playNoteCmd"));
  Serial.print(F("Playing notes: "));
  for (int iNote = 0; iNote < NOTE_COUNT_MAX; iNote++)
  {
    Serial.print(iNote); Serial.print(F(" "));
    playNote(true, iNote);
    delay(250);
    playNote(false, iNote);
  }
  Serial.println();
}

void setupSerialCommands()
{
  sCmd.addCommand("SampleLight", sampleLightCmd);
  sCmd.addCommand("SetLightSensitivity", setLightSensitivityCmd);
  sCmd.addCommand("SetNoteCount", setNoteCountCmd);
  sCmd.addCommand("AutoPosition", autoPositionMirror);
  sCmd.addCommand("PlayNotes", playNotesCmd);
  sCmd.setDefaultHandler(unrecognizedSerialCmd);  
}

void setupGinsing()
{
  if (gGinsing == false)
  {
    return;
  }
  
  Serial.println(F("Setting up ginsing"));

#define rcvPin  4       // this is the pin used for receiving    ( can be either 4 or 12 )
#define sndPin  3       // this is the pin used for transmitting ( can be either 3 or 11 ) 
#define ovfPin  2       // this is the pin used for overflow control ( can be either 2 or 10 )
  gGS.begin( rcvPin , sndPin , ovfPin );

//  GinSingVoice *voice = gGS.getVoice();                    // get the interface for voice mode
//  voice->begin();                                         // enter voice mode
//  voice->preview();                                       // preview the mode

  gGSPoly = gGS.getPoly();                           // get the poly mode interface
  gGSPoly->begin ();  // enter poly mode
}

void setup()
{
  setupSerialCommands();

  Log.Init(gLogLevel, BAUD);

  //For the light sensor
  analogReference(EXTERNAL);

  setupGinsing();
  
  setupMotor();  

  //Get some initial values for each light string
  gLightAverage = sampleLight(50);

}

//Read the sonar unit and figure out if the
