 // --------------------------------------------------
 //   get the match number and increment
 // --------------------------------------------------
-int getMatchNum(void) {
+int getMatchNum(void)
+{
   File matchFile;
   char matchNumFile[] = "matchNum.txt";
   int match = 0;
 
-  if (!SD.exists(matchNumFile)) {
+  if (!SD.exists(matchNumFile))
+  {
     matchFile = SD.open(matchNumFile, FILE_WRITE);
-    if (matchFile) {
+    if (matchFile)
+    {
       matchFile.print(++match);
       matchFile.close();
     }
     else
+    {
       return (0);
+    }
   }
-  else {
+  else
+  {
     matchFile = SD.open(matchNumFile, FILE_READ);
-    if (matchFile) {
+    if (matchFile)
+    {
       char fileData = matchFile.read();
-      while (fileData != -1) {
+      while (fileData != -1)
+      {
         match = match*10 + (fileData - '0');
         fileData = matchFile.read();
       }

// --------------------------------------------------
//   setup function
// --------------------------------------------------
void setup() {
  int match;

  system("telnetd -l /bin/sh");   // open galileo for ethernet access

  static unsigned long redTime = 0;
  int packetSize;
  int i;
  static int adc[6] = {0,0,0,0,0,0};
  static char chan = 0;
  float temp;
  static unsigned long muxTime = 0;
  char tempString[40];

  switch(s)
  {

    case 0:   //wait for first packet
      packetSize = Udp.parsePacket();
      if (packetSize)
      {
        if (!createDataFile())
        {
          i = Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
          initialTime = millis();
          packetTime = initialTime;
          writeTime = 0;
          if (packetBuffer[0] != '#')
          {
            dataFile.print(";\r\n");
            dataFile.print(writeTime);
            sprintf(tempString, ";%d;%d;%d;%d;%d;%d;", adc[0], adc[1], adc[2], adc[3], adc[4], adc[5]);
            dataFile.print(tempString);
          }
          else
          {
            dataFile.print(";");
          }
          dataFile.write((uint8_t *)packetBuffer, i);
          dataFile.flush();
          redTime = initialTime + errorFlash;
          digitalWrite(redPin, HIGH);
          digitalWrite(redPin2, HIGH);
          s = 1;
        }
        else
        {
          s = 7;
        }
      } // if
      break;

    case 1:   // wait for packet
      packetSize = Udp.parsePacket();
      if (packetSize)
      {
        i = Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
        packetTime = millis();
        writeTime = packetTime - initialTime;
        if (packetBuffer[0] != '#')
        {
          dataFile.print(";\r\n");
          dataFile.print(writeTime);
          sprintf(tempString, ";%d;%d;%d;%d;%d;%d;", adc[0], adc[1], adc[2], adc[3], adc[4], adc[5]);
          dataFile.print(tempString);
        }
        else
        {
          dataFile.print(";");
        }
        dataFile.write((uint8_t *)packetBuffer, i);
        dataFile.flush();
        redTime = packetTime + errorFlash;
        digitalWrite(redPin, HIGH);
        digitalWrite(redPin2, HIGH);
      }
      if ((millis() - packetTime) > timeOut)
      {
        s = 3;
      }
      break;

    case 3:  // close
      dataFile.print("\r\n");
      dataFile.close();
      s = 0;
      break;

    case 7:   // error
      break;

  }  // switch s


  //led state machine

  switch(ledState)
  {
    case 0:
      if (millis() > redTime)
      {
        digitalWrite(redPin, LOW);
        digitalWrite(redPin2, LOW);
      }
      if ( millis() > grnTime)
      {
        digitalWrite(grnPin, HIGH);
        digitalWrite(grnPin2, HIGH);
        grnTime = millis() + shortOn;
        ledState = 1;
      }
      break;

    case 1:
      if (millis() > redTime)
      {
        digitalWrite(redPin, LOW);
        digitalWrite(redPin2, LOW);
      }
      if (millis() > grnTime)
      {
        digitalWrite(grnPin, LOW);
        digitalWrite(grnPin2, LOW);
        grnTime = millis() + shortOff;
        if (s == 0)
        {
          ledState = 3;
        }
        else
        {
          ledState = 2;
        }
      }
      break;

    case 2:
      if (millis() > redTime)
      {
        digitalWrite(redPin, LOW);
        digitalWrite(redPin2, LOW);
      }
      if ( millis() > grnTime)
      {
        digitalWrite(grnPin, HIGH);
        digitalWrite(grnPin2, HIGH);
        grnTime = millis() + shortOn;
        ledState = 3;
      }
      break;

    case 3:
      if (millis() > redTime)
      {
        digitalWrite(redPin, LOW);
        digitalWrite(redPin2, LOW);
      }
      if (millis() > grnTime)
      {
        digitalWrite(grnPin, LOW);
        digitalWrite(grnPin2, LOW);
        grnTime = millis() + longOff;
        if (s == 7)
        {
          ledState = 4;
        }
        else
        {
          ledState = 0;
        }
      }
      break;

    case 4:
      if (millis() > grnTime)
      {
        grnTime = millis() + shortOff;
        digitalWrite(grnPin, HIGH);
        digitalWrite(grnPin2, HIGH);
        digitalWrite(redPin, LOW);
        digitalWrite(redPin2, LOW);
        ledState = 5;
      }
      break;

    case 5:
      if (millis() > grnTime)
      {
        grnTime = millis() + shortOff;
        digitalWrite(grnPin, LOW);
        digitalWrite(grnPin2, LOW);
        digitalWrite(redPin, HIGH);
        digitalWrite(redPin2, HIGH);
        if (s == 7)
        {
          ledState = 4;
        }
        else
        {
          ledState = 3;
        }
      }
      break;   
    }  // led switch

  if (millis() > muxTime)
  {
    temp = (float)analogRead(0) * 0.488;
    adc[chan] = (int)temp;
    //Serial.println(adc[chan]);
    if (++chan > 5) 
    {
      chan = 0;
    }
    mux(chan);
    muxTime = millis() + 50;
  }
