//Ebenezer Mawufemor tefe 1703021230
//Sylvia Kwabea Owusu 1704135871
#include <Servo.h>
#include <DistanceSensor.h>
#include <LCD-I2C.h>
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
int timer1 = 20;
int timer2 = 100;
const int echoPin = 10;
const int trigPin = 9;
#define red 5
#define green 4
int thresholdDistance = 30;
int servoPos;
LCD_I2C lcd(0x27, 16, 2); // Default address of most PCF8574 modules, change according
DistanceSensor sensor(trigPin, echoPin);
void setup() {
myservo.attach(6); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
lcd.begin();
lcd.display();
lcd.backlight();
lcd.setCursor(3, 0);
lcd.print("Reception");
myservo.write(0);
}
void loop() {
int distance = sensor.getCM();
Serial.print("Distance: ");
Serial.print(distance);
Serial.println("cm");
//delay(1000);
// Check the distance and move the servo incrementally
if (distance <= thresholdDistance && servoPos < 90) {
// Move servo incrementally to 90 degrees
servoPos += 3;
myservo.write(servoPos);
lcd.setCursor(0, 1);
delay(2); // Small delay to smooth the movement
} else if (distance > thresholdDistance && servoPos > 0) {
// Move servo incrementally back to 0 degrees
servoPos -= 3;
myservo.write(servoPos);
delay(2); // Small delay to smooth the movement
}
// Update LEDs based on servo position
if (servoPos == 0) { // Door closed (servo at 0 degrees)
digitalWrite(red, HIGH);
digitalWrite(green, LOW);
lcd.setCursor(5, 1);
lcd.print("Closed");
} else if (servoPos == 90) { // Door open (servo at 90 degrees)
digitalWrite(red, LOW);
digitalWrite(green, HIGH);
lcd.setCursor(6, 1);
lcd.print("Open");
} else { // Intermediate positions
digitalWrite(red, LOW);
digitalWrite(green, LOW);
lcd.setCursor(0, 1);
lcd.print(" ");
}
delay(100);
}