Hardware Reference
In-Depth Information
The preceding code simply moves the servo to an angle and then prints out the distance
value reported by the IR sensor. The specific statements that may be of interest are as fol-
lows:
•
servo.attach(servoPin);
: This statement attaches the servo control to
the pin defined
•
servo.write(angle);
: This statement sends the servo to this angle
•
inValue = analogRead(inputPin);
: This statement reads the analog
input value from this pin
•
distance = 30431 * pow(inValue, -1.169);
: This statement trans-
lates the reading to distance in centimeters
If you upload the sketch, open
Serial Monitor
, and enter different angle values, the servo
should move and you should see something like the following screenshot: