Arduino Does Unexplainable Things with Delays and Ultrasounds - Why?

Hi there, I have a line of code that says:

if(distance < 50){
hit1 = 1;
}

I need it to say:

if(50>distance>1){
hit1 = 1;
}

So I wrote this:

if(distance < 50 && distance > 1){
hit1 = 1;
}

Yes, it gives me what I want which is for hit1 to be 1 when distance is between 1 and 50 BUT only when I add this, the ultrasound sensor becomes very laggy and misses 15x more often than usual even though I didn't really change anything that would affect that. Without the second &&, the ultrasound was very responsive, snappy and accurate, now it doesn't catch my hand wave UNLESS I do it super slow. If I just remove the && and change nothing else, the sensor is super responsive again.

Please explain why.

I did fix the problem but I do not understand why my solution fixed the problem so I was hoping someone could explain. When I had one conditional in my if statement and a delay of 30ms using delay() between each ping, everything worked normally.

As soon as I added a second conditional to my if statement using &&, the sensor would not function quickly and was just sloppy. (It still worked though) This happened even though adding a second conditional shouldn't make an ultrasound sensor less accurate.

I fixed the issue so that I have two conditionals in my if statement using && and the ultrasound is responsive by simply reducing the delay() between pings from 30ms to 5ms.

Seriously, how does that even matter? LOL : ) 30ms delay with one conditional gives the same performance as 5ms delay with two conditionals???

Also, I noticed that with two conditions and delay() less than 30ms, the two sensors should be interfering with each other but they don't even when the back wall is 3 meters away giving the echo more time to interfere.

Also, I noticed when I have two conditions and delay(30), the response was too slow (BUT ONLY WHEN USING TWO CONDITIONALS). When I used delay(8) and delay(15) the ultrasounds wouldn't work but then they would work fine with delay(5) with is even less.

I though computers were perfect. Disappointed!!! If anyone can explain the weird scenarios above and why they happen, please let me know.

Thanks,

Ardalan

Please post your full sketch. If possible you should always post code directly in the forum thread as text using code tags (</> button on the toolbar). This will make it easy for anyone to look at it, which will increase the likelihood of you getting help. If the sketch is longer than the forum will allow then it's ok to add it as an attachment. Don't put your code in some external file service like dropbox, etc. We shouldn't need to go to an external website just to help you. I do feel it's reasonable to post a link to code hosted on GitHub or similar code hosting sites since that's an platform specifically designed for this sort of thing

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you're using the Arduino Web Editor then you will not have access to this useful tool. I recommend using the standard Arduino IDE instead.

When your code requires a library that's not included with the Arduino IDE please post a link(using the chain links icon on the toolbar to make it clickable) to where you downloaded that library from or if you installed it using Library Manger(Sketch > Include Library > Manage Libraries) then say so and state the full name of the library.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

Please explain why.

Your code is wrong. Isn't that obvious?