#define TRIGGER_PIN_1 27 // Define the trigger pin for the first ultrasonic sensor
#define ECHO_PIN_1 26 // Define the echo pin for the first ultrasonic sensor
#define TRIGGER_PIN_2 25 // Define the trigger pin for the second ultrasonic sensor
#define ECHO_PIN_2 33 // Define the echo pin for the second ultrasonic sensor
#define TRIGGER_PIN_3 16 // Define the trigger pin for the third ultrasonic sensor
#define ECHO_PIN_3 17 // Define the echo pin for the third ultrasonic sensor
#define TRIGGER_PIN_4 21 // Define the trigger pin for the fourth ultrasonic sensor
#define ECHO_PIN_4 3 // Define the echo pin for the fourth ultrasonic sensor
#define TRIGGER_PIN_5 1 // Define the trigger pin for the fifth ultrasonic sensor
#define ECHO_PIN_5 22 // Define the echo pin for the fifth ultrasonic sensor
#define BUZZER_PIN 34 // Define the pin for the buzzer
#define RED_LED_PIN_1 13 // Define the pin for the red LED (first sensor)
#define YELLOW_LED_PIN_1 12 // Define the pin for the yellow LED (first sensor)
#define GREEN_LED_PIN_1 14 // Define the pin for the green LED (first sensor)
#define RED_LED_PIN_2 32 // Define the pin for the red LED (second sensor)
#define YELLOW_LED_PIN_2 2 // Define the pin for the yellow LED (second sensor)
#define GREEN_LED_PIN_2 15 // Define the pin for the green LED (second sensor)
#define RED_LED_PIN_3 5 // Define the pin for the red LED (third sensor)
#define YELLOW_LED_PIN_3 18 // Define the pin for the yellow LED (third sensor)
#define GREEN_LED_PIN_3 19 // Define the pin for the green LED (third sensor)
#define RED_LED_PIN_4 35
#define YELLOW_LED_PIN_4 23 // Define the pin for the yellow LED (fourth sensor)
#define GREEN_LED_PIN_4 35
#define RED_LED_PIN_5 34
#define YELLOW_LED_PIN_5 4 // Define the pin for the yellow LED (fifth sensor)
#define GREEN_LED_PIN_5 34
int distance_1, distance_2, distance_3, distance_4, distance_5;
void setup() {
pinMode(TRIGGER_PIN_1, OUTPUT);
pinMode(ECHO_PIN_1, INPUT);
pinMode(TRIGGER_PIN_2, OUTPUT);
pinMode(ECHO_PIN_2, INPUT);
pinMode(TRIGGER_PIN_3, OUTPUT);
pinMode(ECHO_PIN_3, INPUT);
pinMode(TRIGGER_PIN_4, OUTPUT);
pinMode(ECHO_PIN_4, INPUT);
pinMode(TRIGGER_PIN_5, OUTPUT);
pinMode(ECHO_PIN_5, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(RED_LED_PIN_1, OUTPUT);
pinMode(YELLOW_LED_PIN_1, OUTPUT);
pinMode(GREEN_LED_PIN_1, OUTPUT);
pinMode(RED_LED_PIN_2, OUTPUT);
pinMode(YELLOW_LED_PIN_2, OUTPUT);
pinMode(GREEN_LED_PIN_2, OUTPUT);
pinMode(RED_LED_PIN_3, OUTPUT);
pinMode(YELLOW_LED_PIN_3, OUTPUT);
pinMode(GREEN_LED_PIN_3, OUTPUT);
pinMode(RED_LED_PIN_4, OUTPUT);
pinMode(YELLOW_LED_PIN_4, OUTPUT);
pinMode(GREEN_LED_PIN_4, OUTPUT);
pinMode(RED_LED_PIN_5, OUTPUT);
pinMode(YELLOW_LED_PIN_5, OUTPUT);
pinMode(GREEN_LED_PIN_5, OUTPUT);
}
long readUltrasonicDistance(int triggerPin, int echoPin) {
long duration, cm;
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
duration = pulseIn(echoPin, HIGH);
cm = (duration / 2) / 29.1;
return cm;
}
void handleUltrasonicSensor(int distance, int redLedPin, int yellowLedPin, int greenLedPin) {
if (distance < 1) {
noTone(BUZZER_PIN);
digitalWrite(redLedPin, LOW);
digitalWrite(yellowLedPin, LOW);
digitalWrite(greenLedPin, LOW);
} else if (distance < 50) {
noTone(BUZZER_PIN);
digitalWrite(redLedPin, HIGH);
digitalWrite(yellowLedPin, LOW);
digitalWrite(greenLedPin, LOW);
} else if (distance >= 50 && distance <= 200) {
digitalWrite(redLedPin, LOW);
digitalWrite(yellowLedPin, HIGH);
digitalWrite(greenLedPin, LOW);
tone(BUZZER_PIN, 1000);
} else {
noTone(BUZZER_PIN);
digitalWrite(redLedPin, LOW);
digitalWrite(yellowLedPin, LOW);
digitalWrite(greenLedPin, HIGH);
}
}
void loop() {
distance_1 = int(readUltrasonicDistance(TRIGGER_PIN_1, ECHO_PIN_1));
distance_2 = int(readUltrasonicDistance(TRIGGER_PIN_2, ECHO_PIN_2));
distance_3 = int(readUltrasonicDistance(TRIGGER_PIN_3, ECHO_PIN_3));
distance_4 = int(readUltrasonicDistance(TRIGGER_PIN_4, ECHO_PIN_4));
distance_5 = int(readUltrasonicDistance(TRIGGER_PIN_5, ECHO_PIN_5));
handleUltrasonicSensor(distance_1, RED_LED_PIN_1, YELLOW_LED_PIN_1, GREEN_LED_PIN_1);
handleUltrasonicSensor(distance_2, RED_LED_PIN_2, YELLOW_LED_PIN_2, GREEN_LED_PIN_2);
handleUltrasonicSensor(distance_3, RED_LED_PIN_3, YELLOW_LED_PIN_3, GREEN_LED_PIN_3);
handleUltrasonicSensor(distance_4, RED_LED_PIN_4, YELLOW_LED_PIN_4, GREEN_LED_PIN_4);
handleUltrasonicSensor(distance_5, RED_LED_PIN_5, YELLOW_LED_PIN_5, GREEN_LED_PIN_5);
}