#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Inisialisasi objek LCD
LiquidCrystal_I2C lcd(0x27, 16, 2); // Alamat I2C untuk LCD 16x2
// Inisialisasi pin untuk LED dan Buzzer
const int ledKuningPin = 17;
const int ledBiruPin = 16;
const int ledMerahPin = 2;
const int buzzerPin = 32;
const int potensiometerPin = 4;
void setup() {
// Set up pin-mode
pinMode(ledKuningPin, OUTPUT);
pinMode(ledBiruPin, OUTPUT);
pinMode(ledMerahPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
// Inisialisasi LCD
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("PWR = 0%");
}
void loop() {
int potValue = analogRead(potensiometerPin); // Baca nilai potensiometer
int power = map(potValue, 0, 4095, 0, 100); // Mapping nilai potensiometer ke rentang 0-100
// Tampilkan nilai power di LCD
lcd.setCursor(6, 0);
lcd.print(" "); // Membersihkan tampilan sebelum menampilkan nilai baru
lcd.setCursor(6, 0);
lcd.print(power);
lcd.print("% ");
// Mengatur LED dan buzzer sesuai dengan kondisi power
if (power >= 0 && power <= 30) {
analogWrite(ledKuningPin, map(power, 0, 30, 0, 255)); // LED kuning
digitalWrite(ledBiruPin, LOW);
digitalWrite(ledMerahPin, LOW);
tone(buzzerPin, map(power, 0, 30, 0, 1000)); // Buzzer volume
lcd.setCursor(0, 1);
lcd.print("LED Kuning Hidup ");
} else if (power > 30 && power <= 70) {
digitalWrite(ledKuningPin, LOW);
analogWrite(ledBiruPin, map(power, 30, 70, 0, 255)); // LED biru
digitalWrite(ledMerahPin, LOW);
noTone(buzzerPin);
lcd.setCursor(0, 1);
lcd.print("LED Biru Hidup ");
} else if (power > 70 && power <= 100) {
digitalWrite(ledKuningPin, LOW);
digitalWrite(ledBiruPin, LOW);
analogWrite(ledMerahPin, map(power, 70, 100, 0, 255)); // LED merah
noTone(buzzerPin);
lcd.setCursor(0, 1);
lcd.print("LED Merah Hidup ");
}
delay(100); // Delay untuk stabilisasi
}