Go Down

Topic: multiple ping Sensors (Read 611 times) previous topic - next topic

newbie22

Hi, I'm a beginner programmer with little knowledge regarding computer programming XD
I wanted to make 3 ping sensors read the values in inches
and through the guidebook, I could make one ping sensor work
I wanted to implement this to a servo so if the ping sensor reads someone, the servo will respond
because this was basic, it worked ;)
now i want to make 3 ping sensors to work and read the values separately
However, I couldn't think of any way to code this project and i'm in lost
I would appreciate any help to how I can code this from any experienced programmer
thanx in advance 8 :smiley-sweat:

johnwasser

You need to use a different pin to connect to each sensor.  Then you duplicate the part of the code that reads from the sensor and change the pin number.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

newbie22

Code: [Select]


#include <Servo.h>
Servo LFTleg;
Servo RGTleg;
Servo Tail;
Servo Frontpaw;

const int buttonPin = 2;
int pingPin = 7;
int pingPin2 = 12;
int pingPin3 = 4;
const int ledPin = 13;
int speakerPin = 3;
int buttonState = 0;
int length = 15;
char notes[] = "ccggaagffeeddc";
int beats [] = {1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 300;

void playTone(int tone, int duration) {
  for (long i = 0; i < duration * 1000L; i += tone * 2);
digitalWrite(speakerPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(tone);

}

void playNote(char note, int duration) {
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  int tones[] = { 1519, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
 
   for (int i = 0; i < 8; i++) {
    if (names[i] == note) {
      playTone(tones[i], duration);
    }
  }
}
 
  // play the tone corresponding to the note name



void setup()
{
  Serial.begin(115200);
  pinMode (ledPin, OUTPUT);
  LFTleg.attach(9);
  RGTleg.attach(10);
  Tail.attach(11);
  Frontpaw.attach(6);
  pinMode(speakerPin, OUTPUT);
pinMode(buttonPin, INPUT);
}


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(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
 
  pinMode(pingPin2, OUTPUT);
  digitalWrite(pingPin2, LOW);
  delayMicroseconds(2);
 
  pinMode(pingPin3, OUTPUT);
  digitalWrite(pingPin3, LOW);
  delayMicroseconds(2);
 
 
 
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  digitalWrite(pingPin2, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin2, LOW);
 
  digitalWrite(pingPin3, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin3, 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(pingPin, INPUT);
  pinMode(pingPin2, INPUT);
  pinMode(pingPin3, INPUT);
 
  duration = pulseIn(pingPin, HIGH);
  duration = pulseIn)pingPin2, HIGH);
  duration = pulseIn)pingPin3, 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();
 
 
  if (inches <= 60) {
    digitalWrite (speakerPin, LOW);
    digitalWrite (ledPin, HIGH);
    LFTleg.write(50);
    RGTleg.write(50);
    Tail.write(50);
    Frontpaw.write(90);
  }
   {
   buttonState = digitalRead(buttonPin);
   if (buttonState == HIGH) {         
    LFTleg.write(110);
    RGTleg.write(110);
    Frontpaw.write(80);
    delay(3000);

   }
  else {
    digitalWrite (speakerPin, HIGH);
    for (int i = 0; i < length; i++)
      playNote(notes[i], beats[i] * tempo);
    delay(tempo / 2);
    digitalWrite (ledPin, LOW);
    LFTleg.write(90);
    RGTleg.write(90);
    Tail.write(90);
    Frontpaw.write(70);
  }

}}

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 / 73.746 / 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 did what you said but I'm still stuck on the
void loop


if i wanted to set 3 ping sensors to work seperately like

if pingsensor 2  === read object
digital.write XXX

and if pinsensor 3 === read object
digital.write yyy

Sorry for my bad explanation =(

PeterH

#3
Mar 14, 2013, 03:46 am Last Edit: Mar 14, 2013, 03:48 am by PeterH Reason: 1
You need to do the whole ping sequence for one sensor before you do it for the next sensor. Given that you're going through essentially the same sequence for each sensor, I suggest you put that code in a function and pass in the pin number as an argument.

Code: [Select]


long ping(int pinNumber)
{
  pinMode(pinNumber, OUTPUT);
  digitalWrite(pinNumber, LOW);
  delayMicroseconds(2);
 
  digitalWrite(pinNumber, HIGH);
  delayMicroseconds(5);
  digitalWrite(pinNumber, LOW);

  pinMode(pinNumber, INPUT);
 
  long duration = pulseIn(pinNumber, HIGH);

 long inches = microsecondsToInches(duration);
 return inches;
}


Then you would just need to call that function once for each sensor.
I only provide help via the forum - please do not contact me for private consultancy.

johnwasser

You have to complete one ping before moving on to the next.  Something like this:
Code: [Select]

void loop()
{
  unsigned long duration;
  int inches1, inches2, inches3;

  // Read distance 1
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  inches1 = microsecondsToInches(duration);

  // Read distance 2
  pinMode(pingPin2, OUTPUT);
  digitalWrite(pingPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin2, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin2, LOW);
  pinMode(pingPin2, INPUT);
  duration = pulseIn(pingPin2, HIGH);
  inches1 = microsecondsToInches(duration);
 
 
  // Read distance 3
  pinMode(pingPin3, OUTPUT);
  digitalWrite(pingPin3, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin3, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin3, LOW);
  pinMode(pingPin3, INPUT);
  duration = pulseIn(pingPin3, HIGH);
  inches3 = microsecondsToInches(duration);
 
  if (inches1 <= 60) {
    digitalWrite (speakerPin, LOW);
    digitalWrite (ledPin, HIGH);
    LFTleg.write(50);
    RGTleg.write(50);
    Tail.write(50);
    Frontpaw.write(90);
  }
   {
   buttonState = digitalRead(buttonPin);
   if (buttonState == HIGH) {         
    LFTleg.write(110);
    RGTleg.write(110);
    Frontpaw.write(80);
    delay(3000);

   }
  else {
    digitalWrite (speakerPin, HIGH);
    for (int i = 0; i < length; i++)
      playNote(notes[i], beats[i] * tempo);
    delay(tempo / 2);
    digitalWrite (ledPin, LOW);
    LFTleg.write(90);
    RGTleg.write(90);
    Tail.write(90);
    Frontpaw.write(70);
  }

}}
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

terryking228

Sounds like a job for NEWPING!  See: https://code.google.com/p/arduino-new-ping/

He shows an example of running 15 ultrasonic sensors at once...

Quote
This example code was used to successfully communicate with 15 ultrasonic sensors. You can adjust the number of sensors in your project by changing SONAR_NUM and the number of NewPing objects in the  "sonar" array.
Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

HazardsMind

#6
Mar 14, 2013, 03:59 pm Last Edit: Mar 14, 2013, 04:01 pm by HazardsMind Reason: 1
Something here in particular is wrong.
Quote
duration = pulseIn)pingPin2, HIGH);
duration = pulseIn)pingPin3, HIGH);

Not that you need it anymore at this point, but it is good to check over your code.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

newbie22

wow, THANK YOU SOOO MUCH!!
Much appreciated

Go Up