#define BLYNK_TEMPLATE_ID "TMPL6p8QRR97i"
#define BLYNK_TEMPLATE_NAME "ooo"
#define BLYNK_AUTH_TOKEN "q9rk4uf3LgJVybV7ayKcufedIzV5R-w5"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
// These constants should match the photoresistor's "gamma" and "rl10" attributes
const float GAMMA = 0.7;
const float RL10 = 50;
// Convert the analog value into lux value:
int analogValue = analogRead(A0);
float voltage = analogValue / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
// Informasi jaringan WiFi
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// Token Blynk
char auth[] = "q9rk4uf3LgJVybV7ayKcufedIzV5R-w5" ;
// Pin untuk sensor LDR
const int ldrPin = 32;
// Pin untuk servos
const int servoPin1 = 2;
const int servoPin2 = 4;
// Nilai ambang batas untuk mendeteksi cahaya
const int threshold = 500;
// Deklarasi objek untuk LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Deklarasi objek untuk servos
Servo servo1;
Servo servo2;
void setup() {
Serial.begin(115200);
// Inisialisasi LCD
lcd.init ();
lcd.backlight();
// Inisialisasi servos
servo1.attach(servoPin1);
servo2.attach(servoPin2);
// Inisialisasi koneksi WiFi
Blynk.begin(auth, ssid, pass);
// Menampilkan pesan awal pada LCD
lcd.print("System ready");
}
void loop() {
Blynk.run();
// Membaca nilai dari sensor LDR
int ldrValue = analogRead(ldrPin);
// Memeriksa apakah cahaya tidak terdeteksi
if (ldrValue < threshold) {
// Memberi peringatan pada LCD
lcd.clear();
lcd.print("Light not detected");
// Menggerakkan servo 1 dan servo 2
servo1.write(0); // posisi tengah servo 1
servo2.write(180); // posisi tengah servo 2
} else {
// Memberi peringatan pada LCD
lcd.clear();
lcd.print("Light detected");
servo1.write(90); // posisi tengah servo 1
servo2.write(0); // posisi tengah servo 2
}
}
BLYNK_WRITE(V1) {
int servoPosition = param.asInt();
servo1.write(servoPosition);
}
BLYNK_WRITE(V2) {
int servoPosition = param.asInt();
servo2.write(servoPosition);
}