     }
   }
   else if (bytesAvail) {
-    char val = Serial.read();
-    if ( val == 1 )
-    {
-      backgroundLayer.swapBuffers(true);
-      dataPos=0;
-      dataExpected = kMatrixWidth * kMatrixHeight * 3;
-      digitalWrite(ledPin, HIGH);
-    }
-    else {
+      char val = Serial.read();
+
+   if (gotCommand)
+   {
       buffer[dataPos++] = val;
       dataExpected--;
       if (dataExpected == 0) {
         swap = true;
       }
     }
+   else if (commandPos < USB_MARKER_LENGTH) {
+      usbCommand[commandPos] = val;
+      if (usbCommand[commandPos] == usbMarker[commandPos]) {
+        commandPos++;
+      }
+      else {
+        commandPos = 0;
+      }
+    }
+    else if (commandPos < USB_COMMAND_LENGTH) {
+      usbCommand[commandPos] = val;
+      commandPos++;
+      if (commandPos >= USB_COMMAND_LENGTH)
+      {
+      gotCommand = true;
+      backgroundLayer.swapBuffers(true);
+      dataPos=0;
+      dataExpected = kMatrixWidth * kMatrixHeight * 3;
+      }
+    }
   }
 
   if (swap) {
     frameCount++;
+    gotCommand = false;
+    commandPos = 0;
     char frameText[12];
     itoa(frameCount, frameText, 10);
     backgroundLayer.swapBuffers(true);

int dataPos = 0;
int dataExpected = 0;


// the setup() method runs once, when the sketch starts
void setup() {

    frameOn = !frameOn;
  }
}
