#include <LiquidCrystal.h>
#include <Servo.h>
#include <EEPROM.h>
// initialize the library with the numbers of the interface pins
Servo myepicservo;
const int echoPin = 49;
const int trigPin = 51;
int targAngle=0;
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
float duration;
float cmeters; // distance in centimeters
int opendist;
bool domove = true; // queues a move
bool open = true; // reflects state of door
String tempS = " "; // temp string to take in serial data
void setup() {
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
myepicservo.attach(53);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Start Serial monitor
Serial.begin(9600);
// get value from EEPROM
opendist = EEPROM.read(0);
if (opendist == 0xFFFF){ // if EEPROM empty fill with default value
opendist = 100;
}
}
void loop() {
Serial.println(opendist);
if (Serial.available() > 0){
tempS = Serial.readStringUntil('\n');
if (opendist != tempS.toInt()){ // update if value differs
opendist = tempS.toInt();
if (EEPROM.read(0) != opendist){ // puts value in EEPROM
EEPROM.update(0, opendist);
}
}
}
delayMicroseconds(38); //gap time
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); //pulse time
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); //listen
cmeters = (0.0343 * duration)/2; // (Speed x Time) / 2
lcd.setCursor(0,1);
lcd.print(String(cmeters) + "cm ");
//checks if a move is avalible
if (open == true && cmeters > opendist){
domove = true;
open = false;
}
if (open == false && cmeters <= opendist){
domove = true;
open = true;
}
// moves door if move is avalible
if (domove == true){
lcd.setCursor(0,0);
if (open == true){ // Open door
lcd.print("Door Open ");
myepicservo.write(180);
}
if (open == false){ // Close door
lcd.print("Door Closed ");
myepicservo.write(0);
}
domove = false; // set move to false
}
}