Saturday, December 1, 2012

Servo

12/1/2012

Arduino provides very convenient interface to servos. Simply connect red wire to +5v, brown to GND, and yellow (signal) to pin No. 9 (there seems to be limitations on which pin can be used to control a servo). There is a Servo library provides simple functions like write(angle), which commands the servo to turn to a certain angle, or read(), which returns the servo angle. However, after a few experimenting, the read() function does not return the real time angle. It makes sense as there is no feed back from the servo to Arduino. It seems that servo did all the feedback control within itself. Though this simplifies the control, it lacks flexibility. For my purpose of use, I will really need the "in position" feedback which tells the controller whether or not the servo has turned to the commanded angle. I guess I need to figure something else.

A sample control code for Arduino modified from Sweep: ()


#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
int led=13;

void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  pinMode(led,OUTPUT);
}

int first=0;

void loop(){
myservo.write(pos);
//delay(15);
if (myservo.read()==pos){
  digitalWrite(led,HIGH);
  delay(50);
  digitalWrite(led,LOW);
  delay(50);
}

if (pos>=180){pos=0;}
pos+=10;

}

No comments:

Post a Comment