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.