HC-SR04 Problem Within Project

I am working on an art project that incorporates three HC-SR04’s, one PIR sensor, one micro servo, and audio amp. I built two Bareduino boards, one to control the sensors and the power to the amp and one to loop a music file. The two boards, amp, and servo all end up in a box with an internet tablet and a speaker. The servo is used to bump the sleep button on the tablet. The HC-SR04’s are about 3’ below the box positioned 3" apart in a straight line, all facing the same direction. The PIR is about 10’ above the box, with the box being mounted around eye level.

I tested the project before installing it into the box and everything worked fine. I have now installed the project at a gallery and it’s giving me trouble. Unfortunately the gallery is closed today so I can not do any testing today but hope to get some ideas of where to proceed when I go back tomorrow.

The Goal:
Whenever motion is sensed or an object is within 36" the servo is supposed to turn the tablet off along with the music. I chose to have both types of sensors as I couldn’t reliably cover the entire space with rangefinders, given other object that are in space, and the motion sensor doesn’t do so well when people stand still. So having four sensors seems like it would supply an adequate amount of detection.

The Problem:
I plug in the system and wait for the 60 second warm up period. After that one of the sensors is immediately tripped and the music and tablet turn off(nothing was supposed to be detected). I then wait for the 60 second safeguard against people standing still. After this the music and tablet turn on and then right back off. It basically happens as fast as the servo can move. It makes the motion to turn off the tablet and then without pausing repeats the motion to turn it back off. The amp doesn’t even have enough time to fully turn on.

The problem seems to be the rangefinders. I uploaded a modified version of my code which excluded the rangefinders and it works perfectly. I then tried a code with only one rangefinder and it behaved just as it did with all three. I double checked all the wiring and it’s good. The boards that I built use 22ga wire that is soldered into the board for each of the input/outputs that I need. They were then wire nutted together with the sensors. The sensors use 20ga wire that is soldered to them and then brought into the box.

My code is below and it is almost identical to what I tested before install. I don’t think I changed much other than some delays and adding in the third HC-SR04. I’m using IDE 1.0.2 and the chips in the project are Atmega328p’s. Thanks in advance for your help and let me know if you’d like more information.

#include <Servo.h> 
#include <Ultrasonic.h>

Servo myservo; 

const int ECHO_PIN_LEFT = A5;
const int TRIGGER_PIN_LEFT = A4;
Ultrasonic ultrasonic_left(TRIGGER_PIN_LEFT, ECHO_PIN_LEFT);

const int ECHO_PIN_CENTER = A3;
const int TRIGGER_PIN_CENTER = A2;
Ultrasonic ultrasonic_center(TRIGGER_PIN_CENTER, ECHO_PIN_CENTER);

const int ECHO_PIN_RIGHT = A1;
const int TRIGGER_PIN_RIGHT = A0;
Ultrasonic ultrasonic_right(TRIGGER_PIN_RIGHT, ECHO_PIN_RIGHT);

int motion = 5;  //  This is the pin to attach the motion sensor
int music = 9;  //  This pin is soldered to the switch to control the amp power
 
int pos = 0;    // variable to store the servo position 
int Dist1 = 0;  // variable to store inMsec1 distance, rangefinder on left
int Dist2 = 0;  // variable to store inMsec2 distance, rangefinder on center
int Dist3 = 0;  // variable to store inMsec3 distance, rangefinder on right
int State_left = 0;  // variable for rangefinder left
int State_center = 0;  // variable for rangefinder center
int State_right = 0;  // variable for rangefinder right
int State_motion = 0;  // variable for motion sensor
int StateA = 0; // variable to store state of music and screen
int StateB = 0;  // additional variable to make things a little easier
int lastState = 0; // variable to store last reading from rangefinders, the tablet screen needs to be on before this program runs

// "1" means motion is detected "0" means no motion is detected


void setup() 
{ 
myservo.attach(1);  // attaches the servo on pin 1 

pinMode(motion, INPUT);
pinMode(music, OUTPUT);

delay(60000); //this delay allow the motion sensor to do its initial setup
} 
 
void loop() 
 {
  
float Dist1;
float Dist2;
float Dist3;
long microsec_left = ultrasonic_left.timing();
long microsec_center = ultrasonic_center.timing();
long microsec_right = ultrasonic_right.timing();


Dist1 = ultrasonic_left.convert(microsec_left, Ultrasonic::IN);
delay(10);
Dist2 = ultrasonic_center.convert(microsec_center, Ultrasonic::IN);  
delay(10);
Dist3 = ultrasonic_right.convert(microsec_right, Ultrasonic::IN);
  
if(Dist1 <= 36){   // enter distance for rangefinder left here in inches
  State_left = 1;
 } 
 else{
   State_left = 0;
 }
 
if(Dist2 <= 36){  // enter distance for rangefinder center here in inches
  State_center =1;
}
 else{
  State_center = 0;
 }
 
if(Dist3 <= 36){  // enter distance for rangefinder right here in inches
  State_right =1;
}
 else{
  State_right = 0;
 } 
 
if(digitalRead(motion) == HIGH){
  State_motion = 1;
}
else{
  State_motion = 0;
}
 
StateA = State_left + State_center + State_right + State_motion; //  adds up the states of each sensor

if(StateA >= 1){  // converts StateA, which can be 0,1,2,3, or 4, to StateB which can only be 0 or 1
  StateB = 1;
}
else{
  StateB = 0;
}
   
if(StateB == 1) {
    digitalWrite(music, HIGH);
  }
  else{
    digitalWrite(music, LOW);
  }     


if(StateB != lastState){

   for(pos = 80; pos > 52; pos -= 1)  // moves the servo into the sleep button on the tablet
  {                                  
    myservo.write(pos);              
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 52; pos < 80; pos +=1)     // moves the servo away from the sleep button 
  {                                
    myservo.write(pos);              
    delay(15);    // waits 15ms for the servo to reach the position 
  } 

}

if(StateB == 1){ 
  delay(60000);  //if motion was detected the program will wait 1 minute will it checks again for motion, this is incase they decide to stand still and are not close enough to the rangefinders to set them off
}

lastState = StateB; //  this stores whether or not motion was detected during the last measurment

 }

Perhaps the gallery has an ultrasonic alarm system that interferes with your distance sensors. Perhaps you could take several reading from each sensor and only keep the furthest distance or the average distance.

More likely, with only 10 milliseconds between readings, you're picking up echoes from previous pings.

Thanks for the responses. I was also thinking along the lines of an echo issue. I don't think there is an alarm issue but I'll definitely check. I'm going to try these solutions today and post back hopefully with some success.