I have one arduino with two proximity sensors that send two digitalWrite signals if an object is detected. I checked to make sure the output of these is working properly and only sending high signals when they should. I have a second arduino that is wired directly from those two outputs as two digitalReads and printing whether or not they are high or low.
The problem is that even though the first arduino is not sending a high signal, the second arduino's digitalReads are seeing high. I tried putting a 1k or 10k resistor in between and that just seemed to decrease the rate of this happening, but it also decreased the rate of seeing the high signal when there actually was one.
Anybody know what I'm doing wrong?
Arduino 2 (sensors) code:
/* Two ultrasonic sensors for collision detection
Angled on front sides
Checks for object and sends digital high if distance is less than set threshold
(ran out of pwm pins on main arduino)
*/
#define LEFT_INPUT 5
#define RIGHT_INPUT 6
#define LEFT_OUTPUT 8
#define RIGHT_OUTPUT 9
// distance to stop
int stopThreshold = 24; // ~10cm
// vars
long ultraLeft, inchesLeft, cmLeft;
long ultraRight, inchesRight, cmRight;
int objectDetectedLeft, objectDetectedRight;
void Poll_Ultrasonic()
{
// left
ultraLeft = pulseIn(LEFT_INPUT, HIGH);
inchesLeft = ultraLeft / 147;
cmLeft = inchesLeft * 2.54;
if (cmLeft < stopThreshold)
objectDetectedLeft = 1;
else
objectDetectedLeft = 0;
// right
ultraRight = pulseIn(RIGHT_INPUT, HIGH);
inchesRight = ultraRight / 147;
cmRight = inchesRight * 2.54;
if (cmRight < stopThreshold)
objectDetectedRight = 1;
else
objectDetectedRight = 0;
}
void setup()
{
// Serial monitor
Serial.begin(9600);
// pins
pinMode(LEFT_INPUT, INPUT);
pinMode(RIGHT_INPUT, INPUT);
pinMode(LEFT_OUTPUT, OUTPUT);
pinMode(RIGHT_OUTPUT, OUTPUT);
}
void loop()
{
// check distances, set flags
Poll_Ultrasonic();
// set output pins high on detection, print detection messages
if (objectDetectedLeft) {
digitalWrite(LEFT_OUTPUT, HIGH);
Serial.println("\n LEFT object detected!\n");
}
else
digitalWrite(LEFT_OUTPUT, LOW);
if (objectDetectedRight) {
digitalWrite(RIGHT_OUTPUT, HIGH);
Serial.println("\n RIGHT object detected!\n");
}
else
digitalWrite(RIGHT_OUTPUT, LOW);
// print distances
Serial.print("Left ultrasonic distance: ");
Serial.print(cmLeft);
Serial.println("cm");
Serial.print("Right ultrasonic distance: ");
Serial.print(cmRight);
Serial.println("cm");
}
Arduino 1 (main) code snippet:
// Ultrasonic pins
#define ULTRA_LEFT 7
#define ULTRA_RIGHT 11
// Ultrasonics
pinMode(ULTRA_LEFT, INPUT);
pinMode(ULTRA_RIGHT, INPUT);
void Poll_Ultrasonic()
{
if(digitalRead(ULTRA_LEFT) && digitalRead(ULTRA_RIGHT))
leftOrRight = 2;
else if (digitalRead(ULTRA_LEFT))
leftOrRight = -1;
else if (digitalRead(ULTRA_RIGHT))
leftOrRight = 1;
else
leftOrRight = 0;
if (leftOrRight)
objectDetected = 1;
else
objectDetected = 0;
}
...
void StopCar()
{
esc.write(90);
if (leftOrRight == -1)
Serial.println("Object detected on LEFT! Stopping.");
else if (leftOrRight == 1)
Serial.println("Object detected on RIGHT! Stopping.");
else if (leftOrRight == 2)
Serial.println("Object detected on BOTH! Stopping.");
else // should never reach this
Serial.println("Ultrasonic debug statement: bad condition");
}