Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. int trigPin = 11; // Trigger
  2. int echoPin = 12; // Echo
  3. long duration, cm, inches;
  4.  
  5. void setup() {
  6. //Serial Port begin
  7. Serial.begin (9600);
  8. //Define inputs and outputs
  9. pinMode(trigPin, OUTPUT);
  10. pinMode(echoPin, INPUT);
  11. }
  12.  
  13. void loop() {
  14. // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  15. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  16. digitalWrite(trigPin, LOW);
  17. delayMicroseconds(5);
  18. digitalWrite(trigPin, HIGH);
  19. delayMicroseconds(10);
  20. digitalWrite(trigPin, LOW);
  21.  
  22. // Read the signal from the sensor: a HIGH pulse whose
  23. // duration is the time (in microseconds) from the sending
  24. // of the ping to the reception of its echo off of an object.
  25. pinMode(echoPin, INPUT);
  26. duration = pulseIn(echoPin, HIGH);
  27.  
  28. // Convert the time into a distance
  29. cm = (duration/2) / 29.1; // Divide by 29.1 or multiply by 0.0343
  30. inches = (duration/2) / 74; // Divide by 74 or multiply by 0.0135
  31.  
  32. Serial.print(inches);
  33. Serial.print("in, ");
  34. Serial.print(cm);
  35. Serial.print("cm");
  36. Serial.println();
  37.  
  38. delay(250);
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement