2 ping sensors and 1 diecimila

hello people

I have a problem I am trying to interface 2 pings with my arduino, I have done it with one, but when i use two pings and upload the code on to the board the lights on the pings flash but at the same time, and when i move my hand away the flashing becomes faster and slower and sometimes erratic, i have changed the delays of the low high low pulse for each sensor but still the lights come on at the same time and gives me an erratic read in the serial monitor, has anyone had this problem before or does anyone know what i am doing wrong any help would be greatly appreciated.
cheers,
rich

If you posted the code then perhaps someone could see what you are doing wrong. Otherwise my physic powers draw a blank on this one.

sorry for not putting it up before.

int pingPinx = 7;
int pingPiny = 8;

void setup()
{
Serial.begin(9600);
}

void loop()
{
long duration, inches, cm;

// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// We give a short LOW pulse beforehand to ensure a clean HIGH pulse.
pinMode(pingPinx, OUTPUT);
pinMode(pingPiny, OUTPUT);
digitalWrite(pingPinx, LOW);
delayMicroseconds(2);
digitalWrite(pingPiny, LOW);
delayMicroseconds(3);
digitalWrite(pingPinx, HIGH);
delayMicroseconds(5);
digitalWrite(pingPiny, HIGH);
delayMicroseconds(6);
digitalWrite(pingPinx, LOW);
digitalWrite(pingPiny, LOW);

// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPinx, INPUT);
pinMode(pingPiny, INPUT);
duration = pulseIn(pingPinx, HIGH);
duration = pulseIn(pingPiny, HIGH);

// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

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

delay(100);
}

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;
}

I think your problem is that you are sending two pulses out at the same time but reading them back one after the other. The pulseIn() function waits until the pulse has been received before looking for the next one by which time the second pulse has disappeared.
What you need to do is to write the function of the pulseIn() function but looking for two inputs. Make a note of the time you start looking with:-
startTime = millis();
and when you see either pulse go low make a note of the time for that pin:-
Pulse1Time = missis() - startTime;
Keep two logic variables so that you know when you have made a record of the time on the first occurrence of a zero on each of the pins and so prevent it recording the time on each pass of the loop. When both pins are zero you can finish the loop.

Alternately just put one pulse and look after the other pulse and look.
Hope that helps.

i have adapted some more code and it does not work at all, it will upload to the board but it will not make the pings light up, I am trying to use and arrays and loop but i think i'm doing something wrong

/* New Variables */
int delay_time = 20; // delay for this amount each write cycle.
byte printing_byte = 0;

int timer = 10;
int pingpins[] = { 7, 8 }; // Ultrasound signal pin
int val = 0;
int pingValue = 0;
int timecount = 0;
int num_pins = 2;
int ledPin = 13; // LED connected to digital pin 13

void setup() {
beginSerial(9600); // Sets the baud rate to 9600
pinMode(ledPin, OUTPUT); // Sets the digital pin as output
}

void loop() {
int i;

timecount = 0;
val = 0;
pinMode(pingpins*, OUTPUT); // Switch signalpin to output*
_ /* Send low-high-low pulse to activate the trigger pulse of the sensor_
_ * -------------------------------------------------------------------_
_ */_

  • for (i = 0; i < num_pins,i++;){*
    _ digitalWrite(pingpins*, LOW); // Send low pulse*_
    * delayMicroseconds(2); // Wait for 2 microseconds*
    _ digitalWrite(pingpins*, HIGH); // Send high pulse*
    * delayMicroseconds(5); // Wait for 5 microseconds*
    digitalWrite(pingpins*, LOW); // Holdoff*
    }
    * for (i = num_pins - 1; i >= 0; i--){*
    digitalWrite(pingpins*, LOW); // Send low pulse*
    * delayMicroseconds(2); // Wait for 2 microseconds*
    digitalWrite(pingpins*, HIGH); // Send high pulse*
    * delayMicroseconds(5); // Wait for 5 microseconds*
    digitalWrite(pingpins*, LOW); // Holdoff*
    }
    /* Listening for echo pulse_

    _ * -------------------------------------------------------------------
    /
    pinMode(pingpins, INPUT); // Switch signalpin to input*

    val = digitalRead(pingpins*); // Append signal value to val*
    * while(val == LOW) { // Loop until pin reads a high value*
    val = digitalRead(pingpins*);
    delayMicroseconds(timer);
    }
    while(val == HIGH) { // Loop until pin reads a high value*

    val = digitalRead(pingpins*);
    timecount = timecount +1; // Count echo pulse time*

    * }
    / Writing out values to the serial port_

    _ * -------------------------------------------------------------------
    /
    pingValue = timecount; // Append echo pulse time to ultrasoundValue*

    /* BEGIN EDITED CODE /
    pingValue = pingValue - 14;
    pingValue = pingValue / 5;
    if(pingValue > 127) {
    }*_

* else {*

* printing_byte = pingValue;
Serial.print(printing_byte);
_ }
/ END EDITED CODE /
/ Lite up LED if any value is passed by the echo pulse_

_ * -------------------------------------------------------------------
/
if(timecount > 0){
digitalWrite(ledPin, HIGH);
}
/ Delay of program_

_ * -------------------------------------------------------------------
/
delay(40);
}*_

You need to send the pulse and read it on one ping at a time. send pulse x, wait, read pulse x send pulse y, wait, read pulse y.

does that mean i can actually use an array for the sensors and it will work?

heres is what i have now but still things are wrong now the act light on the sensors flashes twice at the start lights up the led but must just switch off or get confused.

/* New Variables */
int delay_time = 20; // delay for this amount each write cycle.
byte printing_byte = 0;

int pingpina = 7; // Ultrasound signal pin
int pingpinb = 8; // Ultrasound signal pin
int vala = 0;
int valb = 0;
int pingValuea = 0;
int pingValueb = 0;
int timecounta = 0;
int timecountb = 0;
int ledPin = 13; // LED connected to digital pin 13

void setup() {
beginSerial(9600); // Sets the baud rate to 9600
pinMode(ledPin, OUTPUT); // Sets the digital pin as output
}

void loop() {
timecounta = 0;
timecountb = 0;
vala = 0;
valb = 0;
pinMode(pingpina, OUTPUT);
pinMode(pingpinb, OUTPUT); // Switch signalpin to output

/* Send low-high-low pulse to activate the trigger pulse of the sensor


*/

digitalWrite(pingpina, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(pingpina, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(pingpina, LOW); // Holdoff
delayMicrosenconds(10); // delay between the sends of pulse to each sensor
digitalWrite(pingpinb, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(pingpinb, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(pingpinb, LOW); // Holdoff

/* Listening for echo pulse


*/

pinMode(pingpina, INPUT); // Switch signalpin to input
vala = digitalRead(pingpina); // Append signal value to val
while(vala == LOW) { // Loop until pin reads a high value
vala = digitalRead(pingpina);
delayMicroseconds(10);
pinMode(pingpinb, INPUT); // Switch signalpin to input
valb = digitalRead(pingpinb); // Append signal value to val
while(valb == LOW) { // Loop until pin reads a high value
valb = digitalRead(pingpinb);
}

while(vala == HIGH) { // Loop until pin reads a high value
vala = digitalRead(pingpina);
timecounta = timecounta +1;
delayMicroseconds(10);
while(valb == HIGH) { // Loop until pin reads a high value
valb = digitalRead(pingpinb);
timecountb = timecountb +1; // Count echo pulse time
}

/* Writing out values to the serial port


*/

pingValuea = timecounta; // Append echo pulse time to ultrasoundValue

/* BEGIN EDITED CODE */

pingValuea = pingValuea - 14;
pingValuea = pingValuea / 5;

if(pingValuea > 127) {
}

else {

printing_byte = pingValuea;
Serial.print(printing_byte);
}
/* END EDITED CODE */

/* Lite up LED if any value is passed by the echo pulse


*/

if(timecounta > 0){
digitalWrite(ledPin, HIGH);
}

/* Delay of program


*/

delay(40);
}

Again you are getting mixed up and not doing what I said.
But never mind it's all part of the learning process. :wink:
This code has been compiled but as I don't have any sensors I have not been able to test it but the logic of what you need to do is sound.

int pingpina = 7; // Ultrasound signal pin
int pingpinb = 8; // Ultrasound signal pin
long vala, valb;
long timecounta;
long timecountb;
long startTime;
int ledPin = 13; // LED connected to digital pin 13
boolean pulseAin, pulseBin;

void setup() {
 beginSerial(9600);                  // Sets the baud rate to 9600
 pinMode(ledPin, OUTPUT);            // Sets the digital pin as output
}

void loop() {
 timecounta = 0;
 timecountb = 0;
 vala = 0;
 valb = 0;
 pinMode(pingpina, OUTPUT);
 pinMode(pingpinb, OUTPUT); // Switch signalpin to output

 /* Send low-high-low pulse to activate the trigger pulse of the sensor
  * -------------------------------------------------------------------
  */

  digitalWrite(pingpina, LOW); // Send low pulse
  digitalWrite(pingpinb, LOW); // Send low pulse
  
 delayMicroseconds(2); // Wait for 2 microseconds
 digitalWrite(pingpina, HIGH); // Send high pulse
 digitalWrite(pingpinb, HIGH); // Send high pulse
 // do you need anything else here?
 /* Listening for echo pulse
  * -------------------------------------------------------------------
  */

 pulseAin = false;
 pulseBin = false;
 
 pinMode(pingpina, INPUT); // Switch signalpin A to input
 pinMode(pingpinb, INPUT); // Switch signalpin B to input
 startTime = millis();
 
 while(pulseAin == false || pulseBin == false) { // Loop until both pins reads a high value
 if( digitalRead(pingpina) != 0 && pulseAin == false){
 vala = millis() - startTime;
 pulseAin = true;
    }
if( digitalRead(pingpinb) != 0 && pulseBin == false){
 valb = millis() - startTime;
 pulseBin = true;
    }
 }
 /* Writing out values to the serial port
  * -------------------------------------------------------------------
  */
// do the scalling of your results here
  Serial.print("Sensor A value =");
  Serial.println(vala);
 Serial.print("Sensor b value =");
  Serial.println(valb);
  // put any delay you need here to slow down the loop
}

When posting code go to the hash character in the reply window and paste your code between the two bracketed expressions. That way you get a scrolling box and you don't take up masses of room with each message.

The only problem I see with your code is you are sending a pulse out both sensors at the same time and then counting until you hear a return which could be either pulse and you have no way of knowing which it is. As I said you need to send pulseA and wait for the return before sending pulseB and waiting for it's return.

and you have no way of knowing which it is.

That is only if the two sensors have an overlapping area.
The original code had the two pulses being sent simultaneously but received sequentially. I did say in my first posting that one solution was:-

Alternately just put one pulse and look, after the other pulse and look.

Grumpy_Mike, my comment was to the OP since his code is still doing it wrong. At 1100 fps you don't have much time to wait for the pulse.

Thank you everyone one who has responded, i'm sorry if I didn't really understand straight away what is wrong but am really new to programming and have never done anything like this before! I just think if you can imagine it and it doesn't bend the laws of physics then it's probably possible thanks for your help but it'll never be over!

take it easy

Mouldysoul, did you ever get it working?

yes just now and am very excited, I am comparing so some the code together so i can get it working the way i want to, i'll be back very shortly i'm sure!

hello again
The code is flashing the pings in the right way now but am getting confused as to where to fit the bit to count the pulse and then scale it.

  pinMode(pingpina, INPUT); // Switch signalpin to input
  val = digitalRead(pingpina); // Append signal value to val
  while(val == LOW) { // Loop until pin reads a high value
    val = digitalRead(pingpina);
  }

  while(val == HIGH) { // Loop until pin reads a high value
    val = digitalRead(pingpina);
    timecount = timecount +1;            // Count echo pulse time
  }
int pingpina = 7; // Ultrasound signal pin
int pingpinb = 8; // Ultrasound signal pin
long vala, valb;
long timecounta;
long timecountb;
long startTime;
int ledPin = 13; // LED connected to digital pin 13
boolean pulseAin, pulseBin;

void setup() {
 beginSerial(9600);                  // Sets the baud rate to 9600
 pinMode(ledPin, OUTPUT);            // Sets the digital pin as output
}

void loop() {
 timecounta = 0;
 timecountb = 0;
 vala = 0;
 valb = 0;
 pinMode(pingpina, OUTPUT);
 pinMode(pingpinb, OUTPUT); // Switch signalpin to output

 /* Send low-high-low pulse to activate the trigger pulse of the sensor
  * -------------------------------------------------------------------
  */

 digitalWrite(pingpina, LOW); // Send low pulse
 digitalWrite(pingpinb, LOW); // Send low pulse
 delayMicroseconds(2); // Wait for 2 microseconds
 digitalWrite(pingpina, HIGH); // Send high pulse
 digitalWrite(pingpinb, HIGH); // Send high pulse
 delayMicroseconds(5); // Wait for 5 microseconds
 digitalWrite(pingpina, LOW); // Send low pulse
 digitalWrite(pingpinb, LOW); 

 /* Listening for echo pulse
  * -------------------------------------------------------------------
  */

 pulseAin = false;
 pulseBin = false;

 pinMode(pingpina, INPUT); // Switch signalpin A to input
 pinMode(pingpinb, INPUT); // Switch signalpin B to input
 startTime = millis();

 while(pulseAin == false || pulseBin == false) { // Loop until both pins reads a high value
 if( digitalRead(pingpina) != 0 && pulseAin == false){
 vala = millis() - startTime;
 pulseAin = true;
    }
if( digitalRead(pingpinb) != 0 && pulseBin == false){
 valb = millis() - startTime;
 pulseBin = true;
    }
 } // i think i need to start counting in this part above

 /* Writing out values to the serial port
  * -------------------------------------------------------------------
  */
// do the scalling of your results here
  Serial.print("A = ");
  Serial.println(vala);
 Serial.print("B = ");
  Serial.println(valb);
 delay(40);
}

where to fit the bit to count the pulse and then scale it.

there is no need to start counting, that has already been done by the
valb = millis() - startTime;
bit of the code. valb will contain a number which is the number of milliseconds it took to get the pulse back.

the at the line:-

// do the scaling of your results here

is where you do the code to scale your results to give you numbers in a range that you want.

so do i scale vala like this

vala = timecounta;
valb = timecountb;

vala = vala - 14;
vala = vala / 5;
valb = valb - 14;
valb = valb / 5;


if(vala > 127) {
}
else { printing_bytea = vala;

  Serial.print(printing_bytea);

delay(40);
}

right so some other dude who has got it working has shown me his code but there is an error message in the bottom window that says error: stray \ in program, what does this mean? here is the code:

/* Ultrasound Sensor
*——————
*
* Reads values (00014-01199) from an ultrasound sensor (3m sensor)
* and writes the values to the serialport.
*
* http://www.xlab.se | http://www.0j0.org
* copyleft 2005 Mackie for XLAB | DojoDave for DojoCorp
*
*/

int ultraSoundSignal1 = 6; // Ultrasound signal pin
int ultraSoundSignal2 = 7; // Ultrasound signal pin
int val1 = 0;
int val2 = 0;
int ultrasoundValue1 = 0;
int ultrasoundValue2 = 0;
int timecount1 = 0; // Echo counter
int timecount2 = 0; // Echo counter
int ledPin= 13; // LED connected to digital pin 13

void setup() {
Serial.begin(9600); // Sets the baud rate to 9600 - was Serial.begin(9600);
pinMode(ledPin, OUTPUT); // Sets the digital pin as output
}

void loop() {

getRange( ultraSoundSignal1, val1, ultrasoundValue1, timecount1, 'A' );
getRange( ultraSoundSignal2, val2, ultrasoundValue2, timecount2, 'B' );

delay(100);
}

void getRange( int ultraSoundSignal, int val, int ultrasoundValue, int timecount, char id )
{
timecount = 0;
val = 0;
pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output

/* Send low-high-low pulse to activate the trigger pulse of the sensor
* ——————————————————————-
*/

digitalWrite(ultraSoundSignal, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(ultraSoundSignal, LOW); // Holdoff

/* Listening for echo pulse
* ——————————————————————-
*/

pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
val = digitalRead(ultraSoundSignal); // Append signal value to val
while(val == LOW) { // Loop until pin reads a high value
val = digitalRead(ultraSoundSignal);
}

while(val == HIGH) { // Loop until pin reads a high value
val = digitalRead(ultraSoundSignal);
timecount = timecount +1; // Count echo pulse time
}

/* Writing out values to the serial port
* ——————————————————————-
*/

ultrasoundValue = timecount; // Append echo pulse time to ultrasoundValue

serialWrite(id); // Example identifier for the sensor
printInteger(ultrasoundValue);
//serialWrite(10);
//serialWrite(13);

/* Lite up LED if any value is passed by the echo pulse
* ——————————————————————-
*/

if(timecount > 0){
digitalWrite(ledPin, HIGH);
}

/* Delay of program
* ——————————————————————-
*/

//Serial.print( val );
}

so do i scale vala

Scale it any way you want, it depends on what you want to do with the numbers.

Personally I wouldn't do any scaling and just work with the raw numbers. If you do it the way you posted then I would make sure you are using floats as ints are going to suffer from rounding errors as a result of the divides.

As to the other code I can't see where the stray \ is coming from but I have not got a compiler, I will try it at home tonight.