   }
   delay(setupWaitTime); 
   //set threshold for red-green-blue high
-  th_Val[7].r_t = ((float) r * thErrON); 
-  th_Val[7].g_t = ((float) g * thErrON);
-  th_Val[7].b_t = ((float) b * thErrON);
+     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.print("Getting threshold values");
  Serial.print(".");
  //set threshold for red HIGH
  th_Val[1].r_t = ((float) r * thErrON); 
  th_Val[1].g_t = ((float) g * thErrOFF);
  th_Val[1].b_t = ((float) b * thErrOFF);
  while(r > th_Val[0].r_t) //wait until red is LOW
  {
    tcs.getRawData(&r, &g, &b, &c); //get env info

  }
  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
