0

I'm new to Arduino and I currently have a project that involves the control of an LED via ultrasonic sensor. Basically, if an object passes through the ultrasonic sensor, the LED will be ON for 5 seconds but adding another set of LED and ultrasonic sensor with the same function gives a problem because the other LED won't light up if the other one is ON and vice versa because of the delay whereas what I want is to be able to make them both light up and stay lit for 5 seconds after their respective ultrasonic sensors detect an object. I've tried using the "Blink without delay" example but I can't understand it nor make it work. Please help me guys and thanks for reading. Here is my current code:

int trigPin1=2;
int echoPin1=3;
int trigPin2=4;
int echoPin2=5;
int led1=10;
int led2=11;
void setup() {
 Serial.begin(9600);
 pinMode(trigPin1, OUTPUT);
 pinMode(echoPin1, INPUT);
 pinMode(trigPin2, OUTPUT);
 pinMode(echoPin2, INPUT);
 pinMode(led1,OUTPUT);
 pinMode(led2,OUTPUT);
}
void loop() {
 long duration1, distance1;
 digitalWrite(trigPin1, LOW); // Added this line
 delayMicroseconds(2); // Added this line
 digitalWrite(trigPin1, HIGH);
 delayMicroseconds(10); // Added this line
 digitalWrite(trigPin1, LOW);
 duration1 = pulseIn(echoPin1, HIGH);
 distance1 = (duration1/2) / 29.1;
 if (distance1 >= 500 || distance1 <= 0){
 Serial.println("Out of range");
 }
 else {
 Serial.print ( "Sensor1 ");
 Serial.print ( distance1);
 Serial.println("cm");
 }
 long duration2, distance2;
 digitalWrite(trigPin2, LOW); // Added this line
 delayMicroseconds(2); // Added this line
 digitalWrite(trigPin2, HIGH);
 delayMicroseconds(10); // Added this line
 digitalWrite(trigPin2, LOW);
 duration2 = pulseIn(echoPin2, HIGH);
 distance2= (duration2/2) / 29.1;
 if (distance2 >= 500 || distance2 <= 0){
 Serial.println("Out of range");
 }
 else {
 Serial.print("Sensor2 ");
 Serial.print(distance2);
 Serial.println("cm"); 
 }
 if(distance1>20 || distance2>20){
 digitalWrite(led1,LOW);
 digitalWrite(led2,LOW);
 }
 else if(distance1<20){
 digitalWrite(led1,HIGH);
 delay(5000);
 }
 else if(distance2<20){
 digitalWrite(led2,HIGH);
 delay(5000);
 }
}
asked Feb 18, 2018 at 11:21

1 Answer 1

2

The first set of delaymicroseconds for measuring is fine.

you'll want to replace the delay(5000) when switching the leds.

 if(distance1>20 || distance2>20){
 digitalWrite(led1,LOW);
 digitalWrite(led2,LOW);
 }
 else if(distance1<20){
 digitalWrite(led1,HIGH);
 delay(5000);
 }
 else if(distance2<20){
 digitalWrite(led2,HIGH);
 delay(5000);
 }

you can do that by using a timestamp of when the distance was last high:

 if(distance1<20){
 digitalWrite(led1,HIGH);
 timestamp1 = millis();
 }
 if(distance2<20){
 digitalWrite(led2,HIGH);
 timestamp2 = millis();
 }
if(millis() - timestamp1 > 5000){
 digitalWrite(led1,LOW);
}
if(millis() - timestamp2 > 5000){
 digitalWrite(led2,LOW);
}

timestamp1 and timestamp2 should be globals (or static locals). This code will delay turning off the led to 5 seconds after the sensor last detected something close.

answered Feb 18, 2018 at 11:49
2
  • Thank you very much! I managed to do it using your hints but I believe the code after if(millis()-timestamp1>5000) should be digitalWrite(led1,LOW); Commented Feb 18, 2018 at 12:27
  • @LelouchLamperouge indeed they should be set to low there. copy paste errors... Commented Feb 18, 2018 at 13:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.