Using funtions

I'm new to arduino and coding.
I'm trying to make a safe zone presence detector with a sr04, which would be working when I activate a switch connected to pin 11, that’s the only condition.
The problem I have is that all the code is working, except that it doesn't care about my condition, is as if I didn't put it at all.
Could someone help me understand why my conditions isn't working.
The function activado works fine, but it is always running without taking in consideration my condition in pin 11.

int const trigPin = 8; 
int const echoPin = 9; 
int const buzzPin = 10; 
int const conditionPin = 11; 
void setup()
{
pinMode(trigPin, OUTPUT); 
pinMode(echoPin, INPUT); 
pinMode(buzzPin, OUTPUT); 
pinMode(conditionPin, INPUT); // Condition to activate the sensor
}
void loop()
{
if (digitalRead(conditionPin) == HIGH){
  activado();
}if (digitalRead(conditionPin) == LOW){
 digitalWrite(buzzPin, LOW);
} 
delay(60);
}

void activado()
{int duration, distance;
digitalWrite(trigPin, HIGH);
delay(1);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 350) {
digitalWrite(buzzPin, HIGH);
} else {
digitalWrite(buzzPin, LOW);
}
delay(60);
}

Could you go ahead and say what happens when you run your code?

Do you have a pull down resistor from pin 11 to ground?

How is conditionPin wired? pin 11----switch---->?
EDIT: Drat Fungus, you beat me to it! :grin:

It may be that your pin 11 is set up with active being low. Without seeing how it's wired we can't really say.
You can try this and see if it works as expected. Also you can just use 'else' as the pin is HIGH implicitly if it's not low.

if (digitalRead(conditionPin) == LOW){
  activado();
} else {
 digitalWrite(buzzPin, LOW);
}

groundFungus:
Do you have a pull down resistor from pin 11 to ground?

That was the problem. It is working now.

Posted by groundFungus - Jun 19, 2018, 10:52 pm Quote
Do you have a pull down resistor from pin 11 to ground?

That was the problem. Now it is working fine.