Advertisement
RuiViana

Marcha Ré

Jul 6th, 2015
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1.  
  2. // http://labdegaragem.com/forum/topics/duvidas-altera-o-de-frequencia-no-buzzer-sensor-ultrassonico
  3.  
  4. #define trigPin 13 // Pino de disparo do US
  5. #define echoPin 12 // Pino de leitura do US
  6. #define led 11 // Pino Led indicativo 1
  7. #define led2 10 // Pino Led indicativo2
  8. #define led3 5 // Pino Led indicativo 3
  9. #define pinBuzzer 7 // Pino saída buzzer
  10.  
  11. void setup() {
  12. Serial.begin (9600);
  13. pinMode(trigPin, OUTPUT);
  14. pinMode(echoPin, INPUT);
  15. pinMode(led, OUTPUT);
  16. pinMode(led2, OUTPUT);
  17. pinMode(led3,OUTPUT);
  18. pinMode(pinBuzzer,OUTPUT);
  19. }
  20.  
  21. void loop() {
  22. long duration, distance;
  23. digitalWrite(trigPin, LOW);
  24. delayMicroseconds(2);
  25. digitalWrite(trigPin, HIGH);
  26. delayMicroseconds(10);
  27. digitalWrite(trigPin, LOW);
  28.  
  29. duration = pulseIn(echoPin, HIGH);
  30. distance = (duration/2) / 29.1;
  31.  
  32. map(distance, 0, 400,200,10);
  33. tone(pinBuzzer,(10,262,distance));
  34.  
  35. if (distance >= 400 || distance <= 0){
  36. Serial.println("Out of range");
  37. }
  38. else {
  39. Serial.print(distance);
  40. Serial.println(" cm");
  41. }
  42. delay(50);
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement