#define TRIGGER_PIN_1 15
#define ECHO_PIN_1 4
#define TRIGGER_PIN_2 18 // Choose another available digital pin for the second sensor's trigger
#define ECHO_PIN_2 19 // Choose another available digital pin for the second sensor's echo
#define TRIGGER_PIN_3 13
#define ECHO_PIN_3 12
#define TRIGGER_PIN_4 14
#define ECHO_PIN_4 27
int distance_cm_1;
int distance_cm_2;
int distance_cm_3;
int distance_cm_4;
int led1 = 23;
int led2 = 22;
int led3 = 26;
int led4 = 25;
bool sensorOn1 = false;
bool sensorOn2 = false;
bool sensorOn3 = false;
bool sensorOn4 = false;
int i;
int buzzerPin = 2;
unsigned long previousMicros_1 = 0, previousMicros_2 = 0, previousMicros_3 = 0, previousMicros_4 = 0;
const unsigned long interval_1 = 20000, interval_2 = 20000, interval_3 = 20000, interval_4 = 20000;
void setup() {
Serial.begin(9600);
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(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
// Read sensor 1
unsigned long currentMicros = micros();
if (currentMicros - previousMicros_1 >= interval_1) {
previousMicros_1 = currentMicros;
digitalWrite(TRIGGER_PIN_1, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN_1, LOW);
long duration_1 = pulseIn(ECHO_PIN_1, HIGH);
int distance_cm_1 = duration_1 * 0.034 / 2 + 1;
Serial.print("Distance 1: ");
Serial.print(distance_cm_1);
Serial.println(" cm");
// Control LED 1 based on distance from sensor 1
if (distance_cm_1 <= 200) {
digitalWrite(led1, LOW); // Turn on LED 1
sensorOn1 = true;
} else {
digitalWrite(led1, HIGH); // Turn off LED 1
sensorOn1 = false;
}
}
// Read sensor 2
currentMicros = micros();
if (currentMicros - previousMicros_2 >= interval_2) {
previousMicros_2 = currentMicros;
digitalWrite(TRIGGER_PIN_2, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN_2, LOW);
long duration_2 = pulseIn(ECHO_PIN_2, HIGH);
int distance_cm_2 = duration_2 * 0.034 / 2 + 1;
Serial.print("Distance 2: ");
Serial.print(distance_cm_2);
Serial.println(" cm");
// Control LED 2 based on distance from sensor 2
if (distance_cm_2 <= 200) {
digitalWrite(led2, LOW); // Turn on LED 2
sensorOn2 = true;
} else {
digitalWrite(led2, HIGH); // Turn off LED 2
sensorOn2 = false;
}
}
// Read sensor 3
currentMicros = micros();
if (currentMicros - previousMicros_3 >= interval_3) {
previousMicros_3 = currentMicros;
digitalWrite(TRIGGER_PIN_3, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN_3, LOW);
long duration_3 = pulseIn(ECHO_PIN_3, HIGH);
int distance_cm_3 = duration_3 * 0.034 / 2 + 1;
Serial.print("Distance 3: ");
Serial.print(distance_cm_3);
Serial.println(" cm");
// Control LED 2 based on distance from sensor 2
if (distance_cm_3 <= 200) {
digitalWrite(led3, LOW); // Turn on LED 2
sensorOn3 = true;
} else {
digitalWrite(led3, HIGH); // Turn off LED 2
sensorOn3 = false;
}
}
// Read sensor 4
currentMicros = micros();
if (currentMicros - previousMicros_4 >= interval_4) {
previousMicros_4 = currentMicros;
digitalWrite(TRIGGER_PIN_4, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN_4, LOW);
long duration_4 = pulseIn(ECHO_PIN_4, HIGH);
int distance_cm_4 = duration_4 * 0.034 / 2 + 1;
Serial.print("Distance 4: ");
Serial.print(distance_cm_4);
Serial.println(" cm");
// Control LED 2 based on distance from sensor 2
if (distance_cm_4 <= 200) {
digitalWrite(led4, LOW); // Turn on LED 2
sensorOn4 = true;
} else {
digitalWrite(led4, HIGH); // Turn off LED 2
sensorOn4 = false;
}
}
if (sensorOn1 == true && sensorOn2 == true) {
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
noTone(buzzerPin); // Stop the buzzer tone
delay(500);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
tone(buzzerPin, 1000);
delay(500);
} else {
noTone(buzzerPin); // Stop the buzzer tone
}
if (sensorOn3 == true && sensorOn4 == true) {
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
noTone(buzzerPin); // Stop the buzzer tone
delay(500);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
tone(buzzerPin, 1000);
delay(500);
} else {
noTone(buzzerPin); // Stop the buzzer tone
}
}