Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. // DECLARACION DE VARIABLES PARA PINES
  2. int pinecho = 11;
  3. int pintrigger = 12;
  4. int pinled = 13;
  5.  
  6. // VARIABLES PARA CALCULOS
  7. int tiempo, distancia;
  8.  
  9. void setup() {
  10. // PREPARAR LA COMUNICACION SERIAL
  11. Serial.begin(9600);
  12. // CONFIGURAR PINES DE ENTRADA Y SALIDA
  13. pinMode(pinecho, INPUT);
  14. pinMode(pintrigger, OUTPUT);
  15. pinMode(13, OUTPUT);
  16. }
  17.  
  18. void loop() {
  19. // ENVIAR PULSO DE DISPARO EN EL PIN "TRIGGER"
  20. digitalWrite(pintrigger, LOW);
  21. delayMicroseconds(2);
  22. digitalWrite(pintrigger, HIGH);
  23. // EL PULSO DURA AL MENOS 10 uS EN ESTADO ALTO
  24. delayMicroseconds(10);
  25. digitalWrite(pintrigger, LOW);
  26.  
  27. // MEDIR EL TIEMPO EN ESTADO ALTO DEL PIN "ECHO" EL PULSO ES PROPORCIONAL A LA DISTANCIA MEDIDA
  28. tiempo = pulseIn(pinecho, HIGH);
  29.  
  30. // LA VELOCIDAD DEL SONIDO ES DE 340 M/S O 29 MICROSEGUNDOS POR CENTIMETRO
  31. // DIVIDIMOS EL TIEMPO DEL PULSO ENTRE 58, TIEMPO QUE TARDA RECORRER IDA Y VUELTA UN CENTIMETRO LA ONDA SONORA
  32. distancia = tiempo / 58;
  33.  
  34. // ENVIAR EL RESULTADO AL MONITOR SERIAL
  35. Serial.print(distancia);
  36. Serial.println(" cm");
  37. delay(200);
  38.  
  39. // ENCENDER EL LED CUANDO SE CUMPLA CON CIERTA DISTANCIA
  40. if (distancia <= 50) {
  41. digitalWrite(13, HIGH);
  42. delay(500);
  43. } else {
  44. digitalWrite(13, LOW);
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement