Hi guys,
I'm also experimenting on the subject of 2 sensors with 1 servo.
I have the 4 pin sensors (VCC, TRIG, Echo, GND).
The first one worked great, by seeing if the distance is < Xcm and then turning the motor if something is close.
When I connected a second sensor, I thickened the code to add all it's parameters, and said that:
if ((cm < 10) && (cm2 < 10)) {
myservo.write(90);
Serial.println(cm);
Serial.println(" from #1 sensor"); //display the distance from sensor
Serial.println(cm2);
Serial.println(" from #2 sensor");
delay(250);
}
else {
myservo.write(0);
Serial.println("Too Far");
delay(250);
}
What gets printed in the serial monitor is something like this:
Too Far
Too Far
8
from #1 sensor
0
from #2 sensor
7
from #1 sensor
0
from #2 sensor
4
from #1 sensor
0
from #2 sensor
Too Far
Too Far
The above situation happens when I place objects in front of BOTH sensors. So #1 gets distance, #2 doesn't.
So basically it's telling me that something is wrong with the input from the #2 sensor, because the cm2 variable is 0, so it doesn't pick up any distance at all.
I don't want to paste the whole code here because I think it might be the wiring, not the code.
Please take a look at the pic and tell me if something is wrong.
Take note of the Black and Yellow wires. Are they correct?
I hope you can understand the drawing. Just in case - the 2 sensor (I don't have the image), are in opposite order of the input pins, so the 4 right-most rows represent each one, where the sticker is.
Thanks!