#include <LiquidCrystal.h> // Include the LiquidCrystal library for LCD control
#include <Servo.h> // Include the Servo library for servo motor control
#include <EEPROM.h> // Include the EEPROM library for reading and writing to EEPROM memory
Servo doorServo; // Create a servo motor object
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // Create an LCD object with specified pins
const int trigPin = 2; // Define the trigger pin for the ultrasonic sensor
const int echoPin = 3; // Define the echo pin for the ultrasonic sensor
const int thresholdAdjustPin = A0; // Define the analog pin for threshold adjustment
const int servoPin = 10; // Define the pin for the servo motor
int thresholdDistance = 30; // Initialize threshold distance variable
int doorState = 0; // Initialize door state variable
int getDistance() { // Function to measure distance using ultrasonic sensor
digitalWrite(trigPin, LOW); // Set trigger pin to low
delayMicroseconds(2); // Wait for stabilization
digitalWrite(trigPin, HIGH); // Generate 10us pulse on trigger pin
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
int duration = pulseIn(echoPin, HIGH); // Measure duration of echo pulse
int distance = duration * 0.034 / 2; // Convert duration to distance in cm
return distance; // Return the calculated distance
}
void openDoor() { // Function to open the door
doorServo.write(180); // Rotate servo motor to open position (180°)
doorState = 1; // Update door state to open
}
void closeDoor() { // Function to close the door
doorServo.write(0); // Rotate servo motor to close position (0°)
doorState = 0; // Update door state to closed
}
void adjustThreshold() { // Function to adjust threshold distance
int thresholdValue = analogRead(thresholdAdjustPin); // Read potentiometer value
thresholdDistance = map(thresholdValue, 0, 1023, 10, 100); // Map potentiometer value to threshold distance range
EEPROM.update(0, thresholdDistance); // Update threshold distance in EEPROM memory
}
void setup() {
doorServo.attach(servoPin); // Attach servo motor to specified pin
lcd.begin(16, 2); // Initialize LCD with 16x2 characters
pinMode(trigPin, OUTPUT); // Set trigger pin as output
pinMode(echoPin, INPUT); // Set echo pin as input
pinMode(servoPin, OUTPUT); // Set servo pin as output
Serial.begin(9600); // Initialize serial communication
// Read threshold distance from EEPROM
int storedThreshold = EEPROM.read(0);
if (storedThreshold >= 10 && storedThreshold <= 100) {
thresholdDistance = storedThreshold;
}
}
void loop() {
int distance = getDistance(); // Get current distance from ultrasonic sensor
lcd.clear(); // Clear the LCD display
lcd.setCursor(0, 0); // Set cursor to first row, first column
lcd.print("Door: "); // Display "Door: " on LCD
lcd.print(doorState == 1 ? "Open " : "Closed"); // Display door state (Open/Closed) on LCD
lcd.setCursor(0, 1); // Set cursor to second row, first column
lcd.print("Distance: "); // Display "Distance: " on LCD
lcd.print(distance); // Display current distance on LCD
lcd.print(" cm"); // Display unit (cm) on LCD
if (distance < thresholdDistance && doorState == 0) { // If object is within threshold distance and door is closed
openDoor(); // Open the door
} else if (distance >= thresholdDistance && doorState == 1) { // If object is beyond threshold distance and door is open
closeDoor(); // Close the door
}
adjustThreshold(); // Adjust threshold distance based on potentiometer
delay(100); // Delay for stability
}