#include const int TRIG_PIN = 9; const int ECHO_PIN = 10; float inches; Servo myservo; int pos = 60; void setup() { myservo.attach(13); Serial.begin(9600); pinMode(TRIG_PIN, OUTPUT); digitalWrite(TRIG_PIN, LOW); } void loop() { for (pos = 30; pos <= 180; pos += 15) { myservo.write(pos); FindRange(); Serial.print("Angle: "); Serial.print(myservo.read()); Serial.print(" Distance: "); Serial.println(inches); delay(1000); } for (pos = 180; pos >= 30; pos -= 15) { myservo.write(pos); FindRange(); Serial.print("Angle: "); Serial.print(myservo.read()); Serial.print(" Distance: "); Serial.println(inches); delay(1000); } } float FindRange () { unsigned long t1; unsigned long t2; unsigned long pulse_width; float cm; float inches; // start finding the distance digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); while ( digitalRead(ECHO_PIN) == 0 ); t1 = micros(); while ( digitalRead(ECHO_PIN) == 1); t2 = micros(); pulse_width = t2 - t1; inches = pulse_width / 148.0; return inches; }