Ultrasonic Sensor Guidance

I am relatively new to programming and electronics and whatnot, and I need some help. I have tried searching this online but did not find results that suited my needs. What I am trying to do is to get five hc-sro4 ultrasonic sensors working together so that they can sense a range of 45 degrees. Each sensor works fine by themselves, but when I try to put them together, The arduino keeps on print ‘0’ back to the serial moniter after every five seconds or so. Any suggestions?



Here is the code

 HC-SR04 Ping distance sensor]
 VCC to arduino 5v GND to arduino GND
 Echo to Arduino pin 8 Trig to Arduino pin 7
 Red POS to Arduino pin 6
 Green POS to Arduino pin 6
 560 ohm resistor to both LED NEG and GRD power rail
 More info at: http://goo.gl/kJ8Gl
 Original code improvements to the Ping sketch sourced from Trollmaker.com
 Some code and wiring inspired by http://en.wikiversity.org/wiki/User:Dstaub/robotcar

#define trigPin1 12
#define echoPin1 11
#define trigPin2 10
#define echoPin2 9
#define trigPin3 8
#define echoPin3 7
#define trigPin4 6
#define echoPin4 5
#define trigPin5 4
#define echoPin5 3
#define led 13
#define led2 2
#define led3 1
#define led4 0
void setup() {
  Serial.begin (9600);
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  pinMode(trigPin3, OUTPUT);
  pinMode(echoPin3, INPUT);
  pinMode(trigPin4, OUTPUT);
  pinMode(echoPin4, INPUT);
  pinMode(trigPin5, OUTPUT);
  pinMode(echoPin5, INPUT);
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);

void loop() {
  digitalWrite(trigPin1, LOW);
  digitalWrite(trigPin1, HIGH);
  digitalWrite(trigPin1, LOW);
  int distance1 = detect(trigPin1, trigPin2, trigPin3, trigPin4, trigPin5);
 digitalWrite(trigPin2, LOW);
  digitalWrite(trigPin2, HIGH);
  digitalWrite(trigPin2, LOW);

    int distance2 = detect(trigPin1, trigPin2, trigPin3, trigPin4, trigPin5);
  digitalWrite(trigPin3, LOW);
  digitalWrite(trigPin3, HIGH);
  digitalWrite(trigPin3, LOW);
  int distance3 = detect(trigPin1, trigPin2, trigPin3, trigPin4, trigPin5);

  Serial.print(distance1 + distance2 + distance3/3);
    Serial.println(" cm");

int detect(int x, int y, int z, int a, int b)
long du1;
int di1;
long du2;
int di2;
long du3;
int di3;
long du4;
int di4;
long du5;
int di5;
  du1 = pulseIn(x, HIGH);
  di1 = (du1/2) / 29.1;
  du2 = pulseIn(y, HIGH);
  di2 = (du2/2) / 29.1;
  du3 = pulseIn(z, HIGH);
  di3 = (du3/2) / 29.1;
  du4 = pulseIn(a, HIGH);
  di4 = (du4/2) / 29.1;
  du5 = pulseIn(b, HIGH);
  di5 = (du5/2) / 29.1;
  int q = low(di1, di2, di3, di4, di5);
  return q;

int low(int a, int b, int c, int d, int e){
  if(a<b && a<c && a<d && a<e){
    return a;
    if(b<e && b<c && b<d && b<a){
    return b;
      if(c<b && c<a && c<d && c<e){
    return a;
        if(d<b && d<c && d<a && d<e){
    return a;
    return e;


Serial.print(distance1 + distance2 + distance3/3);

First off, do the calculation outside the Serial.Print function.

Thanks for the advice, but that doesn't change much and I feel there are many more things that are wrong.



Any suggestions?

Use the CODE TAG "#" button. (click MODIFY button, highlight code ,click code tag "#" button, click SAVE.)

Is led 3 and 4 connected to the digital pins 1 and 0 or A1 an A0? If yours using the serial monitor then you shouldn’t have anything in the digital pins 1 and 0, otherwise you will get unwanted results in the serial monitor.

They were connected earlier but they are not connected now.

It doesn’t matter if they are physically connected or not, your code is setting those pins as outputs.

I fixed that but it didn't change anything...

Perhaps, divide and conquer is the way to go. Simplify the code make it work then ad more code, etc.

As far as I can see you're sending all the trigger pulses in loop() and then calling pulseIn() for each sensor in detect(). That's not going to work. Firstly there's a good chance that each sensor will detect echoes from the other sensors so you need to wait briefly after each ping before you try the next one. Secondly, as coded the call to pulseIn for the first sensor will block your code until it receives the echo (or times out) before the second call will be executed. Obviously since all the pings were triggered together, any echoes received by the second and subsequent sensors will have been long gone by the time you get round to calling pulseIn for that sensor. Since pulseIn waits until it receives a complete pulse or times out, and it has missed the pulse, you will be left hanging while it times out.

You need to restructure your code so that it sends each ping and receives the response, then waits briefly for any echoes to dissipate, then does the same for the next sensor, and so on. I suggest that since you have multiple sensors, it would make sense to write a function that does this for a single sensor, put your pin numbers in an array and use a FOR loop to perform a ping for each entry in the array.

I suspect you will end up replacing this code, but I don't think it does what you want.

distance1 + distance2 + distance3/3

You need to ensure that the addition is performed before the division:

(distance1 + distance2 + distance3)/3

Good advice LarryD. I'm going to call it a night for tonight because I want to get at least 7 hours of sleep (I live on the East Coast). With your help, I can get this done and no time. Thanks for the advice everyone. I'm learning bit by bit (no pun intended).

In my code, I pulse in all of the sensors after sending a pulse out on one. I do this in case there is an angle that the sensor that sent out the pulse wouldn't recieve but the others do. I now realize that is pointless.