#include <Ultrasonic.h>
Ultrasonic ultrasonic(12, 13); // Trigger pin = 12, Echo pin = 13
void setup() {
pinMode(8, OUTPUT); // Red light
pinMode(9, OUTPUT); // Green light
Serial.begin(9600); // Optional: Start the Serial monitor for debugging
}
void loop() {
int distance = ultrasonic.read(); // Read distance from ultrasonic sensor
// Debugging: Print the distance to the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
// Control lights based on distance
if (distance < 50) {
digitalWrite(8, HIGH); // Turn on red light for emergency vehicle
digitalWrite(9, LOW); // Turn off green light
} else {
digitalWrite(9, HIGH); // Normal green light
digitalWrite(8, LOW); // Turn off red light
}
delay(200); // Small delay to stabilize the sensor reading and avoid flickering
}