float distancia;
#define red 32
#define purple 14
#define trigger 18
#define echo 19
#define relay 15
#define pinoPIR 22
#define pinoOut 32


const int buzzer = 2;
int SW_relay = 0;
int senha;

void setup(){
  Serial.begin(115200);
  pinMode(pinoOut, INPUT); //DEFINE O PINO COMO ENTRADA
  pinMode(relay, OUTPUT);
  pinMode(trigger, OUTPUT); // trigger
  pinMode(echo, INPUT); //echo
  pinMode(pinoPIR, INPUT);
  pinMode(buzzer, OUTPUT);
}

void loop() {
  if(digitalRead(pinoOut) == LOW){ //SE LEITURA DO PINO FOR IGUAL A LOW, FAZ
 Serial.println ("Nivel 1 Violado"); 
 delay(1000); //IMPRIME O TEXTO NO MONITOR SERIAL
 us();
  }
 }

void us(){
  distancia = Ultrasonic (18, 19);
  delay(10);
  delay(1000);

  if (distancia < 20 && digitalRead(pinoOut) == LOW){
 Serial.println("Nivel 1 e 2 Violados.");
 delay(1000);
 pir();
 }

 else if(distancia > 20){
loop();

 }
}

void pir(){
//teste PIR
if (digitalRead(pinoPIR) == 1) { 
 Serial.println("Nivel 1, 2 e 3 Violados");
 delay(1000);
finalizar();
}
else{
  us();
}
pir();
}

void finalizar(){
Serial.println("Para desativação do sistema, digite a senha correta");
while(true){
    if(Serial.available()){
      senha = Serial.parseInt();
      switch (senha){
      case 1234:
      digitalWrite(relay, HIGH);
      digitalWrite(relay, LOW);
      Serial.println("Bem Vindo");
      delay(5000);
      digitalWrite(relay, HIGH);
      delay(5000);
      break;

      default: 
      tone(buzzer,392);
      delay(5000);
      noTone(buzzer);
      delay (1000);
      break;
      }
      loop();
      }
     }
    }






  float Ultrasonic(int a, int b){
int distancia = 0;
while(distancia == 0) {
digitalWrite(a, 0);
delayMicroseconds(5);
digitalWrite(a, 1);
delayMicroseconds(5);
float tempo = pulseIn(b,1);
distancia = (tempo/2/29.15);
}
return distancia;
}

$abcdeabcde151015202530fghijfghij
NOCOMNCVCCGNDINLED1PWRRelay Module