Notes and explanations
Servos come in handy. Whereas motors are more difficult to control, and, with the exception of stepper motors have a nasty way of spinning up to speed and down to a stop, servos go both directions with excellent control. They also commonly come in at 5V. This is a simple sketch controlling a servo. It's simple to wire - there is a power(runs to 5V pin), ground (ground) and signal. This makes it easier to power using outside sources (make certain ground is to power source).
Stepper motors are another nice option and can be controlled with a high degree of accuracy. There are two types of stepper motors, both of which can be controlled both forwards and backwards. Both need specific circuits to run. Unipolar steppers need a U2004 Darlington Array, and bipolars need a SN75410HE H-Bridge. We don't have the parts here to make these but there is a very good article about them at Tom Igoe's page on stepper motors. The arduino site has code and more information at http://arduino.cc/en/Tutorial/MotorKnob.
Parts & Symbols
Sevo
Copy & Paste the following code to the Arduino IDE.
// 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 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms 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 15ms for the servo to reach the position
}
}