Guest User

Untitled

a guest
Jun 24th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. #include <Servo.h>
  2.  
  3. int P = 0; //Sensor PIR
  4.  
  5. int Light = 0; //Presença de luz
  6.  
  7. int Luzes = 0; //Status das Luzes
  8.  
  9. int R = 0; //RFID (Botão pressionado)
  10.  
  11. int E = 0; //Servo Motor Ligado
  12.  
  13. int D = 0; //Valor de rotação do servo motor (saber se está aberto ou fechado o portão)
  14.  
  15. int G = 0; //Sensor Ultrassom no Portão
  16.  
  17. int F = 0; //Alarme de Invasor
  18.  
  19. long readUltrasonicDistance(int pin)
  20. {
  21. pinMode(pin, OUTPUT); // Clear the trigger
  22. digitalWrite(pin, LOW);
  23. delayMicroseconds(2);
  24. // Sets the pin on HIGH state for 10 micro seconds
  25. digitalWrite(pin, HIGH);
  26. delayMicroseconds(10);
  27. digitalWrite(pin, LOW);
  28. pinMode(pin, INPUT);
  29. // Reads the pin, and returns the sound wave travel time in microseconds
  30. return pulseIn(pin, HIGH);
  31. }
  32.  
  33. Servo servo_5;
  34.  
  35. int counter, counter2;
  36.  
  37. void setup()
  38. {
  39. pinMode(7, INPUT); //Ultrassom
  40. pinMode(6, INPUT); //Botão
  41. servo_5.attach(5); //Servo Motor
  42.  
  43. pinMode(8, OUTPUT); //Alarme
  44. pinMode(A0, INPUT); //Medidor Luminosidade
  45. pinMode(2, INPUT); //Sensor PIR
  46. pinMode(3, OUTPUT); //LED Azul
  47. pinMode(4, OUTPUT); //LED Vermelho
  48.  
  49. Serial.begin(9600);
  50.  
  51. Serial.println("STARTED!!!!");
  52. }
  53.  
  54. bool temAlguemDentro(){
  55. P = digitalRead(2);
  56.  
  57. if (P == 1) {
  58. if(Luzes <= 0){
  59. Serial.println("Tem Alguém Dentro!");
  60. }
  61.  
  62. return true;
  63. }
  64.  
  65. return false;
  66. }
  67.  
  68. bool estaEscuroDentro(){
  69. Light = analogRead(A0);
  70.  
  71. if (Light >= 100) {
  72. if(Luzes <= 0){
  73. Serial.println("Está Escuro Dentro!");
  74. }
  75.  
  76. return true;
  77. }
  78.  
  79. return false;
  80. }
  81.  
  82. void ligarLuzesDentro(){
  83. if(Luzes <= 0){
  84. digitalWrite(3, HIGH);
  85. digitalWrite(4, HIGH);
  86. Luzes = 500; //5s
  87. Serial.println("Ligando Luzes");
  88. }
  89. }
  90.  
  91. void apagarLuzesDentro(bool forceShutdown){
  92. if(Luzes == 1 || forceShutdown){
  93. digitalWrite(3, LOW);
  94. digitalWrite(4, LOW);
  95. Luzes = 0;
  96. Serial.println("Apagando Luzes");
  97. }
  98.  
  99. --Luzes;
  100. }
  101.  
  102. void acionamentoDeLuzes(bool apagarLuzes){
  103. if(apagarLuzes) {
  104. apagarLuzesDentro(true);
  105. }
  106. else if (temAlguemDentro() && estaEscuroDentro()) {
  107. ligarLuzesDentro();
  108. }
  109. else {
  110. apagarLuzesDentro(false);
  111. }
  112. }
  113.  
  114. void delaySincronizadoComLuzes(int secs){
  115. for(counter = 0; counter < secs*10; ++counter){
  116. acionamentoDeLuzes(false);
  117. delay(100); // Wait for 100 millisecond(s)
  118. }
  119. }
  120.  
  121. bool passouRFID(){
  122. R = digitalRead(6);
  123.  
  124. if (R == 1) {
  125. Serial.println("Leu o RFID");
  126. return true;
  127. }
  128.  
  129. return false;
  130. }
  131.  
  132. void abrirPortao(){
  133. Serial.println("Iniciando Abertura do Portão");
  134.  
  135. E = 1;
  136. D = 90;
  137.  
  138. servo_5.write(D);
  139.  
  140. aguardarAberturaTotalDoPortao();
  141. }
  142.  
  143. void aguardarAberturaTotalDoPortao(){
  144. Serial.println("Aguardando Abertura Total do Portão...");
  145.  
  146. delaySincronizadoComLuzes(10);
  147. }
  148.  
  149. void aguardarEntrarPeloPortao(){
  150. Serial.println("Aguardando Entrar Pelo Portão...");
  151.  
  152. E = 0;
  153. D = 0;
  154.  
  155. G = 0.01723 * readUltrasonicDistance(7);
  156.  
  157. while (G >= 50) {
  158. G = 0.01723 * readUltrasonicDistance(7);
  159.  
  160. delaySincronizadoComLuzes(1);
  161. }
  162.  
  163. aguardarEntradaCompletaPeloPortao();
  164. }
  165.  
  166. void aguardarEntradaCompletaPeloPortao(){
  167. Serial.println("Aguardando Entrada Completa Pelo Portão...");
  168.  
  169. while (G < 50) {
  170. G = 0.01723 * readUltrasonicDistance(7);
  171.  
  172. delaySincronizadoComLuzes(1);
  173. }
  174. }
  175.  
  176. void fecharPortao(){
  177. Serial.println("Fechando o Portão");
  178.  
  179. E = 1;
  180.  
  181. servo_5.write(D);
  182.  
  183. aguardarFechamentoCompletoDoPortao();
  184. }
  185.  
  186. void aguardarFechamentoCompletoDoPortao(){
  187. Serial.println("Aguardando Fechamento Completo do Portão...");
  188.  
  189. for (counter2 = 0; counter2 < 100; ++counter2) {
  190. acionamentoDeLuzes(false);
  191.  
  192. if(alguemEntrouIndevidamente()){
  193. ativarAlarme();
  194. }
  195.  
  196. delay(100); // Wait for 100 millisecond(s)
  197. }
  198.  
  199. E = 0;
  200.  
  201. Serial.println("Portão Fechou");
  202. }
  203.  
  204. bool alguemEntrouIndevidamente(){
  205. G = 0.01723 * readUltrasonicDistance(7);
  206.  
  207. if (G < 50) {
  208. Serial.println("Alguém Entrou Indevidamente!");
  209. return true;
  210. }
  211.  
  212. return false;
  213. }
  214.  
  215. void ativarAlarme(){
  216. Serial.println("Ativando Alarme!");
  217.  
  218. F = 1;
  219.  
  220. for(counter = 0; counter < 5; ++counter){
  221. dispararSom();
  222. }
  223.  
  224. while(temAlguemDentro()){
  225. acionamentoDeLuzes(true);
  226. dispararSom();
  227. }
  228.  
  229. desativarAlarme();
  230. }
  231.  
  232. void dispararSom(){
  233. Serial.println("Disparando Som!");
  234.  
  235. tone(8, 165, 1000); // play tone 40 (E3 = 165 Hz)
  236. delay(1000); // Wait for 1000 millisecond(s)
  237. }
  238.  
  239. void desativarAlarme(){
  240. Serial.println("Desativando Alarme!");
  241.  
  242. F = 0;
  243. }
  244.  
  245. void loop()
  246. {
  247.  
  248.  
  249.  
  250. acionamentoDeLuzes(false);
  251.  
  252. if (passouRFID()) {
  253. abrirPortao();
  254.  
  255. aguardarEntrarPeloPortao();
  256.  
  257. fecharPortao();
  258. }
  259.  
  260. delay(10);
  261. }
Add Comment
Please, Sign In to add comment