#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
const int LDR_PIN = 4; // Analog input pin for the LDR
const int LED_PIN = 14; // Digital output pin for the LED
const int SERVO_PIN = 2; // Digital output pin for the servo
int ldrValue = 0; // Variable to store the LDR value
Servo s; // Create a Servo object
const int SERVO_OPEN_ANGLE = 0; // Servo angle when open
const int SERVO_CLOSED_ANGLE = 90; // Servo angle when closed
LiquidCrystal_I2C lcd(0x27, 16, 2); // LCD object (Update the address if needed)
void setup() {
Serial.begin(115200); // Initialize serial communication
pinMode(LDR_PIN, INPUT); // Set the LDR pin as input
pinMode(LED_PIN, OUTPUT); // Set the LED pin as output
s.attach(SERVO_PIN); // Attach the servo to the specified pin
s.write(SERVO_CLOSED_ANGLE); // Initialize the servo to the closed position
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
lcd.clear(); // Clear the LCD display
lcd.setCursor(0, 0); // Set the cursor to the first row, first column
lcd.print("Receiver"); // Print "Receiver" on the first row
}
void loop() {
ldrValue = analogRead(LDR_PIN); // Read the LDR value
Serial.print("LDR Value: ");
Serial.println(ldrValue);
const int LDR_THRESHOLD = 2000; // Example threshold value
if (ldrValue > LDR_THRESHOLD) {
digitalWrite(LED_PIN, HIGH); // Turn on the LED
s.write(SERVO_OPEN_ANGLE); // Open the servo
lcd.setCursor(0, 1); // Set the cursor to the second row, first column
lcd.print("Servo: Open "); // Print "Servo: Open" on the second row
} else {
digitalWrite(LED_PIN, LOW); // Turn off the LED
s.write(SERVO_CLOSED_ANGLE); // Close the servo
lcd.setCursor(0, 1); // Set the cursor to the second row, first column
lcd.print("Servo: Closed "); // Print "Servo: Closed" on the second row
}
delay(100); // Add a small delay to prevent constant triggering
}