Figure 5-10. The actual Arduino-based servo motor controller prototype
To make the servo motor stationary on the solderless breadboard, a small jumper wire is secured across its wiring pigtail, as seen in Figure 5-10 on the right.
Listing 5-1. The Servo Sweep Sketch
// Sweep
// by BARRAGAN <http://barraganstudio.com>
// This example code is in the public domain.
#include<Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to
180degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); //waits 15 ms for the servo to reach the position
}
for(pos = 180; pos >= 1; pos- = 1) // goes from 180 degrees to
0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15 ms for the servo to reach the position
}
}
The Sweep sketch shown in Listing 5-1 is located in the ArduinoIDE at File→Examples→ Servo→ Sweep. Figure 5-11 shows the Sweep sketch within the Examples directory. When you upload the code to the Arduino computational platform, the servo motor begins to sweep between the established rotational angles of 0° to 180°. As mentioned, this sweep motion is continuous.
Figure 5-11. Obtaining the Sweep sketch within the Arduino-Processing IDE
Physical Computing: A Servo Motor with a Potentiometer
If you add a potentiometer to the servo setup you have now, you can accommodate easy human interaction with the electromechanical components. By adding a potentiometer, manual control of the servo motor is possible. The potentiometer allows the servo motor’s angular position to be dialed in precisely. By rotating the potentiometer’s shaft (wiper arm), the servo motor’s degree of motion can be changed. Discrete angles such as 35°, 45°, 60°, and 110° can easily be dialed in using the potentiometer. The wiring to the Arduino’s PCB inline header connectors is accomplished as shown in Figure 5-12. The circuit schematic diagram is illustrated in Figure 5-13 with the actual prototype shown in Figure 5-14. The sketch for controlling the servo motor with
a potentiometer is obtained from the Arduino-Processing IDE by clicking File → Examples → Servo →Knob. Figure 5-15 shows the Knob sketch within the Examples directory. The Knob sketch is shown in Listing 5-2. 电机物理计算英文文献和中文翻译(5):http://www.751com.cn/fanyi/lunwen_29982.html