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?
Thanks,
Joey
Here is the code
[code][code][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);
delayMicroseconds(0);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(0);
digitalWrite(trigPin1, LOW);
int distance1 = detect(trigPin1, trigPin2, trigPin3, trigPin4, trigPin5);
delay(10);
digitalWrite(trigPin2, LOW);
delayMicroseconds(0);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(0);
digitalWrite(trigPin2, LOW);
int distance2 = detect(trigPin1, trigPin2, trigPin3, trigPin4, trigPin5);
delay(10);
digitalWrite(trigPin3, LOW);
delayMicroseconds(0);
digitalWrite(trigPin3, HIGH);
delayMicroseconds(0);
digitalWrite(trigPin3, LOW);
int distance3 = detect(trigPin1, trigPin2, trigPin3, trigPin4, trigPin5);
delay(10);
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;
}else{
if(b<e && b<c && b<d && b<a){
return b;
}else{
if(c<b && c<a && c<d && c<e){
return a;
}else{
if(d<b && d<c && d<a && d<e){
return a;
}else{
return e;
}
}
}
}
}
[/code][/code][/code]