#include <LiquidCrystal_I2C.h>

#define B1 3
#define B2 4
#define B3 5

#define led1 11
#define led2 10
#define led3 9

#define Tekan LOW

int pindah; 
unsigned char p , n , v ;

LiquidCrystal_I2C lcd (0x27 ,20 ,4);

void setup() {

lcd.init();
lcd.backlight();

pinMode(B1 , INPUT_PULLUP);
pinMode(B2 , INPUT_PULLUP);
pinMode(B3 , INPUT_PULLUP);

pinMode(led1 , OUTPUT);
pinMode(led2 , OUTPUT);
pinMode(led3 , OUTPUT);

lcd.setCursor(3,0);
lcd.print("TH Memilih Led");
lcd.setCursor(2,1);
lcd.print("TM Kecerahan Led");
lcd.setCursor(1,2);
lcd.print("TB Mengurangi Led");
delay (3000);
lcd.clear();
}

void loop() {

analogWrite(led1 , p);
analogWrite(led2 , n);
analogWrite(led3 , v);

if (digitalRead(B1) == Tekan){
  pindah = (pindah + 1) %4;
  if (pindah == 0){
  pindah = 1;
    }
  lcd.setCursor(0,0);
  lcd.print("Led = ");
  lcd.print(pindah);
  delay(150);
}

  if (digitalRead(B2) == Tekan){
      if (pindah == 1){
        p++;
        delay(10);
      } else if (pindah == 2){
        n++;
        delay(10);
      } else if (pindah == 3){
        v++;
        delay(10);
      }
    } else if (digitalRead(B3) == Tekan){
      if (pindah == 1){
        p--;
        delay(10);
      } else if (pindah == 2){
        n--;
        delay(10);
      } else if (pindah == 3){
        v--;
        delay(10);
      }
    }

if (pindah == 1){
  lcd.setCursor(0,1);
  lcd.print("Nilai PWM = ");
  lcd.print(p < 10 ? "00" :( p < 100 ? "0" : ""));
  lcd.print(p);
} else if (pindah == 2){
  lcd.setCursor(0,1);
  lcd.print("Nilai PWM = ");
  lcd.print(n < 10 ? "00" :( n < 100 ? "0" : ""));
  lcd.print(n);
} else if (pindah == 3){
  lcd.setCursor(0,1);
  lcd.print("Nilai PWM = ");
  lcd.print(v < 10 ? "00" :( v < 100 ? "0" : ""));
  lcd.print(v);
}
}