Pages: [1]   Go Down
Author Topic: 4 Ping sensor Question  (Read 2134 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Code:
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 smiley
Logged

Phoenix, Arizona USA
Offline Offline
Faraday Member
**
Karma: 39
Posts: 5557
Where's the beer?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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...

 smiley
Logged

I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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.
Logged

Phoenix, Arizona USA
Offline Offline
Faraday Member
**
Karma: 39
Posts: 5557
Where's the beer?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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!):

Code:
#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.

 smiley-wink
« Last Edit: January 21, 2011, 04:25:00 pm by keeper63@cox.net » Logged

I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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-grin
Logged

Pages: [1]   Go Up
Jump to: