Controlling the servos with a program
Now that the hardware is connected, you'll need to supply the control signal to make your servos move. To control your servo, bring up the Arduino IDE. Make sure that the proper Arduino and port are chosen. Then enter the lines of code as shown in the following screenshot:
This code uses the Servo
library that is installed with the standard Arduino IDE. The three sections of code that you'll need to understand are as follows:
The global variables
servo
,servoPin
, andangle
are used by the program. TheServo
data type adds a set of functions so that you can control your servo. This includes theservo.attach(servoPin)
andservo.write(angle)
functions, which you will use in this program to send the servo to a specific angle. To find out all the different functions that are available, visit http://arduino.cc/en/reference/servo.The
setup()
function connects the servo functionality to the proper pin and then initializes the serial port.The
loop()
function reads...