For my senior project I was instructed to design a collision avoidance system for a golf cart. After arguing with my professor about how LIDAR would be much more reliable, he is convinced that using these HC-SR04 sensors is the way to go due to budget. I'm prototyping the system using three of these sensors on a breadboard, but I think the sensors are interfering with each other. From left to right we have sensor #1, sensor #2, and sensor #3. When any of these sensors are programmed alone, they work great and exactly as I want them to. When sensor #2 is programmed in with either of the others, I get a 0in return. When sensor #1 and sensor #3 are programmed together (the two furthest apart), but only one returning information, it has a very long delay. They won't be mounted near this close on the golf cart, but will I still have issues?
I'm very new to Arduino and much of what I have done so far was taken from the internet and recoded to fit my needs. Here is my current code with most of sensor #2 programming commented out. If this code sucks, please go easy but I'm willing to listen to constructive criticism.
const int trigPin1 = 9;
const int echoPin1 = 8;
const int trigPin2 = 7;
const int echoPin2 = 6;
const int trigPin3 = 5;
const int echoPin3 = 4;
// constants won't change. Used here to set a pin number :
const int redLed = 3; // the number of the LED pin
const int yellowLed = 2;
const int greenLed = 10;
void setup() {
// initialize serial communication:
Serial.begin(9600);
pinMode(redLed, OUTPUT);
pinMode(yellowLed, OUTPUT);
pinMode(greenLed, OUTPUT);
}
void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration1, inches1, cm1;
long duration2, inches2, cm2;
long duration3, inches3, cm3;
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(trigPin1, OUTPUT);
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
/*pinMode(trigPin2, OUTPUT);
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
*/
pinMode(trigPin3, OUTPUT);
digitalWrite(trigPin3, LOW);
delayMicroseconds(2);
digitalWrite(trigPin3, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin3, LOW);
// Read the signal from the sensor: 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(echoPin1, INPUT);
duration1 = pulseIn(echoPin1, HIGH);
/*pinMode(echoPin2, INPUT);
duration2 = pulseIn(echoPin2, HIGH);
*/
pinMode(echoPin3, INPUT);
duration3 = pulseIn(echoPin3, HIGH);
// convert the time into a distance
inches1 = microsecondsToInches(duration1);
cm1 = microsecondsToCentimeters(duration1);
inches2 = microsecondsToInches(duration2);
cm2 = microsecondsToCentimeters(duration2);
inches3 = microsecondsToInches(duration3);
cm3 = microsecondsToCentimeters(duration3);
digitalWrite(greenLed, HIGH);
if (inches1 < 15){
digitalWrite(yellowLed, HIGH);
}
else {
digitalWrite(yellowLed, LOW);
}
if (inches1 < 7) {
digitalWrite(redLed, HIGH);
}
else {
digitalWrite(redLed, LOW);
}
Serial.print(inches1);
Serial.print("in, ");
Serial.print(cm1);
Serial.print("cm");
Serial.println();
delay(100);
}
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 / 74 / 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;
}