#include <LiquidCrystal.h>
#include <Servo.h>
#include <EEPROM.h>
// Display setup
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
Servo myServo;
uint8_t trigPin = A2;
uint8_t echoPin = A1;
uint8_t servoPin = A0;
int distance;
int threshold = EEPROM.read(0); // reads the 0 index threshold
int duration;
bool open = false;
void setup() {
lcdstart();
myServo.attach(servoPin);
myServo.write(0);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600);
thresholdsetup();
delay(1000);
}
void loop() {
digitalWrite(trigPin, LOW); //Ultrasonic trigger pin set up
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); //Echo pin set up
distance = (duration * 0.0343) / 2; // conversion of time to distance
lcd.clear();
if (distance > 0) // prints distance in real time
lcd.print("Distance:" + String(distance) + " cm");
if (distance <= 0)// distance only 0 when nothing is sensed, no value should be displayed
lcd.print("Distance: N/A");
lcd.setCursor(0, 1);
if (distance <= threshold) {
lcd.print("Open");
if (open == false) {
open = true;
myServo.write(180);// Move the servo to 180 degrees
delay(1000);//time to let motor "open"
}
}
else {
myServo.write(0);
lcd.setCursor(0, 1);
lcd.print("Closed");
open = false;
}
if (Serial.available() > 0) { //checks for any input from the serial monitor
threshold = Serial.readStringUntil('\n').toInt();// \n is the delimiter
EEPROM.write(0, threshold);
Serial.print("Threshold value updated to: " + String(threshold)+ "cm\n");
}
delay(150);
}
void lcdstart() {
lcd.begin(16,2); //16 by 2 screen
lcd.print("Hello World"); // displays in an intro message
lcd.setCursor(0,1);//moves Cursor to second row
lcd.print("Thanks Anthony");//what ever second line your want
delay(400); //ms of delay
lcd.setCursor(0,1);//moves Cursor to second row
lcd.print(" ");
lcd.setCursor(0,1);//moves Cursor to second row
lcd.print("Purple Inigma");
delay(700);
lcd.setCursor(0,1);//moves Cursor to second row
lcd.print("I'm Self aware!");
delay(1000);
lcd.setCursor(0,1);//moves Cursor to second row
lcd.print("Restarting......");
delay(500);
lcd.clear();
lcd.print(" ");
delay(500);
lcd.setCursor(1,0);
lcd.print("Hello World : )");
delay(1000);
lcd.clear();
}
void thresholdsetup() {
Serial.print("Current threshold distance is " + String(threshold) + "cm\n");
Serial.print("Enter desired threshold activation distance in centimeters\n");
Serial.print("Interger numbers only, type in the value and hit enter\n");
}