Go Down

Topic: interfacing of multiple ultrasonic sensors at same time (Read 193 times) previous topic - next topic

hemanthmp07

i am working on a project using three ultrasonic sensors(HC-SR04) .But the problem is i cant get the value of multiple ultrasonic sensors at same time.Only one sensor is reading output i want to check the value of two, three sensors at same time. Here the program shown below .i hope someone will help me with it thank you...!!

/* ForNext Labs
   BLIND CANE SENSOR*/
   
#include <Ultrasonic.h>

#define trig1  8
#define echo1  9
#define trig2  10
#define echo2  11
#define trig3  12
#define echo3  13

#define playR 1
#define playF 2
#define playL 3
#define playRF 4
#define playRL 5
#define playFL 6
#define playRFL 7

Ultrasonic ultrasonic1(trig1, echo1);
Ultrasonic ultrasonic2(trig2, echo2);
Ultrasonic ultrasonic3(trig3, echo3);

void setup()
{

  Serial.begin(9600); // begin serial communitication 
  Serial.println("Initialising..!!");
  pinMode(trig1, OUTPUT);// set the trig pin to output (Send sound waves)
  pinMode(echo1, INPUT);// set the echo pin to input (recieve sound waves)
  pinMode(trig2, OUTPUT);// set the trig pin to output (Send sound waves)
  pinMode(echo2, INPUT);// set the echo pin to input (recieve sound waves)
  pinMode(trig3, OUTPUT);// set the trig pin to output (Send sound waves)
  pinMode(echo3, INPUT);// set the echo pin to input (recieve sound waves)
 
}

void loop()
{
  long duration1, distance1; // start the scan
  digitalWrite(trig1, LOW); 
  delayMicroseconds(2); // delays are required for a succesful sensor operation.
  digitalWrite(trig1, HIGH); 
  delayMicroseconds(10); //this delay is required as well!
  digitalWrite(trig1, LOW);
  duration1 = pulseIn(echo1, HIGH);
  distance1 = (duration1/2) / 29.1;// convert the distance to centimeters.

  long duration2, distance2; // start the scan
  digitalWrite(trig2, LOW); 
  delayMicroseconds(2); // delays are required for a succesful sensor operation.
  digitalWrite(trig2, HIGH); 
  delayMicroseconds(10); //this delay is required as well!
  digitalWrite(trig2, LOW);
  duration2 = pulseIn(echo2, HIGH);
  distance2 = (duration2/2) / 29.1;  // convert the distance to centimeters.

  long duration3, distance3; // start the scan
  digitalWrite(trig3, LOW); 
  delayMicroseconds(2); // delays are required for a succesful sensor operation.
  digitalWrite(trig3, HIGH); 
  delayMicroseconds(10); //this delay is required as well!
  digitalWrite(trig3, LOW);
  duration3 = pulseIn(echo3, HIGH);
  distance3 = (duration3/2) / 29.1; // convert the distance to centimeters.

  //**CONDITION FOR GUIDANCE**
 
  if (distance1 < 30)/*if there's an obstacle in right do the following: */
  {
       Serial.println("RIGHT");
       Serial.print ("Distance From RIGHT is " );
       Serial.print ( distance1);
       Serial.print ( " CM!");// print out the distance in centimeters.

        digitalWrite(playR,HIGH);
       
   delay(400);
  }

   
  else if (distance2 < 30)/*if there's an obstacle in front do the following: */
  {
       Serial.println("FRONT");
       Serial.print ("Distance From FRONT is " );
       Serial.print ( distance2);
       Serial.print ( " CM!"); // print out the distance in centimeters.

       digitalWrite(playF,HIGH);
       
   delay(400);
  }

   
  else if (distance3 < 30)/*if there's an obstacle in left do the following: */
  {
      Serial.println("LEFT");
      Serial.print ("Distance From LEFT is " );
      Serial.print ( distance3);
      Serial.print ( " CM!");// print out the distance in centimeters.

        digitalWrite(playL,HIGH);
       
   delay(400);   
  }
 
  else if (distance1 < 30 && distance2 < 30)/*if there's an obstacle in right&front do the following: */
  {
       Serial.println("RIGHT & FRONT");
       Serial.print ("Distance From RIGHT&FRONT is " );
       Serial.print ( distance2);
       Serial.print ( distance1);
       Serial.print ( " CM!");// print out the distance in centimeters.

        digitalWrite(playRF,HIGH);
       
   delay(400);
  }

   else if (distance1 < 30 && distance3 < 30)/*if there's an obstacle in right&LEFT do the following: */
  {
       Serial.println("RIGHT & LEFT");
       Serial.print ("Distance From RIGHT&LEFT is " );
       Serial.print ( distance1);
       Serial.print ( distance3);
       Serial.print ( " CM!");// print out the distance in centimeters.

        digitalWrite(playRL,HIGH);
       
   delay(400);
  }

   else if (distance2 < 30 && distance3 < 30)/*if there's an obstacle in left&front do the following: */
  {
       Serial.println("LEFT & FRONT");
       Serial.print ("Distance From LEFT&FRONT is " );
       Serial.print ( distance2);
       Serial.print ( distance3);
       Serial.print ( " CM!");// print out the distance in centimeters.

       digitalWrite(playFL,HIGH);
       
   delay(400);
  }

  else if (distance1 < 30 && distance2 < 30 && distance3 < 30)/*if there's an obstacle in right&left&front do the following: */
  {
       Serial.println("RIGHT & LEFT &Back");
       Serial.print ("Distance From RIGHT&LEFT is " );
       Serial.print ( distance1);
       Serial.print ( distance2);
       Serial.print ( distance3);
       Serial.print ( " CM!");  // print out the distance in centimeters.

     digitalWrite(playRFL,HIGH);
       
   delay(400);
  }
   
  else
  {
  Serial.println("walk");
  }
}


jremington

Please edit your post and add code tags ("</>" button). See "How to use this forum".

You usually can't use more than one ultrasonic ranger at once, because they interfere with each other.

DaveEvans

Did you try Google?  Up at the top of 1000s of hits is mention of the NewPing lib: it supports multiple sensors.  They're not used simultaneously...it cycles through them. But, maybe it is good enough for you.

Merlin513

I agree with the previous posters.  You have to cycle through the sensors.  In my obstacle avoidance rover Iuse five sensors that I read into an array and do the range test similar to you based on array element corresponding to the sensor platoon.  Hope this helps

hemanthmp07


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy