#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <NewPing.h>
// Define LCD I2C address
LiquidCrystal_I2C lcd(0x27, 16, 2); // Adjust address if necessary
// Define ultrasonic sensor pins and NewPing setup
#define TRIGGER_PIN_1 2
#define ECHO_PIN_1 3
#define TRIGGER_PIN_2 4
#define ECHO_PIN_2 5
#define TRIGGER_PIN_3 6
#define ECHO_PIN_3 7
#define MAX_DISTANCE 200 // Maximum distance in cm
NewPing sonar1(TRIGGER_PIN_1, ECHO_PIN_1, MAX_DISTANCE);
NewPing sonar2(TRIGGER_PIN_2, ECHO_PIN_2, MAX_DISTANCE);
NewPing sonar3(TRIGGER_PIN_3, ECHO_PIN_3, MAX_DISTANCE);
// Define servo motor pin
const int servoPin = 9;
// Create Servo object
Servo myservo;
// Define threshold distance (in cm) to detect a vehicle
const int thresholdDistance = 30; // Adjust this value based on your setup
// Define number of parking slots
const int numSlots = 3;
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Parking Status:");
myservo.attach(servoPin);
myservo.write(0); // Set servo to initial position (0 degrees)
}
void loop() {
int availableSlots = 0;
int availableSlotIndex = -1; // Store index of the first available slot
// Measure distances using NewPing library
unsigned int distance1 = sonar1.ping_cm();
unsigned int distance2 = sonar2.ping_cm();
unsigned int distance3 = sonar3.ping_cm();
// Check each sensor and count available slots
if (distance1 > thresholdDistance) {
availableSlots++;
availableSlotIndex = 0;
}
if (distance2 > thresholdDistance) {
availableSlots++;
if (availableSlotIndex == -1) availableSlotIndex = 1;
}
if (distance3 > thresholdDistance) {
availableSlots++;
if (availableSlotIndex == -1) availableSlotIndex = 2;
}
// Control servo and LCD display
if (availableSlots > 0) {
myservo.write(90); // Move servo to 90 degrees
lcd.setCursor(0, 1);
lcd.print("Available: Slot ");
lcd.print(availableSlotIndex + 1);
} else {
myservo.write(0); // Keep servo at 0 degrees if no slots are available
lcd.setCursor(0, 1);
lcd.print("No slots available");
}
Serial.print("Available Slots: ");
Serial.println(availableSlots);
delay(200);
}