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);
}