#include <LiquidCrystal.h>
const int en1 = 35;
const int en2 = 25;
LiquidCrystal lcd1(22, en1, 5, 21, 26, 23);
LiquidCrystal lcd2(22, en2, 5, 21, 26, 23);
#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
String Sensor_1_Status, Sensor_2_Status, Sensor_3_Status, Sensor_4_Status;
int distance_cm_1;
int distance_cm_2;
int distance_cm_3;
int distance_cm_4;
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(buzzerPin, OUTPUT);
lcd1.begin(16, 2);
lcd2.begin(16, 2);
}
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) {
sensorOn1 = true;
} else {
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) {
sensorOn2 = true;
} else {
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) {
sensorOn3 = true;
} else {
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) {
sensorOn4 = true;
} else {
sensorOn4 = false;
}
}
if (sensorOn1 == true && sensorOn2 == true) {
noTone(buzzerPin); // Stop the buzzer tone
delay(500);
tone(buzzerPin, 1000);
delay(500);
} else {
noTone(buzzerPin); // Stop the buzzer tone
}
if (sensorOn3 == true && sensorOn4 == true) {
noTone(buzzerPin); // Stop the buzzer tone
delay(500);
tone(buzzerPin, 1000);
delay(500);
} else {
noTone(buzzerPin); // Stop the buzzer tone
}
if (sensorOn1 == true) {
Sensor_1_Status = "Blocked!!";
} else {
Sensor_1_Status = "Clear";
}
if (sensorOn2 == true) {
Sensor_2_Status = "Blocked!!";
} else {
Sensor_2_Status = "Clear";
}
if (sensorOn3 == true) {
Sensor_3_Status = "Blocked!!";
} else {
Sensor_3_Status = "Clear";
}
if (sensorOn4 == true) {
Sensor_4_Status = "Blocked!!";
} else {
Sensor_4_Status = "Clear";
}
lcd1.setCursor(0, 1);
lcd1.print("S1 Status: ");
lcd1.print(Sensor_1_Status);
lcd1.setCursor(0, 2);
lcd1.print("S2 Status: ");
lcd1.print(Sensor_2_Status);
lcd2.setCursor(0, 1);
lcd2.print("S3 Status: ");
lcd2.print(Sensor_3_Status);
lcd2.setCursor(0, 2);
lcd2.print("S4 Status: ");
lcd2.print(Sensor_4_Status);
}