const int trigPins[] = {2, 4, 6}; // Array of Trig pins for ultrasonic sensors
const int echoPins[] = {3, 5, 7}; // Array of Echo pins for ultrasonic sensors
const int ledPins[] = {8, 9, 10}; // Array of LED pins
void setup() {
Serial.begin(9600);
for (int i = 0; i < 3; i++) {
pinMode(trigPins[i], OUTPUT);
pinMode(echoPins[i], INPUT);
pinMode(ledPins[i], OUTPUT);
}
}
void loop() {
for (int i = 0; i < 3; i++) {
// Trigger the ultrasonic sensor
digitalWrite(trigPins[i], LOW);
delayMicroseconds(2);
digitalWrite(trigPins[i], HIGH);
delayMicroseconds(10);
digitalWrite(trigPins[i], LOW);
// Read the distance
long duration = pulseIn(echoPins[i], HIGH);
int distance = duration * 0.034 / 2;
// Adjust LED based on distance
if (distance > 100) {
// If the object is far, blink the LED
blinkLED(ledPins[i]);
} else {
// If the object is near, turn on the LED solid
digitalWrite(ledPins[i], HIGH);
}
// Print distance to serial monitor
Serial.print("Sensor ");
Serial.print(i + 1);
Serial.print(" - Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(1000); // Adjust the delay as needed
}
}
void blinkLED(int pin) {
// Blink the LED by turning it on and off quickly
digitalWrite(pin, HIGH);
delay(200);
digitalWrite(pin, LOW);
delay(200);
}