4 Ping sensor Question

i have connected 4 ping sensor to my arduino uno and i want to know if any one can help me with a schetch for my 4 sensors were they show in the same serial monitor individuality the messuerments.

Serial.print(cm);
  Serial.print("cm");
  Serial.println();

this part is were the messuerment of 1 ping sensor is showed in 1 colum and i wanth 4 colum's with the data from all 4 ping sensors . Thank you :slight_smile:

I haven't tried this, but you could try:

Serial.print(cm1);
Serial.print("cm, ");
Serial.print(cm2);
Serial.print("cm, ");
Serial.print(cm3);
Serial.print("cm, ");
Serial.print(cm4);
Serial.print("cm");
Serial.println();

Where cm1-cm4 are the variables holding your measurements from each PING sensor...

:slight_smile:

This is something I have used before when using multiple ping sensors:

const int pingPin2 = 8;
const int pingPin8 = 12;

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

void loop()
{
  ping2();
  delay(250);
  ping8();
  delay(250);
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

void ping2()
{
  long duration2, cm2;
  pinMode(pingPin2, OUTPUT);
  digitalWrite(pingPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin2, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin2, LOW);
  pinMode(pingPin2, INPUT);
  duration2 = pulseIn(pingPin2, HIGH);
  cm2 = microsecondsToCentimeters(duration2);
  Serial.print("Front = ");
  Serial.print(cm2);
  Serial.print("cm");
  Serial.println();
}

void ping8()
{
  long duration8, cm8;
  pinMode(pingPin8, OUTPUT);
  digitalWrite(pingPin8, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin8, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin8, LOW);
  pinMode(pingPin8, INPUT);
  duration8 = pulseIn(pingPin8, HIGH);
  cm8 = microsecondsToCentimeters(duration8);
  Serial.print("Back = ");
  Serial.print(cm8);
  Serial.print("cm");
  Serial.println();
}

You will want to change it around to work for you but it should do what you want it to do.

JimmySticks2001:

Your code could be simplified and made a bit more readable as follows (note that I haven't tested this code in any manner - use at your own risk!):

#define FWD_PING 2
#define BCK_PING 8

long cm1 = 0;
long cm2 = 0;

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

void loop()
{
  cm1 = ping(FWD_PING);
  delay(5);

  cm2 = ping(BCK_PING);
  delay(5);

  Serial.print(cm1);
  Serial.print("cm, ");
  Serial.print(cm2);
  Serial.print("cm");
  Serial.println();
}

long ping(int pin)
{
  pinMode(pin, OUTPUT);
  digitalWrite(pin, LOW);
  delayMicroseconds(2);
  digitalWrite(pin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pin, LOW);
  pinMode(pin, INPUT);
  
  long duration = pulseIn(pin, HIGH);

  return (microsecondsToCentimeters(duration));
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

I don't think the 250 millisecond (1/4 second) delays are absolutely necessary; 5 milliseconds should suffice (and even that might be overkill). You could potentially do a ping, serial print, then ping another, serial print - and the serial prints would take care of any delay needed (if any is really needed).

Other than that, I made the pin definitions #defines, and created a single function for the ping action; potentially, the return values on the ping() and microsecondsToCentimeters() could be ints instead of longs, with some conversion/casting done to make sure of type (or something could be done with floats, so that you could get back fractional values).

I'm just posting these changes to allow others, and perhaps yourself, understand how by consolidation of functional definitions into as few functions as possible can lead to better code reuse, cleaner code, and a more maintainable structure overall.

:wink:

Thank you very much, i will try the code's to see wich one works the best and post the answer so maby other people don't have the same prb as me. Thank you again ! :smiley: