Guest User

Untitled

a guest
Jun 18th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. #define trigger 8
  2. #define echo 7
  3. #define zumbador 9
  4. #include "TimerThree.h"
  5.  
  6. const int viaCM = 125, pin_pulsador[3] = {18, 19, 20}, LED[3] = {22,23,24};
  7.  
  8. volatile int distancia, distancia_ant, sensor, x = 0, y, e, pse_e = 5;
  9. volatile bool pulsador[3] = {false, false, false}, pulsador_anterior[3] = {false, false, false}, salida[3] = {false, false, false};
  10.  
  11. bool password = 0, notificacion = 0;
  12.  
  13.  
  14. void setup() {
  15. pinMode(trigger, OUTPUT);
  16. pinMode(echo, INPUT);
  17. pinMode(zumbador, OUTPUT);
  18. for(int i = 0; i < 3; i++)
  19. {
  20. pinMode(pin_pulsador[i], INPUT);
  21. pinMode(LED[i], OUTPUT);
  22. }
  23. /*
  24. attachInterrupt(digitalPinToInterrupt(18), pulsador1, RISING);
  25. attachInterrupt(digitalPinToInterrupt(19), pulsador1, RISING);
  26. attachInterrupt(digitalPinToInterrupt(20), pulsador1, RISING);
  27. */
  28. Timer3.initialize(1500000);
  29. Timer3.attachInterrupt(timer_general);
  30.  
  31.  
  32.  
  33. Serial.begin(9600);
  34. }
  35. void timer_general(void) {
  36. pse_e--;
  37. e = 0;
  38. distancia = sensor;
  39. }
  40.  
  41. void pulsadores(void) {
  42.  
  43. for(int i = 0; i < 3; i++)
  44. {
  45. pulsador_anterior[i] = pulsador[i];
  46. pulsador[i] = digitalRead(pin_pulsador[i]);
  47. if(pulsador[i] > pulsador_anterior[i])
  48. {
  49. salida[i] = true;
  50. }
  51. else salida[i] = false;
  52. }
  53.  
  54. }
  55.  
  56.  
  57.  
  58. /*
  59. void pulsador1() { CÓDIGO ACTUALMENTE INUTILIZADO
  60. x++;
  61. if((x == 1)||(x == 4)) y++;
  62. }
  63. void pulsador2() {
  64. x++;
  65. if(x == 3) y++;
  66. }
  67. void pulsador3() {
  68. x++;
  69. if(x == 2) y++;
  70. }
  71. */
  72.  
  73. int distancia_() { //Devuelve en cm la distancia a la que se encuentra el objeto del sensor
  74. long respuesta_echo;
  75. digitalWrite(trigger, LOW);
  76. delayMicroseconds(4);
  77. digitalWrite(trigger, HIGH);
  78. delayMicroseconds(10);
  79. digitalWrite(trigger, LOW);
  80. respuesta_echo = pulseIn(echo, HIGH);
  81. return (respuesta_echo * 0.01712);
  82. }
  83.  
  84.  
  85. void loop() {
  86. e = 1;
  87.  
  88. noInterrupts();
  89. sensor = distancia_(); //Evita enviar datos corruptos
  90. interrupts();
  91.  
  92. /*
  93. * El PIN actualmente es de cuatro dígitos con tres pulsadores (1321)
  94. */
  95. if(pse_e < 0)//Para que únicamente haga la lectura cada 0,05s
  96. {
  97. pse_e = 5;
  98. if(salida[0]) {
  99. if((x == 0)||(x == 3)) y++;
  100. x++;
  101. }
  102. if(salida[1]) {
  103. if(x == 2) y++;
  104. x++;
  105. }
  106. if(salida[2]) {
  107. if(x == 1) y++;
  108. x++;
  109. }
  110. pulsadores();
  111. }
  112. //Comprueba si se ha introducido la contraseña correctamente
  113. if(x == 4)
  114. {
  115. if(y == 4) password = 1;
  116. x = 0;
  117. y = 0;
  118. }
  119. else password = 0;
  120.  
  121. if(password)
  122. {
  123. while(password)
  124. {
  125. if(viaCM > distancia)
  126. {
  127. password = 0;
  128. delay(5000);
  129. }
  130. }
  131. }
  132. else if(viaCM > distancia)
  133. {
  134. notificacion = 1;
  135. }
  136. else notificacion = 0;
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143. //Impresiones
  144. switch(x) {
  145. case 1: {
  146. digitalWrite(LED[0], HIGH);
  147. break;
  148. }
  149. case 2: {
  150. digitalWrite(LED[1], HIGH);
  151. break;
  152. }
  153. case 3: {
  154. digitalWrite(LED[2], HIGH);
  155. break;
  156. }
  157. default: {
  158. x = 0;
  159. for(int i = 0; i < 4; i++) {
  160. digitalWrite(LED[i], LOW);
  161. }
  162. break;
  163. }
  164. }
  165. digitalWrite(zumbador, notificacion);
  166.  
  167. //Debug
  168. if(Serial.available() > 0)
  169. {
  170. salida[0] = Serial.read() - 48;
  171. }
  172. Serial.println(pse_e);
  173. Serial.println(salida[0]);
  174. Serial.println(x);
  175. Serial.println(y);
  176. Serial.println("");
  177. /* -FIN DEL PROGRAMA- */
  178.  
  179. while(e)
  180. {
  181.  
  182. }
  183. }
Add Comment
Please, Sign In to add comment