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