#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <EEPROM.h>

LiquidCrystal_I2C lcd(0x27, 20, 4); // I2C LCD address 2004

const int LDR_PIN[] = {A0, A1, A2, A3};
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

int calibrationValue = 500;
char password[] = "1234";
bool calibrationCompleted = false;
int currentMenu = 0;
bool stopDisplayingValues = false;

void setup() {
  Serial.begin(9600); // Activate Serial Monitor
  lcd.init();
  lcd.backlight();
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("A. Measurement");
  lcd.setCursor(0, 1);
  lcd.print("C. Calibration");
  lcd.setCursor(0, 2);
  lcd.print("D. Detail");
  lcd.setCursor(0, 3);
  lcd.print("B. Back");
  EEPROM.get(0, calibrationValue);
}

float measureLight(int pin) {
  int sensorValue = analogRead(pin);
  float voltage = (sensorValue / 1023.0) * 5.0;
  float resistance = (5.0 - voltage) / voltage;
  return calibrationValue / resistance *100; // Lamp Luminous Intensity in Cd
}

void displayLightValues() {
  float luxValues[4];

  for (int i = 0; i < 4; i++) {
    luxValues[i] = measureLight(LDR_PIN[i]);
    Serial.print("Sensor ");
    Serial.print(i + 1);
    Serial.print(": ");
    Serial.print(luxValues[i]);
    Serial.println(" Cd");
  }

  float avgLux = (luxValues[0] + luxValues[1] + luxValues[2] + luxValues[3]) / 4.0;
  String beamInfo;
  String deviationInfo;

  if (luxValues[3] > avgLux) {
    beamInfo = "High Beam";
  } else {
    beamInfo = "Low Beam";
  }

  float deviationInMM = abs(luxValues[1] - luxValues[3])/ 1000.0; // Conversion to mm per 1000 lux;

  // Jari-jari dalam mm (1 meter = 1000 mm)
  float radiusMM = 1000.0;

  // Konversi deviasi dalam mm menjadi sudut dalam derajat
  float deviationInDegrees = (deviationInMM / radiusMM) * (180.0 / PI);

  if (luxValues[1] > luxValues[3]) {
    deviationInfo = "Right : " + String(deviationInDegrees, 2) + "°";
  } else if (luxValues[3] > luxValues[1]) {
    deviationInfo = "Left  : " + String(deviationInDegrees, 2) + "°";
  } else {
    deviationInfo = "No Deviation";
  }

  // Display the information
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("bCal : BRIGHTNESS");
  lcd.setCursor(0, 1);
  lcd.print("Cd");
  lcd.setCursor(8, 1);
  lcd.print(luxValues[0]  , 0);
  lcd.setCursor(0, 2);
  lcd.print(beamInfo);
  lcd.setCursor(0, 3);
  lcd.print(deviationInfo);
}


void calibrateSensor() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Enter the password:");
  lcd.setCursor(0, 1);

  char enteredPassword[5];
  int passwordIndex = 0;

  while (1) {
    char key = keypad.getKey();

    if (key) {
      if (key == '#') {
        enteredPassword[passwordIndex] = '\0';
        if (strcmp(enteredPassword, password) == 0) {
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("Calibration: ");
          lcd.setCursor(0, 1);
          lcd.print("Value: ");
          lcd.setCursor(8, 1);
          lcd.print(calibrationValue);

          char buffer[5];
          int bufferIndex = 0;

          while (1) {
            key = keypad.getKey();

            if (key) {
              if (key == '#') {
                calibrationValue = atoi(buffer);
                EEPROM.put(0, calibrationValue);
                lcd.clear();
                lcd.setCursor(0, 0);
                lcd.print("Value Saved: ");
                lcd.setCursor(0, 1);
                lcd.print("Value: ");
                lcd.setCursor(8, 1);
                lcd.print(calibrationValue);
                delay(2000);
                calibrationCompleted = true;
                return;
              } else if (key == '*') {
                bufferIndex = 0;
              } else {
                buffer[bufferIndex++] = key;
                lcd.setCursor(8 + bufferIndex - 1, 1);
                lcd.print(key);
              }
            }
          }
        } else {
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("Wrong Password!");
          delay(2000);
          return;
        }
      } else {
        enteredPassword[passwordIndex++] = key;
        lcd.print("*");
      }
    }
  }
}

void displayDetailValues() {
  lcd.clear();
  stopDisplayingValues = false;

  while (!stopDisplayingValues) {
    for (int i = 0; i < 4; i++) {
      lcd.setCursor(0, i);
      lcd.print("Sensor ");
      lcd.print(i + 1);
      lcd.setCursor(9, i);

      float lux = measureLight(LDR_PIN[i]);
      Serial.print("Sensor ");
      Serial.print(i + 1);
      Serial.print(": ");
      Serial.print(lux);
      Serial.println(" Cd");

      lcd.print(lux, 0);
    }

    delay(1000); // Wait for a while before continuing to the next measurement
    lcd.clear();

    // Check the 'B' button to stop the loop
    char key = keypad.getKey();
    if (key == 'B') {
      stopDisplayingValues = true;
    }
  }
}

void loop() {
  char key = keypad.getKey();
  if (key) {
    switch (key) {
      case 'A':
        currentMenu = 1;
        displayMenu(currentMenu);
        break;
      case 'C':
        currentMenu = 2;
        displayMenu(currentMenu);
        break;
      case 'D':
        currentMenu = 3;
        displayMenu(currentMenu);
        break;
      case 'B':
        currentMenu = 0;
        displayMenu(currentMenu);
        break;
    }
  }
}

void displayMenu(int menu) {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Menu:");
  switch (menu) {
    case 0:
      lcd.setCursor(0, 1);
      lcd.print("1. Measurement     A");
      lcd.setCursor(0, 2);
      lcd.print("2. Calibration     C");
      lcd.setCursor(0, 3);
      lcd.print("3. Detail          D");
      stopDisplayingValues = false; // Reset the flag to continue displayDetailValues
      break;
    case 1:
      displayLightValues();
      break;
    case 2:
      calibrateSensor();
      break;
    case 3:
      displayDetailValues();
      break;
  }
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
ldr1:VCC
ldr1:GND
ldr1:DO
ldr1:AO
ldr2:VCC
ldr2:GND
ldr2:DO
ldr2:AO
ldr3:VCC
ldr3:GND
ldr3:DO
ldr3:AO
ldr4:VCC
ldr4:GND
ldr4:DO
ldr4:AO
keypad1:R1
keypad1:R2
keypad1:R3
keypad1:R4
keypad1:C1
keypad1:C2
keypad1:C3
keypad1:C4
lcd1:GND
lcd1:VCC
lcd1:SDA
lcd1:SCL