More than only 1 Ping sensor

Hi, does anybody have experience with using more than 1 of the Ping ultrasonic range sensors? I'd like to use 5 of them, but don't really know who to modify the existing arduino code, which is only for 1 sensor. Of course the code for 1 sensors works just fine. Any ideas????

Thanks.

One way to do it is to create a function that does the pinging, and call this function for each of the sensors.

I have posted below a hacked version of the example sketch to give you an idea of what this could look like but leave it to you to fix up as necessary to make it work.

/* Ultrasound Sensor
 *------------------
 *
 * Reads values (00014-01199) from multiple ultrasound sensors (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  ultraSoundSignalPins[] = { 7,8,9,10,11} ; // array holding pins connected to Ultrasound sensors
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
}

int ping(int ultraSoundSignal){    // this function returns the time for the sensor on the given pin

int val, ultrasoundValue,timecount;


ultrasoundValue = timecount = val = 0; 

 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
}

return  timecount ;   
}

void loop() {

  int ultrasoundValue;
  int i;
  
  for(i=0; i < 5; i++){
       ultrasoundValue = ping(ultraSoundSignalPins[i]); 
       serialWrite('A + i'); // Example identifier for the sensor
       printInteger(ultrasoundValue);
       serialWrite(10);
       serialWrite(13);
  }   
/* Delay of program
 * -------------------------------------------------------------------
 */
  delay(100);
}

Hi, thanks for the code. I didn't have time to check it earlier. Seems to work fine, at least all the Ping sensors are blinking nicely. But when I want to use the data in Pure data I get an error message. It is not a problem in Pure data, I already checked that. I guess it has something to do with the writing to serial section of the arduino code. Do you have an idea how I can modify that to get useful data? Pure data complains about the "i"?

Thanks a lot.

Try changing serialWrite('A + i'); // Example identifier for the sensor

to: serialWrite('A' + i); // Example identifier for the sensor

Wow, yeeeeeeeeeeeeeeeeeeessssssssssssssssssss!!!!!!!!!!!!!

Now it works perfectly.

Many thanks, you really helped me a lot...next gig comes always way too early :-)

Good to hear you have it working. :)

What does the system at the other end if the serial port do with the data it gets from the arduino?

Hi mem, sorry for responding so late. The preparation for the last concert was "eating" all my time and attention. Finally I used 7 ping sensors in a kind of theremin style: I "play" them with my hands, so they measure the distance of my hands above them. With my hand motions I controll the granular sythesis of a sound file. Sensor 1 works like a Toggle On/Off button (cross the stream once play, cross it twice stop etc.) Sensor 2 controls the volume Sensor 3 the loopstart position Sensor 4 the looplength Sensor 5 GrainSize Sensor 6 Pitch (but in a range of 2 semitones, not for melodies only for vibrato) Sensor 7 Reproduction speed of the granular synthesis The beautiful thing about this application is that you can control the sound with your hands and body movements respectively, but this system has also a life of its own, it kind of adds his own artistic opinions about your actions: a nice example of the non-contradiction between order and chaos :-) A real dialog between man and machine. The data from the sensors is received and mapped in Pure data and send via a local OSC-network to Reaktor where the granular synthesis takes place. Would you like to have the Pure data patch?

Sounds great, I just listened to the recording on the website you posted.

I think even Karlheinz S would have been impressed if he was still around.

Glad that its worked for you.

Thanks for visiting my webpage and I am really happy that you liked the stuff. I cannot really claim that I am strongly influenced by Karlheinz, but there are 2 fantastic pieces I can highly recommend. 1.) Contacts for electronic sounds, piano and drums, a monster piece really, far ahead and directly from outer space, but try to get this version, there is also a version using exclusively electronic sounds 2.) Studie II, great to listen to it while reading the score, which is a kind of graphical sequencer-style score from 1954!!! I guess at the moment I am kind of entering into a John Cage and Iannis Xenakis phase...and after that maybe into the pink reindeer phase... No, it's honestly faszinating to (re-)discover how visionary artist in earlier decades have already been. Guess our generation has to to work hard to catch up with these guys. Ha!