Advertisement
Guest User

Untitled

a guest
Apr 18th, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. #define trigPin 13 // triger port in arduino
  2. #define echoPin 12 // echo port in arduino
  3.  
  4. void setup() {
  5. Serial.begin(9600); // setting serial communication
  6. pinMode(trigPin, OUTPUT); // Ultrasonic output mode(output by triggering)
  7. pinMode(echoPin, INPUT); // Untrasonic input mode(input from reflection)
  8. }
  9.  
  10. long microsecondsToCentimeters(long microseconds) {
  11. // speed of sound wave is 340 m/s, (340m/s) = (29 microsenconds / cm)
  12. return microseconds / 29 / 2;
  13. }
  14.  
  15. void loop() {
  16. long duration,cm;
  17.  
  18. digitalWrite(trigPin, LOW);
  19. delayMicroseconds(4);
  20.  
  21. digitalWrite(trigPin, HIGH);
  22. delayMicroseconds(10);
  23.  
  24. digitalWrite(trigPin, LOW);
  25.  
  26. duration = pulseIn(echoPin, HIGH);
  27.  
  28. cm = microsecondsToCentimeters(duration);
  29.  
  30. Serial.write(cm); // ".print" is ASCII code but ".write"
  31. delay(200);
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement