hello, i have this problem, and I dont know how to solve it:

``````int trigPin = 13;
int echoPin = 12;
int piezoPin = 2;
//#define led 11
//#define led2 10

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

void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);  // Added this line
digitalWrite(trigPin, HIGH);
//  delayMicroseconds(1000); - Removed this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
for(distance < 50) {  // This is where the LED On/Off happens
noTone(piezoPin);
tone(piezoPin, 1000);
Serial.println(distance);
}
for(distance >50 && distance < 100) {
noTone(piezoPin);
tone(piezoPin,1000);
Serial.println(distance);
delay(200);
}
for(distance > 100 && distance < 150){
noTone(piezoPin);
tone(piezoPin,1000);
Serial.println(distance);
delay(500);
}
for(distance > 150 && distance < 200){
noTone(piezoPin);
tone(piezoPin,1000);
Serial.println(distance);
delay(1000);
}
/* if (distance >= 200 || distance <= 0){
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
}*/
delay(500);
}
``````

Your 'for' statements have invalid syntax. Looks to me like you need to use 'if' though I'm uncomfortable with the whole.

Wait... why should I use if statement? i want it to beep while it is on different distances...

(deleted)

lukapetko2: Wait... why should I use if statement? i want it to beep while it is on different distances...

If you use 'for' then it requires 3 parameters(check with Google). You could change to use 'while' instead of 'for' and then the conditions would be OK BUT BUT BUT there is nothing within the loop to change 'distance' so once you meet a 'while' condition it can never exit from the 'while' ! For your concept to work requires distance being measured each time round the 'while' loop not just at the start of the loop() function.

I don't actually see why you need the 'for/while' loops. I just see the need for 'if' .

stowite: If you use 'for' then it requires 3 parameters(check with Google).

...except when it doesn't.

AWOL: ...except when it doesn't.

Yeah but even when you leave one out you still gotta have the semicolon there. So really it is there it's just empty.

AWOL: ...except when it doesn't.

It is possible that my C/C++ knowledge is out of date but my understanding is that a traditional 'for' requires 3 'parameters' separated by semicolons all or any or which can be empty. Now I understand that there is a new form of 'for' used with iteration but at this time it is not in common use.

They're not "parameters" - check with Google.

AWOL: They're not "parameters" - check with Google.

OK - maybe not "parameters" but a rose by any other name ... . What would you call them? Arguments?

Expressions?

The three stooges?

-jim lee