Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. const int pingPin = 6;
  2. const int echoPin = 5;
  3.  
  4. void setup() {
  5.  
  6. Serial.begin(9600);
  7. pinMode(6, OUTPUT);
  8. pinMode(5, INPUT);
  9. pinMode(11, OUTPUT);
  10. }
  11.  
  12. void loop() {
  13.  
  14. long duration, inches, cm;
  15.  
  16.  
  17.  
  18. digitalWrite(pingPin, LOW);
  19. delayMicroseconds(2);
  20. digitalWrite(pingPin, HIGH);
  21. delayMicroseconds(5);
  22. digitalWrite(pingPin, LOW);
  23.  
  24.  
  25.  
  26. duration = pulseIn(echoPin, HIGH);
  27.  
  28. // convert the time into a distance
  29. inches = microsecondsToInches(duration);
  30. cm = microsecondsToCentimeters(duration);
  31.  
  32. if (cm < 30){
  33. digitalWrite(11, LOW);
  34. }
  35.  
  36. else
  37. digitalWrite(11, HIGH);
  38.  
  39.  
  40. Serial.print(inches);
  41. Serial.print("in, ");
  42. Serial.print(cm);
  43. Serial.print("cm");
  44. Serial.println();
  45.  
  46. delay(100);
  47. }
  48.  
  49. long microsecondsToInches(long microseconds) {
  50.  
  51. return microseconds / 74 / 2;
  52. }
  53.  
  54. long microsecondsToCentimeters(long microseconds) {
  55.  
  56. return microseconds / 29 / 2;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement