#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int PIN_POTENTIOMETER = A0;
const int PIN_LED = 13;
const int PIN_BUZZER = 12;
const int POWER_THRESHOLD = 700;
const int ADC_MAX_VALUE = 1023;
const int LCD_COLUMNS = 16;
const int LCD_ROWS = 2;
const int LCD_I2C_ADDRESS = 0x27;
const int ALERT_TONE_FREQUENCY = 1000;
LiquidCrystal_I2C lcd(LCD_I2C_ADDRESS, LCD_COLUMNS, LCD_ROWS);
enum class PowerState
{
    NORMAL,
    ALERT
};
void setup()
{
    initialize_hardware();
    initialize_lcd();
    initialize_serial();
}
void loop()
{
    int power_value = read_power_value();
    PowerState current_state = determine_state(power_value);
    perform_state_action(current_state, power_value);
    print_debug_info(power_value, current_state);
    delay(100);
}
void initialize_hardware()
{
    pinMode(PIN_LED, OUTPUT);
    pinMode(PIN_POTENTIOMETER, INPUT);
    digitalWrite(PIN_LED, LOW);
    noTone(PIN_BUZZER);
}
void initialize_lcd()
{
    lcd.init();
    lcd.backlight();
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Power Monitor");
    lcd.setCursor(0, 1);
    lcd.print("System Ready");
    delay(2000);
    lcd.clear();
}
void initialize_serial()
{
    Serial.begin(9600);
    Serial.println("=== Power Alert System ===");
    Serial.println("System Initialized");
    Serial.println();
}
int read_power_value()
{
    int raw_value = analogRead(PIN_POTENTIOMETER);
    return raw_value;
}
PowerState determine_state(int power_value)
{
    if (power_value >= POWER_THRESHOLD)
    {
        return PowerState::ALERT;
    }
    else
    {
        return PowerState::NORMAL;
    }
}
void perform_state_action(PowerState state, int power_value)
{
    switch (state)
    {
    case PowerState::NORMAL:
        handle_normal_state(power_value);
        break;
    case PowerState::ALERT:
        handle_alert_state(power_value);
        break;
    }
}
void handle_normal_state(int power_value)
{
    digitalWrite(PIN_LED, LOW);
    noTone(PIN_BUZZER);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Power Level:");
    lcd.setCursor(0, 1);
    lcd.print(power_value);
    lcd.print(" / ");
    lcd.print(ADC_MAX_VALUE);
}
void handle_alert_state(int power_value)
{
    digitalWrite(PIN_LED, HIGH);
    tone(PIN_BUZZER, ALERT_TONE_FREQUENCY);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Power Over");
    lcd.setCursor(0, 1);
    lcd.print("Alert! ");
    lcd.print(power_value);
}
void print_debug_info(int power_value, PowerState state)
{
    static unsigned long last_print_time = 0;
    const unsigned long print_interval = 1000;
    unsigned long current_time = millis();
    if (current_time - last_print_time >= print_interval)
    {
        Serial.print("Power Value: ");
        Serial.print(power_value);
        Serial.print(" | Status: ");
        if (state == PowerState::ALERT)
        {
            Serial.println("ALERT");
        }
        else
        {
            Serial.println("NORMAL");
        }
        last_print_time = current_time;
    }
}