Servo Control
Overview
Using servos in a robot is nice for any component that requires precise rotation, or simply a way to move. With exact angle measures or degree rotations, calibration with the Arduino will ensure that your robot can utilize its full range of motion. While, simply hooking your servos up to a wireless receiver, can have your mobility problems solved. Simple and versatile, servos are a great tool.
Parts
-Arduino microcontroller and carrier board
-LiPo battery
-Servo
Prepare the breadboard
Program the Microcontroller
See also: RC Controller, Servo Sweep (for full 180 degree motion).
/**
* @file: servo control example
*
* @description
* drive forward, drive backward
* 1 servo, LiPo battery
*
*
The minimum (backward) and maximum (forward) values
*
will be different depending on your specific servo motor.
*
Ideally, it should be between 1 and 2 milliseconds, but in practice,
*
0.5 - 2.5 milliseconds works well.
*
Try different values to see what numbers are best for you.
*
*/
// include additional headers
#include <Servo.h>
//global declarations
#define SERVO 4 //define a pin for servo
Servo myservo; // initialize servo
// played around with values that sets the servos to each position
// these values need to be set for each servo!!!
const int neutral = 1500;
const int forward = 2000;
const int backward = 1000;
//--- Function: Setup ()
void setup()
{
pinMode (SERVO, OUTPUT); // want servo pin to be an output
myservo.attach(SERVO); // attach pin to the servo
myservo.writeMicroseconds(neutral); // set servo to mid-point
}
//--- Function: loop ()
void loop()
{
myservo.writeMicroseconds(forward); // set servo to forward pulse
delay(200); // Pauses the program
myservo.writeMicroseconds(neutral); // set servo to mid-point
delay(200); // Pauses the program
myservo.writeMicroseconds(backward); // set servo to backward
delay(200); // Pauses the program
}