#include <Wire.h> // Library for I2C communication
#include <LiquidCrystal_I2C.h> // Library for LCD
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2); // Change to (0x27,20,4) for 20x4 LCD.
const int potPin = 14; // Pin untuk potensiometer
const int ledPins[] = {15,2,19,4,16,17,23,18}; // Pin untuk 8 LED
int potValue = 0;
int previousPotValue = 0;
int ledState = 0;
void setup() {
// Inisialisasi komunikasi serial
Serial.begin(115200);
// Inisialisasi pin LED sebagai output
for (int i = 0; i < 8; i++) {
pinMode(ledPins[i], OUTPUT);
digitalWrite(ledPins[i], LOW); // Matikan semua LED pada awal
}
lcd.init();
lcd.init();
lcd.backlight();
lcd.setCursor(1,0);
lcd.print("David Ramadian");
}
void loop() {
potValue = analogRead(potPin) / 40.96; // Baca nilai potensiometer dan skala dari 0-4095 ke 0-100
Serial.println(potValue); // Cetak nilai potensiometer
// Saat potensiometer dinaikkan
if (potValue > previousPotValue) {
if (potValue >= 11 && potValue <= 20) ledState = 1;
else if (potValue >= 21 && potValue <= 30) ledState = 2;
else if (potValue >= 31 && potValue <= 40) ledState = 3;
else if (potValue >= 41 && potValue <= 50) ledState = 4;
else if (potValue >= 51 && potValue <= 60) ledState = 5;
else if (potValue >= 61 && potValue <= 70) ledState = 6;
else if (potValue >= 71 && potValue <= 80) ledState = 7;
else if (potValue >= 81 && potValue <= 90) ledState = 8;
if(ledState==1){
lcd.setCursor(0,1);
lcd.print(" led 1 on ");
}
if(ledState==2){
lcd.setCursor(0,1);
lcd.print(" led 2 on ");
}
if(ledState==3){
lcd.setCursor(0,1);
lcd.print(" led 3 on ");
}
if(ledState==4){
lcd.setCursor(0,1);
lcd.print(" led 4 on ");
}
if(ledState==5){
lcd.setCursor(0,1);
lcd.print(" led 5 on ");
}
if(ledState==6){
lcd.setCursor(0,1);
lcd.print(" led 6 on ");
}
if(ledState==7){
lcd.setCursor(0,1);
lcd.print(" led 7 on ");
}
if(ledState==8){
lcd.setCursor(0,1);
lcd.print(" led 8 on ");
}
}
// Saat potensiometer diturunkan
else if (potValue < previousPotValue) {
if (potValue >= 11 && potValue <= 20) ledState = 0;
else if (potValue >= 21 && potValue <= 30) ledState = 1;
else if (potValue >= 31 && potValue <= 40) ledState = 2;
else if (potValue >= 41 && potValue <= 50) ledState = 3;
else if (potValue >= 51 && potValue <= 60) ledState = 4;
else if (potValue >= 61 && potValue <= 70) ledState = 5;
else if (potValue >= 71 && potValue <= 80) ledState = 6;
else if (potValue <= 10) ledState = 0;
if(ledState==0){
lcd.setCursor(0,1);
lcd.print(" led 1 off ");
}
if(ledState==1){
lcd.setCursor(0,1);
lcd.print(" led 2 off ");
}
if(ledState==2){
lcd.setCursor(0,1);
lcd.print(" led 3 off ");
}
if(ledState==3){
lcd.setCursor(0,1);
lcd.print(" led 4 off ");
}
if(ledState==4){
lcd.setCursor(0,1);
lcd.print(" led 5 off ");
}
if(ledState==5){
lcd.setCursor(0,1);
lcd.print(" led 6 off ");
}
if(ledState==6){
lcd.setCursor(0,1);
lcd.print(" led 7 off ");
}
if(ledState==7){
lcd.setCursor(0,1);
lcd.print(" led 8 off ");
}
}
// Mengatur status LED berdasarkan ledState
for (int i = 0; i < 8; i++) {
if (i < ledState) {
digitalWrite(ledPins[i], HIGH);
} else {
digitalWrite(ledPins[i], LOW);
}
}
// Simpan nilai potensiometer sebelumnya
previousPotValue = potValue;
delay(100); // Delay untuk stabilisasi pembacaan potensiometer
}