Tuesday, June 12, 2012

Servo Sweep Test

I did this so that I could test a servo and see how it runs.  It has both a sweep in the void loop and a specific angle set in the void setup.  It was run on an Arduio UNO Board


// Servo Sweep Test
// by Philip Leete

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
                // a maximum of eight servo objects can be created

int pos;            // variable to store the servo position
int Speed = 15;     // variable to set the speed of sweep
int step_size = 2;  //Step size for sweep

void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object  

  myservo.write(170);  //Moves servo arm to 170 degrees
  delay (5000);        //Wait 5 sec. so you can see the movement
  myservo.write(90);   //Moves servo arm to 90 degrees
  delay (5000);        //Wait 5 sec. so you can see the movement
}


void loop()
{
  for(pos = 10; pos < 170; pos += step_size)  // goes from 10 degrees to 170 degrees
  {                                        //I noticed that there are some odd skips and hops that happen if
                                           //you run the servo to 180 and 0 degrees.
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(Speed);                  
  }
  for(pos = 170; pos>=10; pos -= step_size)     // goes from 170 degrees to 10 degrees
  {                              
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(Speed);                  
  }
}

No comments:

Post a Comment