/*
Wokwi | questions
Please help me

Ferdyano — 11/13/24 at 9:12 AM
*/

#include <LiquidCrystal_I2C.h>

const int THRESHOLD = 2048;
const int LED_PIN = 15;
const int POT_PIN = 34;

int oldPotValue = 0;

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  Serial.begin(115200);
  lcd.init();
  lcd.backlight();
  pinMode(LED_PIN, OUTPUT);
  Serial.println("\nSystem ready\n");
  lcd.print("Demo ready");
}

void loop() {
  delay(10); // this speeds up the simulation
  char buffer[16];

  int potValue = analogRead(POT_PIN);
  bool ledState = (potValue >= THRESHOLD) ? true : false;
  digitalWrite(LED_PIN, ledState);
  if (potValue != oldPotValue)  {
    oldPotValue = potValue;
    lcd.setCursor(0, 0);
    snprintf(buffer, 16, "Pot value: %4d", potValue);
    lcd.print(buffer);
  }
  lcd.setCursor(0, 1);
  lcd.print("LED is ");
  lcd.setCursor(7, 1);
  lcd.print(ledState ? "ON " : "OFF");
}