#include <LiquidCrystal.h>
#include <Servo.h>
#define pingPin 28
#define echoPin 27
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
Servo daServo;
void setup() {
pinMode(pingPin, OUTPUT);
pinMode(echoPin, INPUT);
daServo.attach(4);
lcd.begin(16, 2);
}
void loop() {
long duration, cm; // Allocate vars
// send 10 us Pulse to Trig
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);
// We get the distance with the echo time
duration = pulseIn(echoPin, HIGH);
cm = microsecondsToCentimeters(duration);
bool check = cm <= 100;
daServo.write(90 * check + 90); // Check to lock door
if (check) {
lcd.setCursor(0, 0);
lcd.print("Oh Hi!");
} else {
lcd.setCursor(0, 0);
lcd.print("No one");
}
delay(1); // Adding a delay() here speeds up the simulation
}
// Distance Formula
long microsecondsToCentimeters(long microseconds) {
return microseconds / 29 / 2;
}