   Serial.print(sync);
   Serial.println(" to send threshold sequence");
   uint16_t r_h, g_h, b_h;
-  setupMain: //come back here untill sync seq start
+setupMain: //come back here untill sync seq start
   if (Serial.available() > 0) //if there is an input data
   {
     inputHOLD = Serial.readString(); //get input
 
-    if(inputHOLD == sync) //check for sync code
-      {
-        synchronization(); //if true sync
-        goto setupMain; //then go back
-       }
+    if (inputHOLD == sync) //check for sync code
+    {
+      synchronization(); //if true sync
+      goto setupMain; //then go back
+    }
   }
   tcs.getRawData(&r, &g, &b, &c); //get env info
   //R00
-  if(r < th_Val[0].r_t) //go back if red is lower then OFF threshold
+  if (r < th_Val[0].r_t) //go back if red is lower then OFF threshold
   {
     goto setupMain;
   }


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);
    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   
    Serial.println("Please enter a number between 0 and 7!!");
}


  Serial.println("Starting...");
}

void loop()
{
  short writtenCH = 0; //to count chars before new line
  bool err = 0;
  tcs.getRawData(&r, &g, &b, &c);
  if (g > th_Val[2].g_t)
  {
listenSTR:
    //send start bacon
    digitalWrite(greenLED, HIGH);
    delay(fbTime);
    digitalWrite(greenLED, LOW);
    delay(cycTime);

    tcs.getRawData(&r, &g, &b, &c);

    detect_rgb(r, g, b, th_Val, buff[0], buff[1], buff[2]);

    digitalWrite(blueLED, HIGH);
    delay(fbTime);
    digitalWrite(blueLED, LOW);
    delay(cycTime);

    tcs.getRawData(&r, &g, &b, &c);

    detect_rgb(r, g, b, th_Val, buff[3], buff[4], buff[5]);

    digitalWrite(blueLED, HIGH);
    delay(fbTime);
    digitalWrite(blueLED, LOW);
    delay(cycTime);

    tcs.getRawData(&r, &g, &b, &c);

    detect_rgb(r, g, b, th_Val, buff[6], buff[7], buff[8]);

    digitalWrite(blueLED, HIGH);
    delay(fbTime);
    digitalWrite(blueLED, LOW);
    delay(cycTime);

    tcs.getRawData(&r, &g, &b, &c);

    detect_rgb(r, g, b, th_Val, buff[9], buff[10], buff[11]);

    checkParity(buff[2], buff[4], buff[5], buff[6], buff[8], buff[9], buff[10], buff[11], buff[0], buff[1], buff[3], buff[7], err);

    ch = decodeASCII(buff[2], buff[4], buff[5], buff[6], buff[8], buff[9], buff[10], buff[11]);

    if (ch != NULL)
    {
      digitalWrite(redLED, HIGH);
      delay(redONtime);
      Serial.print(ch);
      writtenCH++;
      if (writtenCH == 80)
      {
        Serial.print("\n");
        writtenCH = 0;
      }
      digitalWrite(redLED, LOW);
      delay(redOFFtime);
      if (ch != '\n')
        goto listenSTR;
    }
  }



  if (Serial.available() > 0)
  {
    inputHOLD = Serial.readString();

    if (inputHOLD == sync)
      synchronization();
    else if (inputHOLD == crdM)
    {
      Serial.println("In alphabetic order:\nOnur Calik\nMusab Cevik\nBaran Dereli\nOzgun Sensoy\nYigit Suoglu");
    }
    else if (inputHOLD == ledContol)
    {
      Serial.print("Enter led code: ");
      while (true)
      {
        if (Serial.available() > 0)
        {
          inputHOLD = Serial.readString();
          if (inputHOLD == "quit")
          {
            break;
          }
          short j = (int) inputHOLD[0] - 48;
          ledControl(j);
        }
      }
    }
    else if (inputHOLD == recal) //start recalabration seq. wait for synchronization seq.
    {
      Serial.print("Getting enviroment values"); //get env
      tcs.getRawData(&r, &g, &b, &c); //store environment values
      th_Val[0].r_t = ((float) r * thErrOFF);
      th_Val[0].g_t = ((float) g * thErrOFF);
      th_Val[0].b_t = ((float) b * thErrOFF);
      Serial.print("\nWaiting for threshold sequence\n");
      uint16_t r_h, g_h, b_h;
setupMain: //come back here untill sync seq start
      if (Serial.available() > 0) //if there is an input data
      {
        inputHOLD = Serial.readString(); //get input

        if (inputHOLD == sync) //check for sync code
        {
          synchronization(); //if true sync
          goto setupMain; //then go back
        }
      }
      tcs.getRawData(&r, &g, &b, &c); //get env info
      //R00
      if (r < th_Val[0].r_t) //go back if red is lower then OFF threshold
      {
        goto setupMain;
      }
      delay(setupWaitTime);
      Serial.print("Getting threshold values");
      Serial.print(".");
      //set threshold for red 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;
          g_h = g;
          b_h = b;
        }
        else
        {
          th_Val[1].r_t = ((float) r_h * thErrON);
          th_Val[1].g_t = ((float) g_h * thErrOFF);
          th_Val[1].b_t = ((float) b_h * thErrOFF);
          break;
        }
      }

      while (r > th_Val[0].r_t) //wait until red is LOW
      {
        tcs.getRawData(&r, &g, &b, &c); //get env info
      }
      Serial.print(".");
      //0G0
      while (g < th_Val[0].g_t)
      {
        tcs.getRawData(&r, &g, &b, &c); //wait until green is HIGH
      }
      delay(setupWaitTime);
      //set threshold for green high
      r_h = g_h = b_h = 0;
      while (true)
      {
        tcs.getRawData(&r, &g, &b, &c); //get env info
        if (g > g_h)
        {
          r_h = r;
          g_h = g;
          b_h = b;
        }
        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;
          b_h = b;
        }
        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);
        Serial.print(" R: ");
        Serial.print(th_Val[j].r_t);
        Serial.print(" G: ");
        Serial.print(th_Val[j].g_t);
        Serial.print(" B: ");
        Serial.println(th_Val[j].b_t);
      }
      Serial.println("Ready...");
    }
    else
    {
      digitalWrite(greenLED, HIGH);

      //Serial.print("\n");
      for (int j = 0; j < inputHOLD.length(); ++j)
      {
        flashSequence(inputHOLD[j]);
      }
      flashSequence('\n');
      Serial.print("\n");
    }
    digitalWrite(redLED, LOW);
    digitalWrite(greenLED, LOW);
