how to program servo motor for robotic arm

I am working on a robotics project. I have designed an arm which lifts the objects up and puts them down.
I want to create a function/procedure in arduino (say fork()) which when called should take the arm up in order to pick some object and then I want to move forward, and then pick the thing.

Right now, I am trying .write(deg) function available in servo library, but it does not seem to fulfill my requirements. I want to lift the arm for some while and move forward and them make the arm down, but I am seeing the arm repeatedly move up and down, even I have inserted a single command to fork, but the arm contnuously rotates moves up and down.

Please help me.

Please post code.
Please don't post multiple posts on the same topic.

code is given below:

#include <Servo.h>

Servo myServo;

void setup(){
myServo.attach(9);
Serial.begin(9600);

}

void loop(){

if(Serial.available()){
char val = (char) Serial.read();

if(val == 'l'){
lift(); //lift the object
}

else if (val == 'p'){
putDown(); //put the object down
}
}
}

void lift(){
myServo.write(45);
}

void putDown(){
myServo.write(0);

}

I want to lift the object when I send 'l' to the arduino and put the object when I send 'p' to it. But, the servo arm continuously lifts and puts the object down. It's totally unexpected.
Please help me, how can I achieve my goal.

What is sending the serial data?

I am sending this via Serial monitor

I am sending this via Serial monitor

First, I find your code very difficult to read. Putting the { on the same line as the function/statement made sense when monitors could only display 24 lines at a time. That ceased to be a problem years ago. Now, it makes more sense to put each on it's own line. When it did make sense to put the { on the same line as the {, you can bet that extraneous blank lines were not used.

It makes sense to use the Tools + Auto Format tool to properly indent your code.

It makes sense to follow the rules and post code properly.

I don't see anything in that code that would cause the servo to move unless a 'l' or 'p' arrived on the serial port.

By the way, Serial.available() doesn't return a boolean. You shouldn't treat the return value as a boolean. It is far clearer that you understand what is supposed to happen if you explicitly test for a reasonable value.

Where are your Serial.print() statements?

NB: keep in mind most posts here are simply people's opinions. Whether you put the { on the same line or not is irrelevant.

If you do if (Serial.available()) it's immediately apparent what is happening, and is the same as the official example provide by Arduino here: Serial.available() - Arduino Reference

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);

}

void loop() {
  // read from port 0, send to port 1:
  if (Serial.available()) {
    int inByte = Serial.read();
    Serial1.print(inByte, BYTE);

  }
  // read from port 1, send to port 0:
  if (Serial1.available()) {
    int inByte = Serial1.read();
    Serial.print(inByte, BYTE);
  }
}

So again, don't feel you need to change anything there.

I have no problem reading your code whatsoever.

Beyond that, I cannot see why the sketch as you have posted would continuously operate. Can you give us a bit more detail as to how you are sending commands? Something from your description is missing if it is behaving as you say.

So again, don't feel you need to change anything there.

Since the code is not working as-is, I guess I would feel that I needed to make a change. YMMV.

Beyond that, I cannot see why the sketch as you have posted would continuously operate.

I believe I said the same thing. I also suggested adding Serial.print() statements to help understand why the code is not behaving as expected. Since you don't seem to feel the need for any more information, I'll leave it to you to help OP find a solution.

Some servo test code (may or may not work, no way to currently test) that provides for sending commands to more than one servo at a time. You can test using the serial monitor (make an arm position control string like 30c,180b,70a,120d, in notepad, copy the control string, paste in the serial monitor text box using keyboard ctrl-v, then click send). Note that servos need a large 6v power supply to function properly.

//zoomkat 11-22-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added 

String readString;
#include <Servo.h> 
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control 
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
        }
         readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

sir i actually doing a project on robotic arm with two sensors on the gripper(inductive and capacitive) ,so that it can sense a metallic and non metallic parts and keep the robotic arm to the different position,
sir i am finding difficulty to find out the codes , so sir please send me the codes for that and if possible please post on my comment??????? please sir

please help me

vivek16_:
please help me

What, EXACTLY, do you need help with? Reading the sensors? Making the gripper, about which you have provided NO details, squash the object you are trying to pick up?

vivek16_:
please help me

6 minutes for a bump.... that must be a record.

95% of all issues people have with servos is inadequate power supply. How are you powering
your servo?

sir i am finding difficulty to find out the codes , so sir please send me the codes for that and if possible please post on my comment??????? please sir

I posted code that you should be able to use to test your arm and determine its mechanical limits.

For the beginner like me, its difficult to build Robotic Arm, I tried the code given in first few posts, that was easy to understand but didn't worked well.
I found a nice technique (new for me:| source: Robotic arm arduino), to easily control the servos for robotic arm. This guy connected the servos to ADC channels, and mapped the servo angle values with the voltage values using map function. This way we can attach potentiometer for each servo and can move the servos just by rotating Pots. Voltage values will be converted to angle values using 'map' function and servo will rotate.

  sensorvalue0 = analogRead(A0);
  sensorvalue0 = map(sensorvalue0, 0, 1023, 0, 180);
  servo0.write(sensorvalue0);
  sensorvalue1 = analogRead(A1);

Its good to build a first robotic arm with this. Now Building a improved design with this technique.
Can someone tell me if I build more fingers using this, it will be very bulky arm? so how can i reduce the size and weight?

UnoWatt:
That's just using the servo knob approach, in the IDE examples.

Yes, this is exactly what he did, thanks for the link. Can you also help me in reducing the weight and size of that arm, are there any micro servos?

Do a search for micro servo, many pop up. Adafruit carries one for example.