// constants won't change. They're used here to set pin numbers:
const int buttonPin = 11;     // the number of the pushbutton pin
const int ledPinH =  4;
const int ledPinM =  3;
const int ledPinL =  2;

// variables will change:
int buttonState = LOW;         // variable for reading the pushbutton status

#include <Servo.h>
#include <Wire.h>
#include "Adafruit_TCS34725.h"

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

/* Initialise with default values (int time = 2.4ms, gain = 1x) */
// Adafruit_TCS34725 tcs = Adafruit_TCS34725();
 
/* Initialise with specific int time and gain values */
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_600MS, TCS34725_GAIN_1X);

void setup() {
  myservo.attach(12);  // attaches the servo on pin 12 to the servo object
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);
  pinMode(ledPinH, OUTPUT);
  pinMode(ledPinM, OUTPUT);
  pinMode(ledPinL, OUTPUT);
  if (tcs.begin()) {
  Serial.println("Found sensor");
  } else {
  Serial.println("No TCS34725 found ... check your connections");
  while (1);
  }
}


void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
      myservo.write(pos);              // tell servo to go to position in variable 'pos'
      delay(15);                       // waits 15 ms for the servo to reach the position
    }
    delay(2000);
    uint16_t r, g, b, c, colorTemp, lux;
   
    tcs.getRawData(&r, &g, &b, &c);
    colorTemp = tcs.calculateColorTemperature(r, g, b);
    lux = tcs.calculateLux(r, g, b);
     
    Serial.print("Color Temp: "); Serial.print(colorTemp, DEC); Serial.print(" K - ");
    Serial.print("Lux: "); Serial.print(lux, DEC); Serial.print(" - ");
    Serial.print("R: "); Serial.print(r, DEC); Serial.print(" ");
    Serial.print("G: "); Serial.print(g, DEC); Serial.print(" ");
    Serial.print("B: "); Serial.print(b, DEC); Serial.print(" ");
    Serial.print("C: "); Serial.print(c, DEC); Serial.print(" ");
    Serial.println(" ");
    Serial.print(int(colorTemp));
    delay(1000);
    if (int(colorTemp) < 5300){
      digitalWrite(ledPinL, HIGH);
      delay(4000);
      digitalWrite(ledPinL, LOW);
    }
    if ((int(colorTemp) > 5299) and (int(colorTemp) < 8000)){
      digitalWrite(ledPinM, HIGH);
      delay(4000);
      digitalWrite(ledPinM, LOW);
      }
    if (int(colorTemp) > 7999){
      digitalWrite(ledPinH, HIGH);
      delay(4000);
      digitalWrite(ledPinH, LOW);
    }
    for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
      myservo.write(pos);              // tell servo to go to position in variable 'pos'
      delay(15);                       // waits 15 ms for the servo to reach the position
    }
  }
}
