how to i correct my program codes for combining 2 HC SR04 together.

Hi, I encountered some problems here. I am currently doing a project that have to use HC SR04. I am still new to Arduino and HC SR04. I am in urgent as I have to submit my project next week on Wednesday. I tried my own using just one ultrasonic sensor. I am able to detect a distance with one ultrasonic. But, when it comes to 2, the system can’t read the 2nd one. I have look through NewPing but I can’t seem to understand. So, besides NewPing, is there other way out?

The attachment below is the program code that for two ultrasonic.

I hope someone could give me some guidelines to correct this program.

sketch_may29a.ino (1.77 KB)

Please:

Modify the tittle to something meaningful.

Put your code in its own window as seen in other posts. This can be done by placing     [code] and [/code]  around the code. This makes it easier for others to read.

Weedpharma

Does the second sensor work if the ping and measurement code for ping 1 are commented out ?

What happens if you do ping1/measurement 1/ping 2/measurement 2 instead of ping1/ping2/measurement 1/measurement 2 ? Using arrays of values and a for loop would tidy up the code to do this would make it more tidy too.

I may be far out into left field on this, but most PLC'S I work with require a timeframe between polling one to polling the other. Looks like you're trying to poll both at the same time.

UKHeliBob: Does the second sensor work if the ping and measurement code for ping 1 are commented out ?

What happens if you do ping1/measurement 1/ping 2/measurement 2 instead of ping1/ping2/measurement 1/measurement 2 ? Using arrays of values and a for loop would tidy up the code to do this would make it more tidy too.

i have not tried using Ping because i do not know how to edit the codes. Maybe you can help me?

What I meant was to try the same code as you use for sensor one but using sensor two and its pins. Does sensor two work on its own ?

Why have you modified your original post to attach the code rather than put it in code tags as suggested ? Attaching it means that it is not so convenient to copy into the IDE or an editor to provide help.

Code from original post:

int echoPin  = 7;  // Arduino pin tied to echo pin on the ultrasonic sensor 1.
int trigPin  = 8;  // Arduino pin tied to trigger pin on the ultrasonic sensor 1.
int echoPin2  = 9;  // Arduino pin tied to echo pin on the ultrasonic sensor 2.
int trigPin2 = 10;  // Arduino pin tied to trigger pin on the ultrasonic sensor 2.
int ledPin   = 11; // yellow LED

int FUS_OUTPUT_DISTANCE = 100;    // Front Ultrasonic distance
int AUS_OUTPUT_DISTANCE = 100;    // Front Ultrasonic distance
//int SUS_OUTPUT_DISTANCE = 100;    // Front Ultrasonic distance
long duration, cm, duration2, cm2;   // Duration used to calculate distance

void setup() 
{
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(trigPin2, OUTPUT);
 pinMode(echoPin2, INPUT); 
 pinMode(ledPin, OUTPUT); 
 
}

void loop() 
{/* The following trigPin/echoPin cycle is used to determine the distance of the nearest object by bouncing soundwaves off of it. */ 
 digitalWrite(trigPin, LOW); 
 digitalWrite(trigPin2, LOW);
 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);
 digitalWrite(trigPin2, HIGH);
 delayMicroseconds(10); 
 
 digitalWrite(trigPin, LOW);
 digitalWrite(trigPin2, LOW);
 duration  = pulseIn(echoPin, HIGH);
 duration2 = pulseIn(echoPin2, HIGH);
 Calculate the distance (in cm) based on the speed of sound.
 cm = duration/58.2;
 cm2 = duration2/58.2;

 
 if (FUS_OUTPUT_DISTANCE <= 100 )//|| AUS_OUTPUT_DISTANCE <100)
 {/* Send the distance to computer and Turn LED ON to indicate "within of range" */
 Serial.println(cm);
 Serial.println(cm2);
 digitalWrite(ledPin, HIGH); 
 
 }
 else 
 {/* Send the distance to the computer and turn LED OFF to indicate "out of range" */
 Serial.println(cm);
 Serial.println(cm2);
 digitalWrite(ledPin, LOW); 

 }
 
 //Delay 50ms before next reading.
 delay(50);
}

Try this

const byte echoPin  = 7;  // Arduino pin tied to echo pin on the ultrasonic sensor 1.
const byte trigPin  = 8;  // Arduino pin tied to trigger pin on the ultrasonic sensor 1.
const byte echoPin2  = 9;  // Arduino pin tied to echo pin on the ultrasonic sensor 2.
const byte trigPin2 = 10;  // Arduino pin tied to trigger pin on the ultrasonic sensor 2.
const byte ledPin   = 11; // yellow LED

const int FUS_OUTPUT_DISTANCE = 100;    // Front Ultrasonic distance
const int AUS_OUTPUT_DISTANCE = 100;    // Rear Ultrasonic distance

void setup() 
{
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(trigPin2, OUTPUT);
 pinMode(echoPin2, INPUT); 
 pinMode(ledPin, OUTPUT); 
 
}

void loop() 
{
// Calculate the distance (in cm) based on the speed of sound.
 delay(30);
 cm = range (trigPin, echoPin);
 delay(30);
 cm2 = range (trig2Pin, echo2Pin);

 Serial.println(cm);
 Serial.println(cm2);
}

unsigned long range (byte trig, byte echo)
{
 digitalWrite(trig, LOW); 
 digitalWrite(trig, HIGH);
 delayMicroseconds(10); 
 digitalWrite(trig, LOW);
 unsigned long duration  = pulseIn(echo, HIGH);
 return duration/58.2;
}

I’m still quite new in using this forum. Thanks.

 duration  = pulseIn(echoPin, HIGH);
 duration2 = pulseIn(echoPin2, HIGH);

The reason it failed: the first pulseIn() waits for the echo pin to go HIGH and then waits for it to go LOW. While that happens you miss the pulse from the other sensor. By the time you get to the second pulseIn() the pulse from the second sensor has come and gone.