CreadPag

UltraSonido, led, buzzer

Oct 11th, 2016
251
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define trigPin 12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
  2. #define echoPin 13 // Arduino pin tied to echo pin on the ultrasonic sensor.
  3.  
  4. const int zumbador = 6; // Zumbador conectado al pin 6
  5. const int led = 8; // LED conectado al pin 6
  6.  
  7. void setup() {
  8. Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.
  9. pinMode(trigPin,OUTPUT);
  10. pinMode(echoPin,INPUT);
  11. pinMode(zumbador, OUTPUT);
  12. pinMode(led, OUTPUT);
  13. }
  14.  
  15. void loop() {
  16. int duration, distance, pausa;
  17. digitalWrite(trigPin,HIGH);
  18. delayMicroseconds(500);
  19. digitalWrite(trigPin, LOW);
  20. duration = pulseIn(echoPin,HIGH);
  21. distance = (duration)/58;
  22. if(distance >= 546 || distance <= 1){
  23. Serial.println("Out of range");
  24. }
  25. else {
  26. Serial.print(distance);
  27. Serial.println(" cm");
  28. }
  29. if(distance < 100){
  30. pausa = distance * 10;
  31. digitalWrite(zumbador, HIGH);
  32. digitalWrite(led, HIGH);
  33. delay(pausa);
  34. }
  35.  
  36. digitalWrite(zumbador, LOW);
  37. digitalWrite(led, LOW);
  38. delay(pausa);
  39. }
RAW Paste Data