First of all, good evening, I want to ask how to program 3 ultrasonic sensors so that when the first and second ultrasonic sensors are active, the third is not active and when the third ultrasonic sensor is active, the first and second ultrasonic sensors are not active. Is my program correct?
const int triggerPin1 = 2;
const int echoPin1 = 3;
const int triggerPin2 = 4;
const int echoPin2 = 5;
const int triggerPin3 = 7;
const int echoPin3 = 8;
const int LED1 = 13;
const int LED2 = 5;
const int LED3 = 6;
long duration;
int distance;
void setup() {
pinMode(triggerPin1, OUTPUT);
pinMode(triggerPin2, OUTPUT);
pinMode(triggerPin3, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(echoPin2, INPUT);
pinMode(echoPin3, INPUT);
Serial.begin(9600);
}
void loop_ultrasonic_1() { // untuk sensor 1
digitalWrite(triggerPin1, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin1, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin1, LOW);
duration = pulseIn(echoPin1, HIGH);
distance = duration*0.034/2;
if(distance > 50){
Serial.println("Jarak lebih dari 50cm");
digitalWrite(LED1, HIGH);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
}else if(distance <= 50 && distance >= 20){
Serial.println("Jarak berada diantara 20cm hingga 50cm");
digitalWrite(LED2, HIGH);
digitalWrite(LED1, LOW);
digitalWrite(LED3, LOW);
}else if(distance < 20){
Serial.println("Jarak kurang dari 20cm");
digitalWrite(LED3, HIGH);
digitalWrite(LED2, LOW);
digitalWrite(LED1, LOW);
}
}
void loop_ultrasonic_2() { // untuk sensor 2
digitalWrite(triggerPin2, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin2, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin2, LOW);
duration = pulseIn(echoPin2, HIGH);
distance = duration*0.034/2;
if(distance > 50){
Serial.println("Jarak lebih dari 50cm");
digitalWrite(LED1, HIGH);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
}else if(distance <= 50 && duration >= 20){
Serial.println("Jarak berada diantara 20cm hingga 50cm");
digitalWrite(LED2, HIGH);
digitalWrite(LED1, LOW);
digitalWrite(LED3, LOW);
}else if(distance < 20){
Serial.println("Jarak kurang dari 20cm");
digitalWrite(LED3, HIGH);
digitalWrite(LED2, LOW);
digitalWrite(LED1, LOW);
}
}
void loop_ultrasonic_3() { // untuk sensor 3
digitalWrite(triggerPin3, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin3, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin3, LOW);
duration = pulseIn(echoPin3, HIGH);
distance = duration*0.034/2;
if(distance > 50){
Serial.println("Jarak lebih dari 50cm");
digitalWrite(LED1, HIGH);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
}else if(distance <= 50 && distance >= 20){
Serial.println("Jarak berada diantara 20cm hingga 50cm");
digitalWrite(LED2, HIGH);
digitalWrite(LED1, LOW);
digitalWrite(LED3, LOW);
}else if(distance < 20){
Serial.println("Jarak kurang dari 20cm");
digitalWrite(LED3, HIGH);
digitalWrite(LED2, LOW);
digitalWrite(LED1, LOW);
}
}
void loop() {
loop_ultrasonic_1();
loop_ultrasonic_2();
loop_ultrasonic_3();
}