#include <Servo.h>
// Define names for the ultrasonic sensors
const String sensorNames[] = {"A", "B", "C", "D", "E", "F"};
// Define the pin numbers for the ultrasonic sensors
const int trigPins[] = {2, 4, 6, 8, 10, 12};
const int echoPins[] = {3, 5, 7, 9, 11, 13};
// Define the pin numbers for the LEDs
const int ledPins[] = {14, 15, 16, 17, 18, 19};
// Define the pin number for the IR sensor and servo motor
const int irSensorPin = 26;
const int servoPin = 27;
// Create a servo object
Servo gateServo;
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize the ultrasonic sensors and LED pins
for (int i = 0; i < 6; i++) {
pinMode(trigPins[i], OUTPUT);
pinMode(echoPins[i], INPUT);
pinMode(ledPins[i], OUTPUT); // Initialize LED pins
}
// Initialize the IR sensor
pinMode(irSensorPin, INPUT);
// Attach the servo motor to the pin
gateServo.attach(servoPin);
// Set the initial position of the servo motor (closed gate)
gateServo.write(0);
}
void loop() {
bool parkingAvailable = false;
// Check the parking spots
for (int i = 0; i < 6; i++) {
long duration, distance;
// Send a pulse to trigger the ultrasonic sensor
digitalWrite(trigPins[i], LOW);
delayMicroseconds(2);
digitalWrite(trigPins[i], HIGH);
delayMicroseconds(10);
digitalWrite(trigPins[i], LOW);
// Read the pulse duration from the echo pin
duration = pulseIn(echoPins[i], HIGH);
// Calculate the distance in cm
distance = (duration / 2) / 29.1;
// Check if the parking spot is occupied
if (distance < 10) {
// Spot is occupied
digitalWrite(ledPins[i], HIGH); // Turn on corresponding LED
Serial.print("Parking spot ");
Serial.print(sensorNames[i]);
Serial.println(": Occupied");
} else {
// Spot is empty
digitalWrite(ledPins[i], LOW); // Turn off corresponding LED
parkingAvailable = true; // Set to true if any parking spot is empty
Serial.print("Parking spot ");
Serial.print(sensorNames[i]);
Serial.println(": Empty");
}
}
// Check the IR sensor for the gate
int irValue = digitalRead(irSensorPin);
// Control the servo based on the gate status
if (irValue == HIGH && parkingAvailable) {
gateServo.write(90); // Open the gate
Serial.println("Car detected at the gate: Opening gate");
} else {
gateServo.write(0); // Close the gate
Serial.println("No car at the gate or no parking available: Closing gate");
}
// Small delay to stabilize the sensor readings
delay(1000);
}