#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
// Initialize the LCD, address 0x27 for a 16x2 LCD screen
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo myservo; // create servo object to control a servo
// Constants for ultrasonic sensor
const int trigPin = 9; // Trigger pin for ultrasonic sensor
const int echoPin = 10; // Echo pin for ultrasonic sensor
const int servoPin = 6; // Servo motor pin
const int threshold = 50; // Threshold distance in cm
// Variables for ultrasonic sensor
long duration;
int distance;
// Variables for lenght of scrolltext
int lcdWidth = 16; // Width of the LCD (number of characters)
// Servo motor position variable
int val;
// LCD scrolling variables
String Doorlock = "Door is Closed";
String Dooropen = "Door is Opened";
String initMessage = "Ebakoh Ugbalo Project";
String scrollMessage;
int scrollIndex = 0;
int scrollDelay = 250; // Delay for scroll speed
void setup() {
// Start serial communication
Serial.begin(9600);
// Set pin modes for ultrasonic sensor
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Attach servo to pin
myservo.attach(servoPin);
// Initialize the LCD
lcd.begin(16, 2); // 16 columns, 2 rows
scrollText(initMessage, 0);
delay(1000);
lcd.clear(); // Clear the display
lcd.print("Device Ready");
delay(1000);
lcd.clear();
Serial.println("System initialized. Waiting for an object...");
delay(2000); // Wait 2 seconds before starting measurements
// Seed the random generator with an arbitrary analog input
randomSeed(analogRead(1));
}
void loop() {
// Automatically trigger ultrasonic sensor at random angles every 1000ms
for (int i = 0; i < 180; i++) {
distance = random(0, 101);
// Generate a random servo angle between 0 and 180
int randomAngle = random(0, 181);
myservo.write(randomAngle); // Set the servo to the random position
// Send ultrasonic pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the time it takes for the echo to be received
duration = pulseIn(echoPin, HIGH);
// Calculate the distance based on the speed of sound
//distance = duration * 0.034 / 2; // Divide by 2 for round-trip time
// Print the distance to the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Update LCD display
displayOnLCD(distance, val);
// Check if the distance is below the threshold
if (distance < threshold) {
// Map the distance to a servo angle between 0 and 180
val = map(distance, 0, threshold, 0, 180);
myservo.write(val); // Set the servo position
Serial.print("Servo angle: ");
Serial.println(val);
//lcd.setCursor(0, 1); // Set the cursor to the beginning of the second row
scrollText(Dooropen, 1); // Print "Door Open" with extra spaces to clear row
} else {
// If no object is close enough, reset the servo to 0 position
myservo.write(0);
Serial.println("No object detected, servo reset to 0.");
// Clear the second row if no object is detected
//lcd.setCursor(0, 1); // Set the cursor to the beginning of the second row
scrollText(Doorlock, 1); // Clear the second row by printing empty spaces
}
delay(500); // Wait for 1000 milliseconds before the next reading
}
}
// Function to display distance and servo angle on the LCD
void displayOnLCD(int distance, int servoAngle) {
lcd.clear(); // Clear the display
// Create the message to scroll
scrollMessage = "Distance: " + String(distance) + " cm Servo: " + String(servoAngle) + " degree";
// Display the scrolling message
scrollText(scrollMessage, 0);
}
// Function to scroll text from right to left on the LCD
void scrollText(String message, int row) {
// Add spaces to the message to clear the display
String scrollMessage = " " + message + " ";
for (int i = 0; i <= scrollMessage.length() - lcdWidth; i++) {
lcd.setCursor(0, row);
lcd.print(scrollMessage.substring(i, i + lcdWidth));
delay(250); // Adjust delay to change the scrolling speed
}
}