Hi Guys just starting here
I am making an R2D2 with arduino
my idea is to put on it 4 ultrasonic sensor hc-sr04
I got the code for one sensor and I am modifying it for 2 sensor
as result the LEFT sensor works and the Right sensor not.
Can you help please?
Thanks
the code:
/*
HC-SR04 Ping distance sensor:
VCC to arduino 5v
GND to arduino GND
Echo to Arduino pin 7
Trig to Arduino pin 8
This sketch originates from Virtualmix: TrollMaker.com is for sale | HugeDomains
Has been modified by Winkle ink here: Winkleink - box of wires: Arduino - HC-SR04 ultrasonic distance sensor
And modified further by ScottC here: http://arduinobasics.blogspot.com/
on 10 Nov 2012.
*/
//sensor lx
#define echoPin1 7 // Echo Pin lx
#define trigPin1 6 // Trigger Pin lx
//sensor rx
#define echoPin2 5 // Echo Pin dx
#define trigPin2 4 // Trigger Pin dx
#define LEDPin 13 // Onboard LED
int maximumRangelx = 300; // Maximum range needed
int minimumRangelx = 0; // Minimum range needed
long durationlx, distancelx; // Duration used to calculate distance
int maximumRangerx = 300; // Maximum range needed
int minimumRangerx = 0; // Minimum range needed
long durationrx, distancerx; // Duration used to calculate distance
void setup() {
Serial.begin (9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}
void loop() {
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
digitalWrite(trigPin2, LOW);
durationlx = pulseIn(echoPin1, HIGH);
durationrx = pulseIn(echoPin2, HIGH);
//Calculate the distance (in cm) based on the speed of sound.
distancelx = durationlx/58.2;
distancerx = durationrx/58.2;
if (distancelx >= maximumRangelx || distancelx <= minimumRangelx){
/* Send a negative number to computer and Turn LED ON
to indicate "out of range" */
Serial.print("LX -1");
digitalWrite(LEDPin, HIGH);
}
else {
/* Send the distance to the computer using Serial protocol, and
turn LED OFF to indicate successful reading. */
Serial.print(distancelx);
Serial.print( " cm");
// digitalWrite(LEDPin, LOW);
}
if (distancerx >= maximumRangerx || distancerx <= minimumRangerx){
/* Send a negative number to computer and Turn LED ON
to indicate "out of range" */
Serial.println(" RX -1");
// digitalWrite(LEDPin, HIGH);
}
else {
/* Send the distance to the computer using Serial protocol, and
turn LED OFF to indicate successful reading. */
Serial.print(distancerx);
Serial.print( " cm");
//digitalWrite(LEDPin, LOW);
}
//Delay 50ms before next reading.
delay(25);
}