Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. //Joseph Tu - HCI Games Ultrasonic Sensor
  2.  
  3. const int trigPin = 50; // Trigger Pin of Ultrasonic Sensor
  4. const int echoPin = 51; // Echo Pin of Ultrasonic Sensor
  5.  
  6. void setup() {
  7. Serial.begin(9600); // Starting Serial Terminal
  8. }
  9.  
  10. void loop() {
  11. long duration, inches, cm;
  12. pinMode(trigPin, OUTPUT);
  13. digitalWrite(trigPin, LOW);
  14. delayMicroseconds(2);
  15. digitalWrite(trigPin, HIGH);
  16. delayMicroseconds(10);
  17. digitalWrite(trigPin, LOW);
  18. pinMode(echoPin, INPUT);
  19.  
  20. // Measures the response from the Echo Pin
  21.  
  22. duration = pulseIn(echoPin, HIGH);
  23. inches = microsecondsToInches(duration);
  24. cm = microsecondsToCentimeters(duration);
  25.  
  26. Serial.print(inches);
  27. Serial.print("inches, ");
  28. Serial.print(cm);
  29. Serial.print("cm");
  30. Serial.println();
  31. delay(100);
  32. }
  33.  
  34. long microsecondsToInches(long microseconds) {
  35. return microseconds / 74 / 2;
  36. }
  37.  
  38. long microsecondsToCentimeters(long microseconds) {
  39. return microseconds / 29 / 2;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement