Issues with Ultrasonic Sensors - One out of three isn't reading

I am working on a project for a vision walker. We can get the left and center Ultrasonic sensors to work, but the right won't record any distance or duration (checked via Serial Monitor). We replaced the wires, checked over it many times, etc. but we can't get the right one to work. The center one is duration1, the left is duration2, and the right is duration3. I've attached a schematic, guide to the pins (as well as a diagram that shows which sensor is which number), and a pdf of the code.
Any help would be much appreciated!
Note: it should be an issue with the first 50 lines of code (posted below), as that's where it should tell us the measured distance of the third sensor. I've posted the full code in an attachment.

// #define pinName ArduinoPinNumber
#include "SoftwareSerial.h"
SoftwareSerial mySerial(10, 11);
# define echoPin1 2
# define trigPin1 3
# define motorPin1 4
#define echoPin2 5
#define motorPin2 6
#define echoPin3 7
#define motorPin3 8
long duration1;
int distance1;
long duration2;
int distance2;
long duration3;
int distance3;

void setup() {
  // put your setup code here, to run once:
  pinMode(trigPin1, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin1, INPUT); // Sets the echoPin as an Input
  pinMode(echoPin2, INPUT);
  pinMode(echoPin3, INPUT);
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(trigPin1, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin1, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration1 = pulseIn(echoPin1, HIGH);
  duration2 = pulseIn(echoPin2, HIGH);
  duration3 = pulseIn(echoPin3, HIGH);
  // Calculating the distance
  distance1= duration1*0.0343/2;
  distance2= duration2*0.0343/2;
  distance3= duration3*0.0343/2;
  //Serial.println(distance3);
  Serial.println(duration3);
}

ultrasonic_3_vibration_3.pdf (74.6 KB)

I am working on a project for a vision walker.

Whatever that is.

Please post your code.
In code tags.

Hint: Ultrasound isn't choosy. A ping from one transmitter can be received by any number of receivers (if they've been told to listen...) so it is best to ping only one at a time and at rate of no more than about 20 - 30Hz, so if you've got three, each one should ping at no more than 10Hz.

Also, that's not a schematic. What are the white circles?

Read the advice like "How to use this forum" and how to attache code, diagrams and info. No way I'll sit and download Your files.

I plucked-up courage, and downloaded your PDF (WT..?).

You'll notice a ton of decimal distances, but all the distance variables are integers.
Fix that.

You'll notice a ton of delays.
Fix that too.

You'll notice that at the top of loop, you trigger one sensor, but expect to read echoes from all three.
Fix that too.

You've got way too much code, which I suspect has been written without the basics of "ping a sensor, read a range" having been tested.

The duration and distance variables do not need global scope.
Write ONE function to ping ONE ultrasound sensor, and return ONE range value.

See where this is going?

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Have you looked at the NewPing library to read your sensors?
As @AWOl has said, you cannot trigger ALL the senors at once and guarantee that each sensor ONLY receives its reflection.
The usual thing is to activate and read each unit in turn.

google

using multiple hc-sr04

Thanks.. Tom... :slight_smile:

Thank you all for the help. I added a trigger for the other two ultrasonic sensors, and that fixed the problem.

Answering some of TheMemberFormerlyKnownAsAWOL's comments:
I did test this code without the if loops, and it worked fine. However, I changed the orientation of the sensors and that's when the issues began.
The circles are vibration motors.
I think the if loops and delays are necessary. The if loops are meant to say "if an obstacle is within this range of the ultrasonic sensor, the corresponding vibration motor will vibrate for _, and then turn off for _." Is there an easier way to write this code?

float usRange (uint8_t trigPin, uint8_t echoPin)
{
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  uint32_t duration = pulseIn(echoPin, HIGH);
  return duration * 0.0343 / 2;
}

One function, one range per call (it also returns a float, so you all your comparisons with decimals will work as you expect)