#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
// Pin Definitions
#define TRIG_PIN 9
#define ECHO_PIN 8
#define BUZZER_PIN 10
#define GREEN_LED 5
#define RED_LED 6
// Variables
long duration; // Variable to store the duration of the pulse
float distance; // Distance in "m" (assume cm from sensor equals m for display)
Servo gateServo; // Servo for the gate
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
// Pin configuration
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(RED_LED, OUTPUT);
// Servo initialization
gateServo.attach(11); // Attach servo to Pin 11
gateServo.write(0); // Gate starts open (0°)
// LCD initialization
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Runway Safety");
lcd.setCursor(0, 1);
lcd.print("System Ready!");
delay(2000);
lcd.clear();
}
void loop() {
distance = measureDistance();
if (distance <= 100) {
alertMode(); // If an obstacle is detected
} else {
clearMode(); // If the runway is clear
}
delay(100);
}
float measureDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH); // Measure the time for echo
float cm = duration * 0.034 / 2; // Convert to cm
return cm; // Return distance in cm, treated as m for display
}
void alertMode() {
// Buzzer beeps repeatedly
for (int i = 0; i < 3; i++) { // 3 beeps
digitalWrite(BUZZER_PIN, HIGH);
delay(200); // Sound for 200ms
digitalWrite(BUZZER_PIN, LOW);
delay(200); // Pause for 200ms
}
digitalWrite(RED_LED, HIGH); // Red LED turns on
digitalWrite(GREEN_LED, LOW); // Green LED turns off
gateServo.write(90); // Close the gate (90°)
lcd.setCursor(0, 0);
lcd.print("ObstacleDetected");
lcd.setCursor(0, 1);
lcd.print("Distance: ");
lcd.print((int)distance); // Treat cm as m and display as an integer
lcd.print("m");
}
void clearMode() {
digitalWrite(BUZZER_PIN, LOW); // Buzzer stops sounding
digitalWrite(RED_LED, LOW); // Red LED turns off
digitalWrite(GREEN_LED, HIGH); // Green LED turns on
gateServo.write(0); // Open the gate (0°)
lcd.setCursor(0, 0);
lcd.print("Runway Clear ");
lcd.setCursor(0, 1);
lcd.print("Distance: ");
lcd.print((int)distance); // Treat cm as m and display as an integer
lcd.print("m");
}