// Project 05 - POD LED LCD
// ------------------------
// Nama : Ahmad Zaky
// Kelas : XII RPL B
// Tanggal : 30/07/2024
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Define pins for LEDs and potentiometer
int ledMerah = 2;
int ledKuning = 3;
int ledHijau = 4;
int Potensio = A0;
// Initialize the LCD with I2C address 0x27 and dimensions 16x2
LiquidCrystal_I2C lcd(0x27, 16, 2); // Adjust the address (0x27 or 0x3F) and dimensions (16 columns x 2 rows) as needed
// Variables to hold the last displayed status
String lastStatus = "";
int lastValue = -1;
//---------------------------------------------setup-----------------------------------------
void setup() {
Serial.begin(115200);
pinMode(Potensio, INPUT);
pinMode(ledMerah, OUTPUT);
pinMode(ledKuning, OUTPUT);
pinMode(ledHijau, OUTPUT);
// Initialize the LCD
lcd.begin(16, 2); // Initialize with 16 columns and 2 rows
lcd.backlight(); // Turn on the backlight (if available)
}
//---------------------------------------------loop-----------------------------------------
void loop() {
int value = analogRead(Potensio);
Serial.println(value);
// Determine current status
String currentStatus;
if (value >= 0 && value <= 341) {
digitalWrite(ledMerah, HIGH); // Turn on red LED
digitalWrite(ledKuning, LOW); // Turn off yellow LED
digitalWrite(ledHijau, LOW); // Turn off green LED
currentStatus = "Status: Merah";
} else if (value >= 342 && value <= 682) {
digitalWrite(ledMerah, LOW); // Turn off red LED
digitalWrite(ledKuning, HIGH); // Turn on yellow LED
digitalWrite(ledHijau, LOW); // Turn off green LED
currentStatus = "Status: Kuning";
} else if (value >= 683 && value <= 1023) {
digitalWrite(ledMerah, LOW); // Turn off red LED
digitalWrite(ledKuning, LOW); // Turn off yellow LED
digitalWrite(ledHijau, HIGH); // Turn on green LED
currentStatus = "Status: Hijau";
}
// Only update LCD if status has changed
if (currentStatus != lastStatus || value != lastValue) {
lcd.clear(); // Clear LCD only when updating
lcd.setCursor(0, 0); // Set cursor to first row
lcd.print(currentStatus); // Display status on LCD
lcd.setCursor(0, 1); // Set cursor to second row
lcd.print("Value: ");
lcd.print(value); // Display the potentiometer value
lastStatus = currentStatus; // Update last status
lastValue = value; // Update last value
}
Serial.println(value); // Print the value to Serial Monitor (for debugging)
delay(100); // Delay before the next read
}