Advertisement
llLazy

Untitled

Mar 31st, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. long findHeight() {
  2. Servo vertControl;
  3. vertControl.attach(vertServo);
  4. const long offset;
  5. long curr, start;
  6. int angle = 0;
  7. start = measurement();
  8. do{
  9. curr = measurement()
  10. angle++;
  11. vertControl.write(angle);
  12. } while(curr<122)
  13.  
  14. return tan(angle)*start;
  15.  
  16.  
  17.  
  18. long measurement(){
  19. long duration;
  20. pinMode(pingPin, OUTPUT);
  21. digitalWrite(pingPin, LOW);
  22. delayMicroseconds(2);
  23. digitalWrite(pingPin, HIGH);
  24. delayMicroseconds(5);
  25. digitalWrite(pingPin, LOW);
  26. pinMode(pingPin, INPUT);
  27. duration = pulseIn(pingPin, HIGH);
  28. return microsecondsToInches(duration);
  29. }
  30.  
  31.  
  32. long microsecondsToInches(long microseconds)
  33. {
  34. // According to Parallax's datasheet for the PING))), there are
  35. // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  36. // second). This gives the distance travelled by the ping, outbound
  37. // and return, so we divide by 2 to get the distance of the obstacle.
  38. // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  39. return microseconds / 74 / 2;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement