Guest User

Code Arudino

a guest
Mar 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1.  
  2. // defines pins numbers
  3. const int trigPin = 5;
  4. const int echoPin = 6;
  5.  
  6. // defines variables
  7. long duration;
  8. int distance;
  9.  
  10. void setup() {
  11. pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  12. pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  13. Serial.begin(9600); // Starts the serial communication
  14. }
  15.  
  16. void loop() {
  17. // Clears the trigPin
  18. digitalWrite(trigPin, LOW);
  19. delayMicroseconds(2);
  20.  
  21. // Sets the trigPin on HIGH state for 10 micro seconds
  22. digitalWrite(trigPin, HIGH);
  23. delayMicroseconds(10);
  24. digitalWrite(trigPin, LOW);
  25.  
  26. // Reads the echoPin, returns the sound wave travel time in microseconds
  27. duration = pulseIn(echoPin, HIGH);
  28.  
  29. // Calculating the distance
  30. distance= duration/50;
  31.  
  32. tone (8, distance/5);
  33.  
  34. // Prints the distance on the Serial Monitor
  35. Serial.print("Distance: ");
  36. Serial.println(distance);
  37. }
Advertisement
Add Comment
Please, Sign In to add comment