Advertisement
Edulynch

Arduino Alarma + Sensor de proximidad + Parlante

Jul 22nd, 2014
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. const int pingPin = 7; //sensor de proximidad
  2.  
  3. void setup() {
  4. pinMode(9, OUTPUT); //Usamos el pin 9 por ser PWM para escribir analogamente
  5. Serial.begin(9600); //iniciar comunicacion serial
  6. }
  7.  
  8. void loop()
  9. {
  10. // INICIO SENSOR DE PROXIMIDAD
  11. long duracion, cm;
  12.  
  13. pinMode(pingPin, OUTPUT);
  14. digitalWrite(pingPin, LOW);
  15. delayMicroseconds(2);
  16. digitalWrite(pingPin, HIGH);
  17. delayMicroseconds(5);
  18. digitalWrite(pingPin, LOW);
  19.  
  20. pinMode(pingPin, INPUT);
  21. duracion = pulseIn(pingPin, HIGH);
  22.  
  23. // convertir tiempo a distancia
  24. cm = microsecondsToCentimeters(duracion);
  25.  
  26. Serial.print(cm);
  27. Serial.print("cm");
  28. Serial.println();
  29.  
  30. delay(100);
  31. //FIN SENSOR 1/2
  32. int vol;
  33. if (cm>200) {vol = 0;} //use 200cms como referencia, ya que de tanto que probamos, nos volvia loco el sonido
  34. else {vol = (20*cm);} //(0 - 4000 de mas grave a mas agudo) ya que son 200cms => 4000/200 = 20
  35.  
  36. analogWrite(9, vol); //Hacer Sonar el Buzzer
  37.  
  38. }
  39. //FIN VOIDLOOP
  40.  
  41. long microsecondsToCentimeters(long microseconds) //Tiempo a distancia
  42. {
  43. return microseconds / 29 / 2;
  44. }
  45. // FIN SENSOR 2/2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement