#include <Wire.h> //use with i2c
#include <LiquidCrystal_I2C.h>
#include <Servo.h>

LiquidCrystal_I2C lcd(0x27, 16, 2); //lcd_i2c
Servo myservo; //servo

const int btn_mode = 8; //yellow; change mode
const int btn_time = 9; //green; change time only manually
const int btn_emer = 11; //red; emergency mode with led blinked; 
const int buzzer = 7;
const int LDR1 = A0;
const int LDR2 = A1;
const int LDR3 = A2;
const int LDR4 = A3;
const int led_day1 = 24;
const int led_day2 = 26;
const int led_night1 = 25;
const int led_night2 = 27;
const int led_emer = 35;
int light_val_ave = 0;  //for LDR sensors
int light_val1, light_val2, light_val3, light_val4;
int i = 0;
int day = true;
int mode = 0;
//char Incoming_value = 0; ***Bluetooth component that doesn't support in the simulator

void setup() { 
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Lightning System");
  lcd.setCursor(2, 3);
  lcd.print("Launching...");
  delay(2000);

  pinMode(btn_mode, INPUT_PULLUP);
  pinMode(btn_time, INPUT_PULLUP);
  pinMode(btn_emer, INPUT_PULLUP);
  pinMode(buzzer, OUTPUT);
  pinMode(led_day1, OUTPUT);
  pinMode(led_day2, OUTPUT);
  pinMode(led_night1, OUTPUT);
  pinMode(led_night2, OUTPUT);
  pinMode(led_emer, OUTPUT);

  lcd.clear();
  lcd.setCursor(1, 0);
  lcd.print("Automatic");

  myservo.attach(2); //set servo to pin 
  myservo.write(0);
  delay(1000);

}


void modee(int m) {
  lcd.setCursor(1, 0);
  lcd.print("                ");
  lcd.setCursor(1, 0);

  
  if (mode == 3) {
    mode = 0;
  }

  if (mode == 0) {
    lcd.print("Automatic");
  } else if (mode == 1) {
    lcd.print("Manual");
  } else if (mode == 2) {
    lcd.print("Bluetooth");
  }
  
  
}

void check_time() {
  lcd.setCursor(2, 3);
  if (day == true){
    lcd.print("Day  ");
  } else if (day == false) {
    lcd.print("Night");
  }
}
void Emergency(){
  lcd.setCursor(1,0);
  lcd.print("Emergency");
  lcd.setCursor(2,3);
  lcd.print("                ");
  for (i=0; i<10; i++) {
    digitalWrite(led_emer, HIGH);
    tone(buzzer,250,100);
    delay(1000);
    digitalWrite(led_emer, LOW);
    delay(100);
  }

}


void loop() {
  //LDR
  light_val1 = analogRead(LDR1);
  light_val2 = analogRead(LDR2);
  light_val3 = analogRead(LDR3);
  light_val4 = analogRead(LDR4);
  light_val_ave = (light_val1 + light_val2 + light_val3 + light_val4) / 4;
  
  if (digitalRead(btn_emer) == LOW){
      Emergency();
      check_time();
      modee(mode);
      while(digitalRead(btn_emer) == LOW);
      delay(300);
        
  }

  else if (digitalRead(btn_mode) == LOW) {
    mode++;
    modee(mode);
    while(digitalRead(btn_mode) == LOW);
    delay(500);
  } else if (digitalRead(btn_mode) == HIGH) {
    check_time(); //check time before run if condition
    if (mode == 0) {

      if (light_val_ave >= 500) {
        day = true;
      } else {
        day = false;
      }

    } else if (mode == 1) {
        
      if (digitalRead(btn_time) == LOW) {
        //
        lcd.setCursor(2, 3);
        if (day == true){
          day = false;
        } else if (day == false) {
          day = true;
        }

        while(digitalRead(btn_time) == LOW);
        delay(300);

        
      }
    } else if (mode == 2) {
      //bluetooth ** don't have on simulator
      /*
      if (Serial.available() > 0) {
        Incoming_value = Serial.read();
        if (Incoming_value = "1") {
          day = true;
        } else if (Incoming_value = "2"){
          day = false;
        } else if (Incoming_value = "3") {
          Emergency();
          check_time();
          modee(mode);
          while(digitalRead(btn_emer) == LOW);
          delay(300);
        }

      }
      */
    }
      
  }

  //servo part, led
  if (day == true) {
    myservo.write(40);
    digitalWrite(led_day1, HIGH);
    digitalWrite(led_day2, HIGH);
    digitalWrite(led_night1, LOW);
    digitalWrite(led_night2, LOW);
  } else if (day == false) {
    myservo.write(90);
    digitalWrite(led_day1, LOW);
    digitalWrite(led_day2, LOW);
    digitalWrite(led_night1, HIGH);
    digitalWrite(led_night2, HIGH);
  }


}