#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int ledPin = 25; // 16 corresponds to GPIO16
// setting PWM properties
const int freq = 1000;
const int ledChannel = 0;
const int resolution = 8;
int LDR_Val = 0;
int sensor = 34;
int led = 25;
void keliphidup(){
for(int dutyCycle = 0; dutyCycle <= 255; dutyCycle++){
// changing the LED brightness with PWM
ledcWrite(ledChannel, dutyCycle);
delay(10);
}
}
void kelipmati(){
// decrease the LED brightness
for(int dutyCycle = 255; dutyCycle >= 0; dutyCycle--){
// changing the LED brightness with PWM
ledcWrite(ledChannel, dutyCycle);
delay(10);
}
}
void setup() {
lcd.init(); // initialize the lcd
lcd.backlight();
pinMode(led, OUTPUT);
ledcSetup(ledChannel, freq, resolution);
// attach the channel to the GPIO to be controlled
ledcAttachPin(ledPin, ledChannel);
}
void loop() {
LDR_Val = analogRead(sensor);
Serial.print("LDR Output Value: ");
Serial.println(LDR_Val);
if(LDR_Val > 100) {
lcd.clear();
lcd.setCursor(0, 0); // start to print at the first row
lcd.print("LDR: ");
lcd.print(LDR_Val);
lcd.setCursor(0, 1);
lcd.print(" Intensiti Tinggi ");
keliphidup();
delay(15);
kelipmati();
}
else {
lcd.clear();
lcd.setCursor(0, 0); // start to print at the first row
lcd.print("LDR: ");
lcd.print(LDR_Val);
lcd.setCursor(0, 1);
lcd.println("Intensiti Rendah ");
kelipmati();
}
delay(15);
}