//Muhammad Haziq Bin Rozman 52224121174
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
LiquidCrystal_I2C lcd(0x27,16,2);
Servo servoBase;
int distanceThreshold = 0;
int cm = 0;
int inches = 0;
int cm1 = 0;
int inches1 = 0;
int counter = 0;
long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
}
long readUltrasonicDistance1(int triggerPin1, int echoPin1)
{
pinMode(triggerPin1, OUTPUT); // Clear the trigger
digitalWrite(triggerPin1, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin1, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin1, LOW);
pinMode(echoPin1, INPUT);
// Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(echoPin1, HIGH);
}
void setup()
{
Serial.begin(9600);
servoBase.attach(8);
servoBase.write(0);
}
void loop()
{
lcd.begin(16, 2);
// calculate the distance to the target in cm
cm = 0.01723 * readUltrasonicDistance(13, 12);
// convert to inches by dividing by 2.54
inches = (cm / 2.54);
Serial.print(cm);
Serial.print("cm, ");
Serial.print(inches);
Serial.println("in");
cm1 = 0.01723 * readUltrasonicDistance1(11, 10);
// convert to inches by dividing by 2.54
inches1 = (cm1 / 2.54);
Serial.print(cm1);
Serial.print("cm, ");
Serial.print(inches1);
Serial.println("in");
delay(100);
if(cm > 333){
lcd.setCursor(0,0);
lcd.print("1A available");
if (counter =1) {
servoBase.write(0);
counter = 0;
}
delay(100);
}
if(cm1 > 333){
lcd.setCursor(0,1);
lcd.print("1B available");
if (counter =1) {
servoBase.write(0);
counter = 0;
}
delay(100);
}
if(cm < 333 && cm1 < 333){
lcd.setCursor(0,0);
lcd.print("Parking Full");
servoBase.write(180);
counter = 1;
delay(100);
}
delay(100);
}