#define trigger 13
#define echo 14
int cod;
int nigma = 2805;
void setup(){
Serial.begin(115200);
pinMode(trigger, OUTPUT);
pinMode(echo, INPUT);
pinMode(26, OUTPUT);
pinMode(27, OUTPUT);
pinMode(25, INPUT);
pinMode(12, OUTPUT);
pinMode(33, OUTPUT);
}
void loop(){
float distancia;
distancia = Ultrasonic(13, 14);
delay(200);
Serial.println("distância:");
Serial.print(distancia);
if(distancia < 30){
digitalWrite(26, 1);
}
else {
digitalWrite(26, 0);
}
int pir = digitalRead(25);
Serial.println(pir);
if(pir == 1 && distancia < 30){
digitalWrite(27, 1);
}
else{
digitalWrite(27, 0);
}
if(pir == 1 && distancia < 30){
Serial.print("Digite a senha: ");
while(true)
{
if(Serial.available()){
cod = Serial.parseInt();
Serial.print(cod);
break;
}
}
while(true){
if(cod == nigma){
Serial.println("\nSenha correta");
delay(500);
digitalWrite(12, 1);
digitalWrite(33, 1);
delay(1500);
digitalWrite(12, 0);
digitalWrite(33, 0);
delay(500);
digitalWrite(12, 1);
digitalWrite(33, 1);
delay(1500);
digitalWrite(12, 0);
digitalWrite(33, 0);
delay(500);
digitalWrite(12, 1);
digitalWrite(33, 1);
delay(1500);
digitalWrite(12, 0);
digitalWrite(33, 0);
break;
}
else{
Serial.println("senha incorreta");
break;
}
}
}
}
float Ultrasonic(int pintrigger, int pinecho){
float distancia = 0;
while(distancia == 0) {
digitalWrite(pintrigger, 0);
delayMicroseconds(5);
digitalWrite(pintrigger, 1);
delayMicroseconds(5);
float tempo = pulseIn(pinecho,1);
distancia = (tempo/2/29.15);
}
return distancia;
}