Proximity sensor error help

We are doing an Arduino project involving a proximity sensor.

We want to make the LED light up 5 seconds after the proximity sensor have detected an object within 5cm or less distance. The LED should stay on for 30 seconds, and it should turn off automatically after.

Now, our problem is, even without detecting a new object, the LED still stays on (the process is continuous). What should we do? Please help us, thank you!

Here is our code:

const int trigPin = 3; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 2; // Echo Pin of Ultrasonic Sensor
const int red = 11; // led to pin 11

void setup() {
// put your setup code here, to run once:
pinMode(trigPin, INPUT);
pinMode(echoPin, OUTPUT);
pinMode(red, OUTPUT); // setting led pin as output
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
long duration, inches, cm;

digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(2);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
Serial.print("Distance: ");
Serial.print(cm);
Serial.println(" cm");

if (constrain(cm, 2, 5)) {
digitalWrite(red, HIGH);
delay(5000);
digitalWrite(red, LOW);
delay(30000);
}

else {
digitalWrite(red, LOW);
}
}

Hello hatdog1234
Replace the delay() function by a timer function based on the BLINKWITHOUTDELAY example to be found in the IDE.
Have a nice day and enjoy coding in C++.

Hello, where have you calculated the distance in your code? Can't see that. This is the formula of calculating distance in cm from duration.
distance = duration /58.82 ;
This must be done inside the code.

We'll try that. Thank you very much!

Thank you very much for letting us know!

@hatdog1234, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the Installation & Troubleshooting category.

Can you please spend sometime reading How to get the best out of this forum (again?) and pay special attention to the section how to post code using code tags; it makes it easier to read, easier to copy and prevents the forum software from incorrect interpretation of the code.

You have 'duration', the number of microseconds the sound traveled to get to the object and back. You forgot to translate that into 'cm' which appears to be your variable to hold the distance. To do that, divide by 58, the number of microseconds sound takes to travel 2 cm (1 cm, round trip).

First, fix your code so cm will store the distance in cm. Then, see that

constrain() will return a value between 2 and 5. It does not return a logical value usable as a condition for your if.

Use

if (cm == constrain(cm,2,5)) {

or

if (cm >=2 && cm <=5) {

If you remove the lower limit (you said

remember that pulseIn() will return 0 if no object is detected. https://www.arduino.cc/reference/en/language/functions/advanced-io/pulsein/

So your condition should be something like

if (cm>0 && cm<=5) {

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