Example Code SparkFun Robotic Arm

February 23, 2013
Tags: ,

Here is some more example code for a 3 servo arm. I call this the robot demo. It simply moves the robot around slowly.


/*
Robot demo

*/

#include   // servo library

Servo servo1; //base-pan
Servo servo2; //arm-tilt
Servo servo3; //claw

int posf1 = 140; //pan
int posf2 = 90;
int posf3 = 90;

void setup()
{
  Serial.begin(9600);
  servo1.attach(9);  //pan range is 10 to 170
  servo2.attach(10); //tilt
  servo3.attach(11); //claw range is 50 to 120
}

void loop()
{
  //pan
  for(posf1 = 140; posf1 > 20; posf1 -= 1)
  {
    servo1.write(posf1);
    Serial.print("PanNum: ");
    Serial.println(posf1);

    delay(25);
  }

  //pan
  for(posf1 = 20; posf1 < 140; posf1 += 1)
  {
    servo1.write(posf1);
    Serial.print("PanNum: ");
    Serial.println(posf1);

    delay(25);
  }

    //tilt
  for(posf2 = 179; posf2 > 1; posf2 -= 1)
  {
    servo2.write(posf2);
    Serial.print("TiltNum: ");
    Serial.println(posf2);
    delay(25);
  }

  //tilt
  for(posf2 = 1; posf2 < 179; posf2 += 1)
  {
    servo2.write(posf2);
    delay(25);
  }

  //claw
  for(posf3 = 130; posf3 > 70; posf3 -= 1)
  {
    servo3.write(posf3);
    delay(25);
  }

  //claw
  for(posf3 = 70; posf3 < 130; posf3 += 1)
  {
    servo3.write(posf3);
    delay(25);
  }

}

Leave a Reply