2 ultrasonic sensors. 4 led lights via arduino

Hello all. I'm doing a project where I have 2 ultrasonic sensors that will control led lights wherein if 1 sensor first detects a movement it will light up led1 while the other lights up led2 based on the distance of which one detects the nearest object first. For reference I'm trying to imitate this project but from the codes he posted I can only get the distance but cant make the LEDs behave the way they were described.

Edit: Heres a code I've managed to write up by referencing other people's work that had similarities with my project.

int trigPin1 = 4;
int echoPin1 = 3;
int trigPin2 = 5;
int echoPin2 = 6;
int red1 = 13;
int red2 = 8;
int gre2 = 10;
int gre1 = 9;

long duration1, duration2;  
int distance1, distance2; 


void setup() {
  // put your setup code here, to run once:
pinMode (trigPin1, OUTPUT);
pinMode (echoPin1, INPUT);
pinMode (trigPin2, OUTPUT);
pinMode (echoPin2, INPUT);
pinMode (red1, OUTPUT);
pinMode (red2, OUTPUT);
pinMode (gre2, OUTPUT);
pinMode (gre1, OUTPUT);

Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:
 /*==================================
   *    Ultrasonic Sensor#1
   ==================================*/
    //clear trigPin1
  digitalWrite(trigPin1, LOW);  
  delayMicroseconds(2);          

  //set TRIGGER_PIN1 to High for 10 microseconds
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin1, LOW);

  //read the duration of the sound wave with ECHO_PIN1
  duration1 = pulseIn(echoPin1, HIGH);

  //calculate distance based on duration of ultrasound from triggerPin to echoPin
  distance1 = duration1*0.034/2;

  //add thresholds to correct for sensor value errors
 if (distance1 <= 100 )
 {
  digitalWrite ( gre1, HIGH);
  digitalWrite ( red2, HIGH);
  digitalWrite (gre2, LOW);
  digitalWrite (red1, LOW);
 } else
 {
  digitalWrite ( gre1, LOW);
  digitalWrite ( red2, LOW);
  digitalWrite ( gre2, LOW);
  digitalWrite ( red1, LOW);
 }
 /*==================================
   *    Ultrasonic Sensor#2
   ==================================*/
  //clear TRIGGER_PIN2 value, setting it to LOW for 2 microseconds
  digitalWrite(trigPin2, LOW);  
  delayMicroseconds(2);          

  //set TRIGGER_PIN2 to High for 10 microseconds
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin2, LOW);

  //read the duration of the sound wave with ECHO_PIN1
  duration2 = pulseIn(echoPin2, HIGH);

  //calculate distance based on duration of ultrasound from triggerPin to echoPin
  distance2 = duration2*0.034/2;

  //add thresholds to correct for sensor value errors
  if (distance2 <= 100 )
 {
  digitalWrite (gre2, HIGH);
  digitalWrite (red1, HIGH);
  digitalWrite (gre1, LOW);
  digitalWrite (red2, LOW);
 }
 else
 {
  digitalWrite ( gre1, LOW);
  digitalWrite ( red2, LOW);
  digitalWrite ( gre2, LOW);
  digitalWrite ( red1, LOW);
 }
   /*==================================
   *    Print to Serial Monitor
   ==================================*/
  
  Serial.print("distance1: ");
  Serial.print(distance1);
  Serial.println("cm");
  Serial.print("distance2: ");
  Serial.print(distance2);
  Serial.println("cm ");
  delay(500);
}

Please post your code here, in code tags.

Update the post with the codes I rummage around.

Are the ultrasonic sensors in the same room?

my idea was to put them on to each side of a corner wherein if it detects a moving object on one side it will light up a green that signals it to go ahead while on the other path it will signal orange to slow down and vice versa.

That doesn't really answer my question.

Nevermind.

Because I'm not gonna put it in a room. Thanks anyway

Why not tell us what actual distances you are seeing on the serial print screen?
What is wrong with them that your test to set the LEDs is not working right? What needs to be changed?

Paul

OK, so how do you determine that an echo received by, let's say, device one isn't from it's most recent ping but is a distant echo from the previous ping from device two?

The code, as I see, it is missing an opening brace around the else statements.

1 Like

Hi,
Can I suggest you dump your existing code for the moment.
AND
Write some code that just uses ONE ultrasonic unit, NOTHING else.
Have it output the result on the IDE serial monitor.

Can I suggest you look at the NewPing library, it will make most of the ultrasonic code so much more cleaner.
How is this code for getting a reading.
This is the NewPing example code;

// ---------------------------------------------------------------------------
// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------

#include <NewPing.h>

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
  delay(50);                     // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  Serial.print("Ping: ");
  Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");
}

There is even code for more than one sensor.

Can you post link to specs/data of your sensor, I suspect its a HC-SR04.
HC-SR04

Tom... :smiley: :+1: :coffee: :australia:

Yep I am using HC-SR04. I have managed to make it work and get accurate distance with each sensor and have the LEDs respond. Only concern left is my GRE1 and RED2, which are the LEDs concerning sensor 1 are blinking when detecting the distance I want instead of staying on high. LEDs connected for sensor2 are working as I want them to behave.

omg thank you for catching that. I'm really blind sometimes.

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