Little problem with Sonars


hi guys,

i'm new this, sorry dumb questions (and not english language).
here code i'm playin' with:

code: [select]


#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 next - when sonar out of range, keep getting 'zona3' on serial monitor.
when delete line 'zona3' , works fine, need 3 zones...
can guys me?
where i'm making mistake?

thank very in advance...
i have more questions later.

not dumb question @ all....

when distance out of range, sensors return value of 0. when it's gone far, gives same if real close.

this line here:

code: [select]
if (distance < 5 && distance >=0) {

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

you change >= >, or give small non-zero value, >=0.1.


Arduino Forum > Using Arduino > Programming Questions > Little problem with Sonars


arduino

Comments