0

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?

Connections: enter image description here

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");
}
  • 1
    On the second arduino, are you switching the pins to input mode using pinMode(); ? – jose can u c Nov 4 '16 at 15:01
  • 1
    I assume you have the grounds of the two arduinos connected together? – Andrew Nov 4 '16 at 15:08
  • 1
    I have the pinMode set correctly, but no I didn't connect the grounds together. When I do this is stops showing as high, but even when it's supposed to be high. – Austin Nov 4 '16 at 15:14
  • 1
    Can you show us the code in both arduino's? You say you set pinMode correctly, but you could set it to INPUT_PULLUP or just INPUT; the difference matters. – jose can u c Nov 4 '16 at 15:17
  • 3
    The grounds must be tied together. Please, show your schematic. – Edgar Bonet Nov 4 '16 at 15:18
-1

OK , start by connecting an LED at the Output of Arduino 2 pins .. ( hopefully it will glow )

Measure the current out of the output pins of Arduino 2 too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.