Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- int P = 0; //Sensor PIR
- int Light = 0; //Presença de luz
- int Luzes = 0; //Status das Luzes
- int R = 0; //RFID (Botão pressionado)
- int E = 0; //Servo Motor Ligado
- int D = 0; //Valor de rotação do servo motor (saber se está aberto ou fechado o portão)
- int G = 0; //Sensor Ultrassom no Portão
- int F = 0; //Alarme de Invasor
- long readUltrasonicDistance(int pin)
- {
- pinMode(pin, OUTPUT); // Clear the trigger
- digitalWrite(pin, LOW);
- delayMicroseconds(2);
- // Sets the pin on HIGH state for 10 micro seconds
- digitalWrite(pin, HIGH);
- delayMicroseconds(10);
- digitalWrite(pin, LOW);
- pinMode(pin, INPUT);
- // Reads the pin, and returns the sound wave travel time in microseconds
- return pulseIn(pin, HIGH);
- }
- Servo servo_5;
- int counter, counter2;
- void setup()
- {
- pinMode(7, INPUT); //Ultrassom
- pinMode(6, INPUT); //Botão
- servo_5.attach(5); //Servo Motor
- pinMode(8, OUTPUT); //Alarme
- pinMode(A0, INPUT); //Medidor Luminosidade
- pinMode(2, INPUT); //Sensor PIR
- pinMode(3, OUTPUT); //LED Azul
- pinMode(4, OUTPUT); //LED Vermelho
- Serial.begin(9600);
- Serial.println("STARTED!!!!");
- }
- bool temAlguemDentro(){
- P = digitalRead(2);
- if (P == 1) {
- if(Luzes <= 0){
- Serial.println("Tem Alguém Dentro!");
- }
- return true;
- }
- return false;
- }
- bool estaEscuroDentro(){
- Light = analogRead(A0);
- if (Light >= 100) {
- if(Luzes <= 0){
- Serial.println("Está Escuro Dentro!");
- }
- return true;
- }
- return false;
- }
- void ligarLuzesDentro(){
- if(Luzes <= 0){
- digitalWrite(3, HIGH);
- digitalWrite(4, HIGH);
- Luzes = 500; //5s
- Serial.println("Ligando Luzes");
- }
- }
- void apagarLuzesDentro(bool forceShutdown){
- if(Luzes == 1 || forceShutdown){
- digitalWrite(3, LOW);
- digitalWrite(4, LOW);
- Luzes = 0;
- Serial.println("Apagando Luzes");
- }
- --Luzes;
- }
- void acionamentoDeLuzes(bool apagarLuzes){
- if(apagarLuzes) {
- apagarLuzesDentro(true);
- }
- else if (temAlguemDentro() && estaEscuroDentro()) {
- ligarLuzesDentro();
- }
- else {
- apagarLuzesDentro(false);
- }
- }
- void delaySincronizadoComLuzes(int secs){
- for(counter = 0; counter < secs*10; ++counter){
- acionamentoDeLuzes(false);
- delay(100); // Wait for 100 millisecond(s)
- }
- }
- bool passouRFID(){
- R = digitalRead(6);
- if (R == 1) {
- Serial.println("Leu o RFID");
- return true;
- }
- return false;
- }
- void abrirPortao(){
- Serial.println("Iniciando Abertura do Portão");
- E = 1;
- D = 90;
- servo_5.write(D);
- aguardarAberturaTotalDoPortao();
- }
- void aguardarAberturaTotalDoPortao(){
- Serial.println("Aguardando Abertura Total do Portão...");
- delaySincronizadoComLuzes(10);
- }
- void aguardarEntrarPeloPortao(){
- Serial.println("Aguardando Entrar Pelo Portão...");
- E = 0;
- D = 0;
- G = 0.01723 * readUltrasonicDistance(7);
- while (G >= 50) {
- G = 0.01723 * readUltrasonicDistance(7);
- delaySincronizadoComLuzes(1);
- }
- aguardarEntradaCompletaPeloPortao();
- }
- void aguardarEntradaCompletaPeloPortao(){
- Serial.println("Aguardando Entrada Completa Pelo Portão...");
- while (G < 50) {
- G = 0.01723 * readUltrasonicDistance(7);
- delaySincronizadoComLuzes(1);
- }
- }
- void fecharPortao(){
- Serial.println("Fechando o Portão");
- E = 1;
- servo_5.write(D);
- aguardarFechamentoCompletoDoPortao();
- }
- void aguardarFechamentoCompletoDoPortao(){
- Serial.println("Aguardando Fechamento Completo do Portão...");
- for (counter2 = 0; counter2 < 100; ++counter2) {
- acionamentoDeLuzes(false);
- if(alguemEntrouIndevidamente()){
- ativarAlarme();
- }
- delay(100); // Wait for 100 millisecond(s)
- }
- E = 0;
- Serial.println("Portão Fechou");
- }
- bool alguemEntrouIndevidamente(){
- G = 0.01723 * readUltrasonicDistance(7);
- if (G < 50) {
- Serial.println("Alguém Entrou Indevidamente!");
- return true;
- }
- return false;
- }
- void ativarAlarme(){
- Serial.println("Ativando Alarme!");
- F = 1;
- for(counter = 0; counter < 5; ++counter){
- dispararSom();
- }
- while(temAlguemDentro()){
- acionamentoDeLuzes(true);
- dispararSom();
- }
- desativarAlarme();
- }
- void dispararSom(){
- Serial.println("Disparando Som!");
- tone(8, 165, 1000); // play tone 40 (E3 = 165 Hz)
- delay(1000); // Wait for 1000 millisecond(s)
- }
- void desativarAlarme(){
- Serial.println("Desativando Alarme!");
- F = 0;
- }
- void loop()
- {
- acionamentoDeLuzes(false);
- if (passouRFID()) {
- abrirPortao();
- aguardarEntrarPeloPortao();
- fecharPortao();
- }
- delay(10);
- }
Add Comment
Please, Sign In to add comment