How do i make it so that when an object is detected by 2 ultrasonic sensors a led turns on

Im working on a arduino project for school and I cant get it so that a led turns on when an object is detected by 2 ultrasonic sensors and another led light would light up if the object is detected by only 1 ultrasonic sensor. the problem is that the led for 2 ultrasonic sensors is turned on even with no object in front of either ultrasonic sensor. I have been searching for answers but I could not find any information on the internet.

// defines pins numbers
// ledpin1 is red, ledpin2 is green
const int trigPin = 10;
const int echoPin = 9;
const int ledPin = A4;
const int ledPin2 = A5;
const int ledPin3 = A3;
const int trigPin2 = 12;
const int echoPin2 = 11;

//remember to define led pin2

// defines variables
long duration;
long duration2;
int distance;
int distance2;
int safetyDistance;
int safetyDistance2;


void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(ledPin, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
Serial.begin(9600); // Starts the serial communication
}


void loop() {
  //SENSOR1
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;

safetyDistance = distance;

//SENSOR2
// Clears the trigpin2
delay(200);
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);

//Sets trigPin2 high for 10ms
digitalWrite(trigPin2, HIGH);
delayMicroseconds(2);
digitalWrite(trigPin2, LOW);

// Reads the echopin2
duration2 = pulseIn(echoPin2, HIGH);
//distance calculation
distance2= duration2 * 0.034/2;
if (safetyDistance <=30 || safetyDistance2 <=30)
{
  digitalWrite(ledPin3, HIGH);
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin, LOW);
}
  else{
    digitalWrite(ledPin2,HIGH);
    digitalWrite(ledPin, LOW);
    digitalWrite(ledPin3, LOW);
  }
  
}

It I understand your question correctly, you have 3 conditions:
detect either sensor being <=30
detect neither sensor being <=30
detect both sensors being <=30

Your code addresses only 2 of these conditions. You're not addressing detect both sensors being <=30

OHH, Yea i see it now thanks 2112!

It should already be clear :roll_eyes:

You're pinging too frequently, and risk distant, late returns being misinterpreted as nearby returns.

safetyDistance2 is always zero, I believe.
(I don't know why you've got a safetyDistance variable anyway)

const int trigPin = 2;
const int echoPin = 3;

void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2; // Calculate distance in centimeters
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(500); // Wait for a short period before taking the next measurement
}

It should already be low.

Please use code tags when posting code, however sloppy it is.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.