Hello,
I am trying to read the values of two ultrasonic sensors one after the other using the pulseIn() function. I have read that it is not possible to read the values of ultrasonic sensors simultaneously, but I am not try to do it simultaneously (although I would like it to read the sensors values at least 500 milliseconds after the other). I have tried using the pulseIn() command but it does not seem to be reading the values of the second sensor. I was hoping for help with this issue.
I have attached my code below.
Thank you in advance for the help!
#define trigPinOne 12
#define echoPinOne 11
#define trigPinTwo 13
#define echoPinTwo 10
int distance_in_one = 0;
int distance_in_two = 14;
int duration_one = 0;
int distance_sum_one;
int distance_one=0;
int duration_two = 0;
int distance_sum_two;
int distance_two=0;
int i;
void setup(){
Serial.begin(9600);
Serial.println("Testing has begun \n");
pinMode(trigPinOne, OUTPUT);
pinMode(echoPinOne,INPUT);
pinMode(trigPinTwo,OUTPUT);
pinMode(echoPinTwo,INPUT);
}
void loop(){
distance_sum_one=0;
distance_sum_two=0;
for(i=0;i<5;i++){
digitalWrite(trigPinOne, LOW);
delayMicroseconds(2);
digitalWrite(trigPinOne, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinOne,LOW);
digitalWrite(trigPinTwo,LOW);
delayMicroseconds(2);
digitalWrite(trigPinTwo,HIGH);
delayMicroseconds(10);
digitalWrite(trigPinTwo,LOW);
duration_one=pulseIn(echoPinOne);
duration_two=pulseIn(echoPinTwo);
distance_sum_one+=duration_one*0.0135/2;
distance_sum_two+=duration_two*0.01235/2;
}
distance_one=distance_sum_one/5;
distance_two=distance_sum_two/5;
Serial.print("DistanceOne: ");
Serial.println(distance_one);
Serial.print("DistanceTwo: ");
Serial.println(distance_two);
delay(1000);
}