HC-SR04

Hey you guys I have a question regarding the use of time as a factor in my projekt (submitted down under). I am currently working with the HC-SR04 module to create a distance measuring device. So instead of it giving constant feedback, ass my current code does, i would like it to use pin13 HIGH only after it's been HIGH for 5 or more seconds. Also has it been LOW for 5 or more seconds ass well I would like it to make pin13 LOW.

Afstandsm_ler_med_afgr_nsning.ino (1.66 KB)

It's better if you post your code, rather than attach it. And it must be posted between code tags. </> in the "Reply" dialog.
When it's just attached, we have to first create a folder then open your code in our IDE. And afterwards, the folder remains unless we navigate to the "Temp" folder and manually remove it. It's much easier to just view the code in your post.

I don't understand what you're trying to say here:-

So instead of it giving constant feedback, ass my current code does, i would like it to use pin13 HIGH only after it's been HIGH for 5 or more seconds. Also has it been LOW for 5 or more seconds ass well I would like it to make pin13 LOW.

What do you mean by giving "it" constant feedback?
And, after what has been high (or low) for 5 or more seconds?
Please clarify.

Right now the onboard pin13 is triggered HIGH when the range is between 0 and 400 cm. Additionally i would like it to maybe use pin11 to give a second feedback, ass I mentioned earlier that if pin13 was HIGH for more than 5 seconds, the pin11 could be triggered HIGH as well. And also if the range is other than 0-400 cm and the pin13 i LOW for more than 5 seconds, i would like the pin11 to be LOW.

Thanks for the quick reply!

And sorry here is the code

#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
#define pin13 // Onboard LED

int maximumRange = 400; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance

void setup() {
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(13, OUTPUT); // Use LED indicator (if required)
}

void loop() {
/* The following trigPin/echoPin cycle is used to determine the
 distance of the nearest object by bouncing soundwaves off of it. */ 
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10); 
 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 
 //Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;
 
 if (distance >= maximumRange || distance <= minimumRange){
 /* Send a negative number to computer and Turn LED ON 
 to indicate "out of range" */
 Serial.println("-1");
 digitalWrite(13, HIGH); 
 }
 else {
 /* Send the distance to the computer using Serial protocol, and
 turn LED OFF to indicate successful reading. */
 Serial.println(distance);
 digitalWrite(13, LOW); 
 }
 
 //Delay 50ms before next reading.
 delay(50);
}

The first point is that the range is unlikely to be <0cm. And, in fact, the HC-SR04 only works down to an absolute minimum of 2cm.
It's unlikely to be >400cm, too, since that's the absolute maximum range of a HC-SR04 - it will return 0 after making you wait for a full second, (pulseIn's default timeout), unless you set a timeout for 'pulseIn()'.

So all you need is code to set pin 13 high on startup, then low if the value returned by 'pulseIn()' is not 0.
To turn on/off the LED on pin 11, you'll need to set a timer based on 'millis()', when pin 13 changes state, then change the state of pin 11 if 5 seconds elapses.

Check out the "BlinkWithoutDelay" example in the IDE for a guide to simple 'millis()'-based timing.

I don't really understand the purpose of all of this. What are you trying to make?

And you might want to check out the NewPing library for your ultrasonic ranging.