#define trigPin1 9
#define echoPin1 10
#define trigPin2 11
#define echoPin2 12
#define buzzer1 6
#define buzzer2 7
long duration1, distance1;
long duration2, distance2;
const int distanceThreshold = 20; // distance threshold in centimeters
void setup() {
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(buzzer1, OUTPUT);
pinMode(buzzer2, OUTPUT);
Serial.begin(9600);
}
void loop() {
// Read first ultrasonic sensor
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1, HIGH);
distance1 = (duration1 / 2) / 29.1; // Convert duration to distance
// Read second ultrasonic sensor
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
duration2 = pulseIn(echoPin2, HIGH);
distance2 = (duration2 / 2) / 29.1; // Convert duration to distance
// Check if any sensor detected an object within the threshold distance
if (distance1 < distanceThreshold) {
digitalWrite(buzzer1, HIGH); // Turn on buzzer 1
Serial.println("Window 1 detected an object.");
} else {
digitalWrite(buzzer1, LOW); // Turn off buzzer 1
}
if (distance2 < distanceThreshold) {
digitalWrite(buzzer2, HIGH); // Turn on buzzer 2
Serial.println("Window 2 detected an object.");
} else {
digitalWrite(buzzer2, LOW); // Turn off buzzer 2
}
delay(100); // Short delay to avoid spamming the serial monitor
}
No comments:
Post a Comment