 
 void synchronization()
 {
+  digitalWrite(redLED, HIGH);
+  delay(initDelay);
+  digitalWrite(redLED, LOW);
+  delay(initDelay);
+
+  digitalWrite(greenLED, HIGH);
+  delay(initDelay);
+  digitalWrite(greenLED, LOW);
+  delay(initDelay);
+
+  digitalWrite(blueLED, HIGH);
+  delay(initDelay);
+  digitalWrite(blueLED, LOW);
+  delay(initDelay);
+
+  digitalWrite(redLED, HIGH);
+  digitalWrite(greenLED, HIGH);
+  delay(initDelay);
+  digitalWrite(redLED, LOW);
+  digitalWrite(greenLED, LOW);
+  delay(initDelay);
+
+  digitalWrite(redLED, HIGH);
+  digitalWrite(blueLED, HIGH);
+  delay(initDelay);
+  digitalWrite(redLED, LOW);
+  digitalWrite(blueLED, LOW);
+  delay(initDelay);
+
+  digitalWrite(greenLED, HIGH);
+  digitalWrite(blueLED, HIGH);
+  delay(initDelay);
+  digitalWrite(greenLED, LOW);
+  digitalWrite(blueLED, LOW);
+  delay(initDelay);
+
+  digitalWrite(redLED, HIGH);
+  digitalWrite(greenLED, HIGH);
+  digitalWrite(blueLED, HIGH);
+  delay(initDelay);
+  digitalWrite(redLED, LOW);
+  digitalWrite(greenLED, LOW);
+  digitalWrite(blueLED, LOW);
+}
+
+void flashDotOrDash(int color, bool dotOrDash)
+{
+  Serial.print(dotOrDash); //for testing ******************************************
+  if (dotOrDash)
+    digitalWrite(color, HIGH);
+  else
+    digitalWrite(color, LOW);
+}
+
+void ledControl(int led)
+{
+  if (led == 0)
+  {
+    digitalWrite(redLED, LOW);
+    digitalWrite(greenLED, LOW);
+    digitalWrite(blueLED, LOW);
+  }
+  else if (led == 1)
+  {
     digitalWrite(redLED, HIGH);
-    delay(initDelay);
+    digitalWrite(greenLED, LOW);
+    digitalWrite(blueLED, LOW);
+  }
+  else if (led == 2)
+  {
     digitalWrite(redLED, LOW);
-    delay(initDelay);
-    
     digitalWrite(greenLED, HIGH);
-    delay(initDelay);
+    digitalWrite(blueLED, LOW);
+  }
+  else if (led == 3)
+  {
+    digitalWrite(redLED, LOW);
     digitalWrite(greenLED, LOW);
-    delay(initDelay);
-    
     digitalWrite(blueLED, HIGH);
-    delay(initDelay);
-    digitalWrite(blueLED, LOW);
-    delay(initDelay);
-    
+  }
+  else if (led == 4)
+  {
     digitalWrite(redLED, HIGH);
     digitalWrite(greenLED, HIGH);
-    delay(initDelay);
+    digitalWrite(blueLED, LOW);
+  }
+  else if (led == 5)
+  {
+    digitalWrite(redLED, HIGH);
+    digitalWrite(greenLED, LOW);
+    digitalWrite(blueLED, HIGH);
+  }
+  else if (led == 6)
+  {
     digitalWrite(redLED, LOW);
-    digitalWrite(greenLED, LOW);
-    delay(initDelay); 
-       
-    digitalWrite(redLED, HIGH);
-    digitalWrite(blueLED, HIGH);
-    delay(initDelay);
-    digitalWrite(redLED, LOW);
-    digitalWrite(blueLED, LOW);
-    delay(initDelay); 
-       
     digitalWrite(greenLED, HIGH);
     digitalWrite(blueLED, HIGH);
-    delay(initDelay);
-    digitalWrite(greenLED, LOW);
-    digitalWrite(blueLED, LOW);
-    delay(initDelay);
-    
+  }
+  else if (led == 7)
+  {
     digitalWrite(redLED, HIGH);
     digitalWrite(greenLED, HIGH);
     digitalWrite(blueLED, HIGH);
-    delay(initDelay);
-    digitalWrite(redLED, LOW);
-    digitalWrite(greenLED, LOW);
-    digitalWrite(blueLED, LOW);   
-}
-
-void flashDotOrDash(int color, bool dotOrDash)
-{
-    Serial.print(dotOrDash); //for testing ******************************************
-    if(dotOrDash)
-        digitalWrite(color, HIGH);
-    else
-        digitalWrite(color, LOW);
-}
-
-void ledControl(int led)
-{
-  if(led == 0)
-  {
-    digitalWrite(redLED, LOW);
-    digitalWrite(greenLED, LOW);
-    digitalWrite(blueLED, LOW);
-  }
-  else if(led == 1)
-  {
-    digitalWrite(redLED, HIGH);
-    digitalWrite(greenLED, LOW);
-    digitalWrite(blueLED, LOW);
-  }
-  else if(led == 2)
-  {
-    digitalWrite(redLED, LOW);
-    digitalWrite(greenLED, HIGH);
-    digitalWrite(blueLED, LOW);
-  }
-  else if(led == 3)
-  {
-    digitalWrite(redLED, LOW);
-    digitalWrite(greenLED, LOW);
-    digitalWrite(blueLED, HIGH);
-  }
-  else if(led == 4)
-  {
-    digitalWrite(redLED, HIGH);
-    digitalWrite(greenLED, HIGH);
-    digitalWrite(blueLED, LOW);
-  }
-  else if(led == 5)
-  {
-    digitalWrite(redLED, HIGH);
-    digitalWrite(greenLED, LOW);
-    digitalWrite(blueLED, HIGH);
-  }
-  else if(led == 6)
-  {
-    digitalWrite(redLED, LOW);
-    digitalWrite(greenLED, HIGH);
-    digitalWrite(blueLED, HIGH);
-  }
-  else if(led == 7)
-  {
-    digitalWrite(redLED, HIGH);
-    digitalWrite(greenLED, HIGH);
-    digitalWrite(blueLED, HIGH);
-  }
-  else   
+  }
+  else
     Serial.println("Please enter a number between 0 and 7!!");
 }
 

    }
    else
    {
      th_Val[2].r_t = ((float) r_h * thErrOFF); 
      th_Val[2].g_t = ((float) g_h * thErrON);
      th_Val[2].b_t = ((float) b_h * thErrOFF);
      break;
    }
  }
  while(g > th_Val[0].g_t) //wait until green is LOW
  {
    tcs.getRawData(&r, &g, &b, &c); 
  }
  Serial.print(".");
  //00B
   while(b < th_Val[0].b_t)
  {
    tcs.getRawData(&r, &g, &b, &c); //wait until blue is HIGH
  }
  delay(setupWaitTime); 
  //set threshold for blue high
  r_h = g_h = b_h = 0;
  while(true)
  {
    tcs.getRawData(&r, &g, &b, &c); //get env info
    if(b > b_h)
    {
      r_h = r;
      g_h = g;

    }
    else
    {
      th_Val[3].r_t = ((float) r_h * thErrOFF);
      th_Val[3].g_t = ((float) g_h * thErrOFF);
      th_Val[3].b_t = ((float) b_h * thErrON);
      break;
    }
  }
  while (b > th_Val[0].b_t)
  {
    tcs.getRawData(&r, &g, &b, &c); //wait until blue is LOW
  }
  Serial.print(".");
  //RG0
  while ((r < th_Val[0].r_t) | (g < th_Val[0].g_t)) //wait until red and green is HIGH
  {
    tcs.getRawData(&r, &g, &b, &c);
  }
  delay(setupWaitTime);
  //set threshold for red-green high
  r_h = g_h = b_h = 0;
  while (true)
  {
    tcs.getRawData(&r, &g, &b, &c); //get env info
    if (r > r_h)
    {
      r_h = r;
      if (b_h < b)
      {
        b_h = b;
      }
    }
    else if (g > g_h)
    {
      g_h = g;
      if (b_h < b)
      {
        b_h = b;
      }
    }
    else
    {
      th_Val[4].r_t = ((float) r_h * thErrON);
      th_Val[4].g_t = ((float) g_h * thErrON);
      th_Val[4].b_t = ((float) b_h * thErrOFF);
      break;
    }
  }
  while ((r > th_Val[0].r_t) | (g > th_Val[0].g_t)) //wait until red and green is LOW
  {
    tcs.getRawData(&r, &g, &b, &c);
  }
  Serial.print(".");
  //R0B
  while ((r < th_Val[0].r_t) | (b < th_Val[0].b_t)) //wait until red and blue is HIGH
  {
    tcs.getRawData(&r, &g, &b, &c);
  }
  delay(setupWaitTime);
  //set threshold for red-blue high
  r_h = g_h = b_h = 0;
  while (true)
  {
    tcs.getRawData(&r, &g, &b, &c); //get env info
    if (r > r_h)
    {
      r_h = r;
      if (g_h < g)
      {
        g_h = g;
      }
    }
    else if (b > b_h)
    {
      b_h = b;
      if (g_h < g)
      {
        g_h = g;
      }
    }
    else
    {
      th_Val[5].r_t = ((float) r_h * thErrON);
      th_Val[5].g_t = ((float) g_h * thErrOFF);
      th_Val[5].b_t = ((float) b_h * thErrON);
      break;
    }
  }

  while ((r > th_Val[0].r_t) | (b > th_Val[0].b_t)) //wait until red and blue is LOW
  {
    tcs.getRawData(&r, &g, &b, &c);
  }
  Serial.print(".");
  //0GB
  while ((g < th_Val[0].g_t) | (b < th_Val[0].b_t)) //wait until green and blue is HIGH
  {
    tcs.getRawData(&r, &g, &b, &c);
  }
  delay(setupWaitTime);
  //set threshold for green-blue high
  r_h = g_h = b_h = 0;
  while (true)
  {
    tcs.getRawData(&r, &g, &b, &c); //get env info
    if (b > b_h)
    {
      b_h = b;
      if (r_h < r)
      {
        r_h = r;
      }
    }
    else if (g > g_h)
    {
      g_h = g;
      if (r_h < r)
      {
        r_h = r;
      }
    }
    else
    {
      th_Val[6].r_t = ((float) r_h * thErrOFF);
      th_Val[6].g_t = ((float) g_h * thErrON);
      th_Val[6].b_t = ((float) b_h * thErrON);
      break;
    }
  }
  while ((g > th_Val[0].g_t) | (b > th_Val[0].b_t)) //wait until green and blue is LOW
  {
    tcs.getRawData(&r, &g, &b, &c);
  }
  Serial.print(".");
  //RGB
  while ((g < th_Val[0].g_t) | (b < th_Val[0].b_t) | (r < th_Val[0].r_t)) //wait until red, green and blue is HIGH
  {
    tcs.getRawData(&r, &g, &b, &c);
  }
  delay(setupWaitTime);
  //set threshold for red-green-blue high
  r_h = g_h = b_h = 0;
  while (true)
  {
    tcs.getRawData(&r, &g, &b, &c); //get env info
    if (r > r_h)
    {
      r_h = r;
    }
    else if (g > g_h)
    {
      g_h = g;
    }
    else if (b_h < b)
    {
      b_h = b;
    }
    else
    {
      th_Val[7].r_t = ((float) r_h * thErrON);
      th_Val[7].g_t = ((float) g_h * thErrON);
      th_Val[7].b_t = ((float) b_h * thErrON);
      break;
    }
  }

  while ((g > th_Val[0].g_t) | (b > th_Val[0].b_t) | (r > th_Val[0].r_t)) //wait until red, green and blue is HIGH
  {
    tcs.getRawData(&r, &g, &b, &c);
  }
  Serial.println("Done");
  //print env values
  for (int j = 0; j < 8; j++)
  {
    Serial.print("Env ");
    Serial.print(j);
