Little problem with Sonars

Hi guys,

I'm completely new to this, so sorry for dumb questions (and also for not so good English language).
Here is code I'm playin' with:

#define trigPin 50
#define echoPin 51
#define led 42


void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);

}
void loop() {
{
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;

if (distance > 30 && distance < 400) {
Serial.println (distance);
digitalWrite(led, LOW);

}

if (distance < 30 && distance > 20) {
Serial.println("ZONA1");
digitalWrite(led, LOW);
delay(1000);
digitalWrite(led, HIGH);
delay(1000);
}

if (distance < 20 && distance > 10) {
Serial.println("ZONA2");
digitalWrite(led, LOW);
delay(500);
digitalWrite(led, HIGH);
delay(500);

}
if (distance < 5 && distance >=0) {
Serial.println("ZONA3");
digitalWrite(led, LOW);
delay(50);
digitalWrite(led, HIGH);
delay(50);
}

if(distance > 30){

Serial.println(distance);
digitalWrite(led, HIGH);

}

}

}

My problem is next - when sonar is out of range, I keep getting 'ZONA3' on serial monitor.
When I delete that line with 'ZONA3' , everything works fine, but I need 3 zones...
Can you guys help me?
Where I'm making mistake?

Thank you very very much in advance...
I will probably have more questions later.

Not a dumb question at all....

When the distance is out of range, those sensors return a value of 0. So when it's gone too far, it actually gives the same as if it was real close.

This line here:

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

is assessed as true when it's very far away, because of the =.

You could change the >= to just >, or give it a small non-zero value, >=0.1.

Sir, You just made my day!
Small changes that makes everything different.
Thank you SO MUCH!
Everything works perfect now...

Glad to help.....

JimboZA:
Glad to help.....

That helped me more than you can imagine... I came to new 'problem'. Code is working fine, BUT readings are too slow. Everything acts like I put delay on 2-3sec. And for this project I need fast as possible reactions... I noticed that when I remove 'duration2' and 'duration3' everything works fast, but I need those for other 2 sensors. Just need to make faster response and don't know how...
Here is the code

#define trigPin1 50
#define echoPin1 51
#define trigPin2 52
#define echoPin2 53
#define trigPin3 54
#define echoPin3 55
#define led 43
#define led2 4
#define led3 44


void setup() {
Serial.begin (9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(trigPin3, OUTPUT);
pinMode(echoPin3, INPUT);
pinMode(led, OUTPUT);

}
void loop() {
{
long duration1, duration2, duration3, distance_N, distance_L, distance_D;
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(2);
digitalWrite(trigPin1, LOW);

duration1 = pulseIn(echoPin1, HIGH);
distance_N = (duration1/2) / 29.1;
distance_L = (duration2/2) / 29.1;
distance_D = (duration3/2) / 29.1;

digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
duration2 = pulseIn(echoPin2, HIGH);

digitalWrite(trigPin3, LOW);
delayMicroseconds(2);
digitalWrite(trigPin3, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin3, LOW);
duration3 = pulseIn(echoPin3, HIGH);

if (distance_N > 30 && distance_N < 400) {
Serial.println (distance_N);
delay(50);
digitalWrite(led, LOW);
 }
if (distance_N < 30 && distance_N > 20) {
Serial.println("ZONA1");
digitalWrite(led, LOW);
delay(10);
digitalWrite(led, HIGH);
delay(10);
}
if (distance_N < 20 && distance_N > 10) {
Serial.println("ZONA2");
digitalWrite(led, LOW);
delay(5);
digitalWrite(led, HIGH);
delay(5);
}
if (distance_N < 5 && distance_N >0.1) {
Serial.println("ZONA3");
digitalWrite(led, LOW);
delay(50);
digitalWrite(led, HIGH);
delay(50);

}

if (distance_L < 30 && distance_L > 10) {
  Serial.println( "LEVI MRTAV UGAO - ZONA1");
  digitalWrite(led2, HIGH);
  delay(200);
  digitalWrite(led2, LOW);
  delay(200);
  }
  if (distance_L < 10 && distance_L > 0.1) {
  Serial.println( "LEVI MRTAV UGAO - ZONA2");
  digitalWrite(led2, HIGH);
  delay(50);
  digitalWrite(led2, LOW);
  delay(50);
  }
  if (distance_L >30){
    Serial.println(distance_L);
    digitalWrite(led2, HIGH);
    }
  if (distance_D < 30 && distance_D > 10) {
  Serial.println( "DESNI MRTAV UGAO - ZONA1");
  digitalWrite(led3, HIGH);
  delay(200);
  digitalWrite(led3, LOW);
  delay(200);
  }
  if (distance_D < 10 && distance_D > 0.1) {
  Serial.println( "DESNI MRTAV UGAO - ZONA2");
  digitalWrite(led3, HIGH);
  delay(50);
  digitalWrite(led3, LOW);
  delay(50);
  }
  if (distance_D >30){
    Serial.println(distance_D);
    digitalWrite(led3, HIGH); 
}   
}
}

pulseIn() has an optional timeout value with the default being one second. If you are looking for values less than 400 cm you can set the timeout to about 32000 microseconds (32 milliseconds) it will give up much sooner looking for a pulse.

For more than one ping sensor, you really should be using the NewPing library. It uses interrupts, so there is no waiting for the sensor(s) to get a response.

johnwasser:
pulseIn() has an optional timeout value with the default being one second. If you are looking for values less than 400 cm you can set the timeout to about 32000 microseconds (32 milliseconds) it will give up much sooner looking for a pulse.

I've tried, but nothing changed... But thank you very much for reply...

PaulS:
For more than one ping sensor, you really should be using the NewPing library. It uses interrupts, so there is no waiting for the sensor(s) to get a response.

I've instaled NewPing and I will try now as you said. Thank you for reply, I will write what I did with this.