#define LDR_PIN A0
#define lampu   2


// LDR Characteristics
const float GAMMA = 0.7;
const float RL10 = 50;
int stat,count;


void setup() 
{
  Serial.begin(115200);

  pinMode(LDR_PIN,  INPUT);
  pinMode(lampu,    OUTPUT);

  digitalWrite(lampu, 1);
  delay(500);
  digitalWrite(lampu, 0);
  delay(500);
}

void loop() 
{
  int analogValue = analogRead(A0);
  float voltage = analogValue / 1024. * 5;
  float resistance = 2000 * voltage / (1 - voltage / 5);
  float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));

  if(stat <= 10)
  {
    if(lux >= 0.1 && lux <= 0.9)
    {
      count = count + 1;
      Serial.print("Malam : ");
      Serial.print(lux);
      Serial.print(" : ");
      Serial.println(stat);
    }

    if(lux >= 1 && lux <= 9)
    {
      count = count + 2;
      Serial.print("Senja : ");
      Serial.println(lux);
    }

    if(lux >= 10 && lux <= 999)
    {
      count = count + 3;
      Serial.print("Mendung : ");
      Serial.println(lux);
    }

    if(lux >= 1000 && lux <= 9999)
    {
      count = count + 4;
      Serial.print("Cerah : ");
      Serial.println(lux);
    }

    if(lux >= 10000)
    {
      count = count + 5;
      Serial.print("Sangat Cerah : ");
      Serial.println(lux);
    }
    stat = stat + 1;
  }

  if(stat >= 10)
  {
    count = count / 10;

    if(count <= 3)
    {
      digitalWrite(lampu, 1);
    }
    if(count >= 4)
    {
      digitalWrite(lampu, 0);
    }

    Serial.println("--------------------------");
    Serial.print("status Cuaca : ");
    if(count == 1)
    {
      Serial.println("Malam");
    }
    if(count == 2)
    {
      Serial.println("Senja");
    }
    if(count == 3)
    {
      Serial.println("Mendung");
    }
    if(count == 4)
    {
      Serial.println("Cerah");
    }
    if(count == 5)
    {
      Serial.println("Sangat Cerah");
    }
    Serial.println("--------------------------");
    count = 0;
    stat  = 0;
  }
  
  delay(1000);
}