Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- long findHeight() {
- Servo vertControl;
- vertControl.attach(vertServo);
- const long offset;
- long curr, start;
- int angle = 0;
- start = measurement();
- do{
- curr = measurement()
- angle++;
- vertControl.write(angle);
- } while(curr<122)
- return tan(angle)*start;
- long measurement(){
- long duration;
- pinMode(pingPin, OUTPUT);
- digitalWrite(pingPin, LOW);
- delayMicroseconds(2);
- digitalWrite(pingPin, HIGH);
- delayMicroseconds(5);
- digitalWrite(pingPin, LOW);
- pinMode(pingPin, INPUT);
- duration = pulseIn(pingPin, HIGH);
- return microsecondsToInches(duration);
- }
- long microsecondsToInches(long microseconds)
- {
- // According to Parallax's datasheet for the PING))), there are
- // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
- // second). This gives the distance travelled by the ping, outbound
- // and return, so we divide by 2 to get the distance of the obstacle.
- // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
- return microseconds / 74 / 2;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement