Ping sensors

Hi there
Im really new with all this, Im battling to get a reading from my pingpin2, I have swapped my sensors around they both work on pingpin1 but cant get both of then to work together.

This is my code below any advice would be much appreciated

Thanks in advance.

#include <Servo.h>

Servo myservo; // create servo object named myservo to control a servo/ Right servo
Servo myservo1; //create servo object named myservo1 to control a servo/ Left servo
int pos = 0;

const int pingPin = 7;
const int pingPin1 = 8;
const int pingPin2 = 13;

void setup() {
// initialize serial communication:
Serial.begin(9600);
myservo.attach(11); // Right servo
myservo1.attach(10); //Left servo
}

void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:

long duration, inches, cm;
long duration1, inches1, cm1;

// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);

pinMode(pingPin1, OUTPUT);
digitalWrite(pingPin1, LOW);
delayMicroseconds(2);
digitalWrite(pingPin1, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin1, LOW);

pinMode(pingPin1, INPUT);
duration = pulseIn(pingPin1, HIGH);

inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

pinMode(pingPin2, OUTPUT);
digitalWrite(pingPin2, LOW);
delayMicroseconds(2);
digitalWrite(pingPin2, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin2, LOW);

pinMode(pingPin2, INPUT);
duration1 = pulseIn(pingPin2, HIGH);

inches1 = microsecondsToInches(duration1);
cm1 = microsecondsToCentimeters(duration1);

Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
Serial.print(inches1);
Serial.print("in1, ");
Serial.print(cm1);
Serial.print("cm1");
Serial.println();

delay(500);

}

long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}

Why have you got three ping pins?

You trigger ping pin. (Pin 7), but don't seem to wait for the echo. Why?

Please remember to use code tags when posting code.

Sure will do that from now on, Ive got three ping pins Because the ping pin (pin 7) is my sensor that only sends out (trig) and the other 2 only receive (echo),
Trying to make a follow robot with 1 sender sensor and two receivers on the robot

But won't trigger the other two produce chirps?
Or have you removed the TX transducer?

Yes i have removed them, i have gotten one sensor to send and one to receive using the same code all i did now is add another ping pin(ping pin 2) but cant get them both to work.

I gather you are using 2 ping sensors, each with separate Trig and Echo pins.

Anyway, first of all, do yourself a big favour and rename pingPin1 and pingPin2 to something a lot more meaningfull, like echo_1 and echo_2.

Second problem I see is that you are messing around setting the Echo pins to outputs. What for? The way you use a ping sensor is to trigger it, then use pulseIn() to time the return. There is never any need to set an Echo pin to output.

Third problem. You trigger once, then use pulseIn to time the first echo pin. You then use pulseIn to time the second Echo pin. Lets' see what happens if both receiving transducers are aimed at the same target.

Trigger - sends pulse.

echo_1 - pulseIn waits for the first echo pin to go HIGH, then waits for it to go LOW, then assigns a value to duration 1. It does this because it's a blocking function.

THEN (after the pulseIn returns)

echo_2 - waits for the second echo pin to go HIGH.

Strangely enough, that second echo pin has already gone HIGH then LOW.

So, if you want to check both echos from the same ping, you will need to use some other technique that does not involve pulseIn(). Perhaps a couple of interrupts, one on echo_1 and one on echo_2, that you can use to time the durations.

There is never any need to set an Echo pin to output.

Yes you do - the devices need to be triggered, even if they are not capable of transmitting.

Ok sort of understand you, but still not sure what to do =(

You could put the trigger/echo pins on the external interrupt pins and use the attached interrupt service routines to timestamp the returning echoes using "micros()".

Great thank you all so much going to see what i can get done.

AWOL:

There is never any need to set an Echo pin to output.

Yes you do - the devices need to be triggered, even if they are not capable of transmitting.

Echo |= Trig. Yes, they need to be triggered, but that does not imply that the echo pins need to be set to outputs at any time

As I said, I am assuming the OP is using a ping sensor with separate Trig and Echo pins. In that case, you leave the Echo pin set as input, regardless of whether or not the sensor as had its transmit transducer removed.

I did make another assumption, though, and it would appear that my initial reading of the setup may have been wrong.

The OP said

ping pin (pin 7) is my sensor that only sends out (trig) and the other 2 only receive (echo),

That could mean he has two sensors or three sensors.

In either case, he will need to trigger all the Trig pins at the same time; two if he has two sensors, and all three if he has three sensors. Only one of these should have a transmit transducer. The Echo pins need to be separate, of course, and he can't use pulseIn() to measure the duration simultaneously.

Echo |= Trig. Yes, they need to be triggered, but that does not imply that the echo pins need to be set to outputs at any time

They're Pings; they only have three pins - power, ground, and trigger/ echo.

AWOL:

Echo |= Trig. Yes, they need to be triggered, but that does not imply that the echo pins need to be set to outputs at any time

They're Pings; they only have three pins - power, ground, and trigger/ echo.

Perhaps the OP will tell us which of us has the correct assumption. All my ping sensors have 4 pins. - power, ground, trig, and echo. Reading his code, I see three pins - one ping and two echo. If he is using 3-pin sensors, that translates to three sensors. It was not clear from the text whether there are two or three sensors.

So, Undermentioned, which is it? do your ping sensors have 3 pins or 4 pins? How many sensors total? How many transmit transducers did you remove?

lar3ry:

AWOL:

Echo |= Trig. Yes, they need to be triggered, but that does not imply that the echo pins need to be set to outputs at any time

They're Pings; they only have three pins - power, ground, and trigger/ echo.

. It was not clear from the text whether there are two or three sensors.

Because the ping pin (pin 7) is my sensor that only sends out (trig) and the other 2 only receive (echo),

What's not clear?

What would be the circuit for a ping sensor?

Hi, well i have 3 HC-SR04 sensors that im using with 4 pins - vcc/trig/echo/gnd, i have removed 2 trig transducers (recieving sensors) and 1 echo transducer (sending sensor).

well i have 3 HC-SR04 sensors that im using with 4 pins - vcc/trig/echo/gnd

So, not three Ping sensors, as you wrote earlier?

No ive got 3 ultrasonic sensors,sorry if i phrased it wrong

Undermentioned:
Hi, well i have 3 HC-SR04 sensors that im using with 4 pins - vcc/trig/echo/gnd, i have removed 2 trig transducers (recieving sensors) and 1 echo transducer (sending sensor).

OK.. that's what I assumed you meant, from your posts.

So, in general terms.

Wire all three Trigger pins together. This is so that all three sensors will be triggered, which is necessary for the echo sensors to await an incoming ultrasonic signal.

Don't set the Echo pins to outputs.

Read up on attaching interrupts (See Reference section)
Wire the Echo pins to Arduino pins 2 and 3
Attach interrupts to both Arduino pins that are attached to Echo pins.

Set the interrupts to CHANGE.
When you get an interrupt, check the level on the echo pin associated with that interrupt.
If it's HIGH, set a variable called something like Echo1_Start, to the current value of micros()
If it's LOW, subtract the current value of micros() from Echo1_Start.
Set a boolean value to true, to tell you that this value has been stored.

Do same thing with the other interrupt, using different variable names.

When you have both boolean flags set, you can use your values, then set the booleans to false.