#include <LiquidCrystal_I2C.h>

const int lcdAddress = 0x27; // Set your LCD I2C address here
const int lcdRows = 2;
const int lcdColumns = 16;
const int ledPin = 13; // LED pin
const int inputPin = 2; // Change to your input pin
const int pHSensorPin = A0; // Analog input pin for pH sensor

LiquidCrystal_I2C lcd(lcdAddress, lcdColumns, lcdRows);

byte emptyBar[8] = {B00000, B00000, B00000, B00000, B00000, B00000, B00000, B00000};
byte fullBar[8] = {B11111, B11111, B11111, B11111, B11111, B11111, B11111, B11111};

int systemLoad = 0; // Replace with your function to get system load
bool loadingComplete = false;

void setup() {
  pinMode(inputPin, INPUT);
  pinMode(ledPin, OUTPUT); // LED pin as output
  lcd.init();
  lcd.createChar(0, emptyBar);
  lcd.createChar(1, fullBar);
  lcd.backlight();
}

void updateSystemLoad() {
  // Simulate system load incrementing from 0 to 100
  systemLoad += 1;
  if (systemLoad > 100) {
    systemLoad = 0;
    loadingComplete = true;
  }
}

void displayGauge() {
  float gaugeResolution = (float)lcdColumns / 100.0;
  int pixelsToFill = round(systemLoad * gaugeResolution);

  lcd.setCursor(0, 0);
  lcd.print("Load:");
  lcd.print(systemLoad);
  lcd.print("% ");

  lcd.setCursor(0, 1);
  for (int i = 0; i < lcdColumns; i++) {
    lcd.write((i < pixelsToFill) ? 1 : 0);
  }
}

void loop() {
  if (!loadingComplete) {
    updateSystemLoad(); // Update system load
    displayGauge();     // Display the gauge on the LCD
  } else {
    int pHValue = analogRead(pHSensorPin); // Read pH sensor value
    float pH = (float)pHValue * 5.0 / 1024.0; // Convert to pH value
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("pH Value:");
    lcd.print(pH, 2);
    lcd.setCursor(0, 1);
    if (digitalRead(inputPin) == HIGH) {
      lcd.print("Sensor Connected");

    } else {
      lcd.print("Error: No Sensor");
    }
  }

  // Blink LED every 1000 milliseconds (1 second)
  digitalWrite(ledPin, HIGH);
 delay (10);
}