Using 2 Ultrasonic sensors with UNO

Hi,
I am building a autonomus driving car, with one ultrasonic senor(HC-SR04) on the front and one on the back. And this is the code I am using

#define trigPin 12
#define echoPin 11
#define trigPin2 6
#define echoPin2 5
#define motorIn1 9
#define motorIn2 10
#define max_distance 200
long duration, distance, frontdis, backdis; 
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(motorIn1, OUTPUT);
pinMode(motorIn2, OUTPUT);
pinMode(trigPin, OUTPUT);                       
pinMode(echoPin, INPUT);                          
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
SonarSensor(trigPin, echoPin);              
frontdis = distance;                     
SonarSensor(trigPin2,echoPin2);               
backdis = distance;
Serial.print(frontdis);
Serial.println(backdis);
/**
if (frontdis < 2)
{
  analogWrite(motorIn1, 500);
  analogWrite(motorIn2, 0);
  Serial.print("Going back");
}else{
  analogWrite(motorIn1, 0);
  analogWrite(motorIn2, 500);
  Serial.print("Everything all right!!");
}
**/
}

void SonarSensor(int trigPinSensor,int echoPinSensor)//it takes the trigPIN and the echoPIN 
{
digitalWrite(trigPinSensor, LOW); 
delayMicroseconds(2);
digitalWrite(trigPinSensor, HIGH);
delayMicroseconds(10); 
digitalWrite(trigPinSensor, LOW);
duration = pulseIn(echoPinSensor, HIGH);
distance= (duration/2) * 0.0314;  
}
void front()
{
  analogWrite(motorIn1, 500);
  analogWrite(motorIn2, 0);
}
void back()
{
  analogWrite(motorIn1, 0);
  analogWrite(motorIn2, 500);
}

In the serial monitor I get 0 for both of the. Here is a picture of the error(it should be somewhere here)
I know that using NewPing Library. But I get a zero for one of them, and I have no idea where to store them. If you are answering this it would be great to provode some code examples, I am using a L293D motor driver for this project. Thanks in advance. :slight_smile:

Try making the distance,frontdis, backdis variables type float instead of long.

Thanks alot now it works. And also I figured out that I was not giving GND the whole time(silly mistake) but changing to float also helped me get my results.

I'd put a longer delay between pings.
As it is now, a delayed return for an earlier ping could be mistaken as a nearby return from the most recent ping.

I'd also make SonarSensor return a value, instead of using a global.

AWOL, so does return in C++ work like this

void SonarSensor(int trigPinSensor,int echoPinSensor)//it takes the trigPIN and the echoPIN 
{
digitalWrite(trigPinSensor, LOW); 
delayMicroseconds(2);
digitalWrite(trigPinSensor, HIGH);
delayMicroseconds(10); 
digitalWrite(trigPinSensor, LOW);
duration = pulseIn(echoPinSensor, HIGH);
distance= (duration/2) * 0.0314;
return(distance);  
}

I know some python, so I get confused between both languages. And also that I still learning C++. Thanks in advance. And also does delay work like this

SonarSensor(trigPin, echoPin);              
frontdis = distance;
delay(500);                     
SonarSensor(trigPin2,echoPin2);               
backdis = distance;

No.
When you wrote "void" there, you were promising the compiler that the function wouldn't return a value.

Yes,that's how delay works, though if you don't want to waste 50% of your processor cycles, you may want to look at the blink without delay example, or Robin2's excellent tutorial on hhow to do multiple things at the same time.

I'd limit your ping rate to no more than about 30 a second, split between both sensors.